blob: 780dea81f861e9ae6404c15ddd2c04d0e40e468e [file] [log] [blame]
Simon Hunt0c85f112017-06-12 21:02:17 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Hunt0c85f112017-06-12 21:02:17 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.ui.lion;
19
Simon Hunte556e942017-06-19 15:35:44 -070020import com.google.common.collect.ImmutableSortedMap;
21import com.google.common.collect.ImmutableSortedSet;
22
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.Map;
26import java.util.Set;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
Simon Hunt0c85f112017-06-12 21:02:17 -070030/**
31 * Encapsulates a bundle of localization strings.
32 */
33public final class LionBundle {
34
Simon Hunte556e942017-06-19 15:35:44 -070035 private final String id;
36 private final Set<LionItem> items;
37 private final Map<String, String> mapped;
Simon Hunt0c85f112017-06-12 21:02:17 -070038
Simon Hunte556e942017-06-19 15:35:44 -070039 private LionBundle(String id, Set<LionItem> items) {
40 this.id = id;
41 this.items = ImmutableSortedSet.copyOf(items);
42 mapped = createLookup();
43 }
44
45 private Map<String, String> createLookup() {
46 Map<String, String> lookup = new HashMap<>(items.size());
47 for (LionItem item : items) {
48 lookup.put(item.key(), item.value());
49 }
50 return ImmutableSortedMap.copyOf(lookup);
Simon Hunt0c85f112017-06-12 21:02:17 -070051 }
52
53 /**
Simon Hunte556e942017-06-19 15:35:44 -070054 * Returns the bundle's identifier.
Simon Hunt0c85f112017-06-12 21:02:17 -070055 *
Simon Hunte556e942017-06-19 15:35:44 -070056 * @return the bundle's ID
Simon Hunt0c85f112017-06-12 21:02:17 -070057 */
Simon Hunte556e942017-06-19 15:35:44 -070058 public String id() {
59 return id;
Simon Hunt0c85f112017-06-12 21:02:17 -070060 }
61
Simon Hunte556e942017-06-19 15:35:44 -070062 /**
63 * Returns the number of entries in this bundle.
64 *
65 * @return number of entries
66 */
67 public int size() {
68 return items.size();
69 }
Simon Hunt0c85f112017-06-12 21:02:17 -070070
Simon Hunte556e942017-06-19 15:35:44 -070071 @Override
72 public String toString() {
73 return "LionBundle{id=" + id + ", #items=" + size() + "}";
74 }
75
76 /**
77 * Returns the localized value for the given key, or null if no such
78 * mapping exists.
79 *
80 * @param key the key
81 * @return the localized value
82 */
83 public String getValue(String key) {
84 return mapped.get(key);
85 }
86
Simon Hunte6c55dd2017-06-21 14:33:02 -070087 /**
Simon Hunt879ce452017-08-10 23:32:00 -070088 * Returns the localized value for the given key, or, if no such mapping
89 * exists, returns the key wrapped in '%' characters.
90 *
91 * @param key the key
92 * @return the localized value (or a wrapped key placeholder)
93 */
94 public String getSafe(String key) {
95 String value = mapped.get(key);
96 return value == null ? "%" + key + "%" : value;
97 }
98
99 /**
Simon Huntd6d3ad32017-06-21 15:27:06 -0700100 * Returns an immutable set of the items in this bundle.
101 *
102 * @return the items in this bundle
103 */
104 public Set<LionItem> getItems() {
105 return items;
106 }
107
108 /**
Simon Hunte6c55dd2017-06-21 14:33:02 -0700109 * Dump the contents of the bundle.
110 *
111 * @return dumped contents
112 */
113 public String dump() {
114 return mapped.toString();
115 }
116
Simon Hunte556e942017-06-19 15:35:44 -0700117 // === --------------------------------------------------------------------
118
119 /**
120 * Builder of Lion Bundles.
121 */
122 public static final class Builder {
123 private final String id;
124 private final Set<LionItem> items = new HashSet<>();
125
126 /**
127 * Creates a builder of Lion Bundles.
128 *
129 * @param id the bundle's identifier
130 */
131 public Builder(String id) {
132 this.id = id;
133 }
134
135 /**
136 * Returns the bundle ID.
137 *
138 * @return the bundle ID
139 */
140 public String id() {
141 return id;
142 }
143
144 /**
145 * Adds an item to the bundle.
146 *
147 * @param key the item key
148 * @param value the item value
149 * @return self, for chaining
150 */
151 public Builder addItem(String key, String value) {
152 items.add(new LionItem(key, value));
153 return this;
154 }
155
156 /**
157 * Builds the lion bundle from this builder instance.
158 *
159 * @return the lion bundle
160 */
161 public LionBundle build() {
162 return new LionBundle(id, items);
163 }
164 }
165
166 // === --------------------------------------------------------------------
167
168 /**
169 * Represents a single localization item.
170 */
171 public static final class LionItem implements Comparable<LionItem> {
172 private final String key;
173 private final String value;
174
175 /**
176 * Creates a lion item with the given key and value.
177 *
178 * @param key the key
179 * @param value the value
180 */
181 private LionItem(String key, String value) {
182 checkNotNull(key);
183 checkNotNull(value);
184 this.key = key;
185 this.value = value;
186 }
187
188 @Override
189 public String toString() {
190 return "LionItem{key=" + key + ", value=\"" + value + "\"}";
191 }
192
193 @Override
194 public int compareTo(LionItem o) {
195 return key.compareTo(o.key);
196 }
197
198 /**
199 * Returns the key.
200 *
201 * @return the key
202 */
203 public String key() {
204 return key;
205 }
206
207 /**
208 * Returns the value.
209 *
210 * @return the value
211 */
212 public String value() {
213 return value;
214 }
215 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700216}