blob: 619b99c70ac76facc4b8f9e959edcb46a36b4be0 [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
Ray Milkey1567b532018-02-05 10:25:32 -080020import com.google.common.base.Objects;
Simon Hunte556e942017-06-19 15:35:44 -070021import com.google.common.collect.ImmutableSortedMap;
22import com.google.common.collect.ImmutableSortedSet;
23
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Map;
27import java.util.Set;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
Simon Hunt0c85f112017-06-12 21:02:17 -070031/**
32 * Encapsulates a bundle of localization strings.
33 */
34public final class LionBundle {
35
Simon Hunte556e942017-06-19 15:35:44 -070036 private final String id;
37 private final Set<LionItem> items;
38 private final Map<String, String> mapped;
Simon Hunt0c85f112017-06-12 21:02:17 -070039
Simon Hunte556e942017-06-19 15:35:44 -070040 private LionBundle(String id, Set<LionItem> items) {
41 this.id = id;
42 this.items = ImmutableSortedSet.copyOf(items);
43 mapped = createLookup();
44 }
45
46 private Map<String, String> createLookup() {
47 Map<String, String> lookup = new HashMap<>(items.size());
48 for (LionItem item : items) {
49 lookup.put(item.key(), item.value());
50 }
51 return ImmutableSortedMap.copyOf(lookup);
Simon Hunt0c85f112017-06-12 21:02:17 -070052 }
53
54 /**
Simon Hunte556e942017-06-19 15:35:44 -070055 * Returns the bundle's identifier.
Simon Hunt0c85f112017-06-12 21:02:17 -070056 *
Simon Hunte556e942017-06-19 15:35:44 -070057 * @return the bundle's ID
Simon Hunt0c85f112017-06-12 21:02:17 -070058 */
Simon Hunte556e942017-06-19 15:35:44 -070059 public String id() {
60 return id;
Simon Hunt0c85f112017-06-12 21:02:17 -070061 }
62
Simon Hunte556e942017-06-19 15:35:44 -070063 /**
64 * Returns the number of entries in this bundle.
65 *
66 * @return number of entries
67 */
68 public int size() {
69 return items.size();
70 }
Simon Hunt0c85f112017-06-12 21:02:17 -070071
Simon Hunte556e942017-06-19 15:35:44 -070072 @Override
73 public String toString() {
74 return "LionBundle{id=" + id + ", #items=" + size() + "}";
75 }
76
77 /**
78 * Returns the localized value for the given key, or null if no such
79 * mapping exists.
80 *
81 * @param key the key
82 * @return the localized value
83 */
84 public String getValue(String key) {
85 return mapped.get(key);
86 }
87
Simon Hunte6c55dd2017-06-21 14:33:02 -070088 /**
Simon Hunt879ce452017-08-10 23:32:00 -070089 * Returns the localized value for the given key, or, if no such mapping
90 * exists, returns the key wrapped in '%' characters.
91 *
92 * @param key the key
93 * @return the localized value (or a wrapped key placeholder)
94 */
95 public String getSafe(String key) {
96 String value = mapped.get(key);
97 return value == null ? "%" + key + "%" : value;
98 }
99
100 /**
Simon Hunta58d8942017-08-11 12:51:14 -0700101 * Converts the given enum constant to lowercase and then uses that as the
102 * key to invoke {@link #getSafe(String)}.
103 *
104 * @param enumConst the constant to use as the key
105 * @return the localized value (or a wrapped key placeholder)
106 */
107 public String getSafe(Enum<?> enumConst) {
108 return getSafe(enumConst.name().toLowerCase());
109 }
110
111 /**
Simon Huntd6d3ad32017-06-21 15:27:06 -0700112 * Returns an immutable set of the items in this bundle.
113 *
114 * @return the items in this bundle
115 */
116 public Set<LionItem> getItems() {
117 return items;
118 }
119
120 /**
Simon Hunte6c55dd2017-06-21 14:33:02 -0700121 * Dump the contents of the bundle.
122 *
123 * @return dumped contents
124 */
125 public String dump() {
126 return mapped.toString();
127 }
128
Simon Hunte556e942017-06-19 15:35:44 -0700129 // === --------------------------------------------------------------------
130
131 /**
132 * Builder of Lion Bundles.
133 */
134 public static final class Builder {
135 private final String id;
136 private final Set<LionItem> items = new HashSet<>();
137
138 /**
139 * Creates a builder of Lion Bundles.
140 *
141 * @param id the bundle's identifier
142 */
143 public Builder(String id) {
144 this.id = id;
145 }
146
147 /**
148 * Returns the bundle ID.
149 *
150 * @return the bundle ID
151 */
152 public String id() {
153 return id;
154 }
155
156 /**
157 * Adds an item to the bundle.
158 *
159 * @param key the item key
160 * @param value the item value
161 * @return self, for chaining
162 */
163 public Builder addItem(String key, String value) {
164 items.add(new LionItem(key, value));
165 return this;
166 }
167
168 /**
169 * Builds the lion bundle from this builder instance.
170 *
171 * @return the lion bundle
172 */
173 public LionBundle build() {
174 return new LionBundle(id, items);
175 }
176 }
177
178 // === --------------------------------------------------------------------
179
180 /**
181 * Represents a single localization item.
182 */
183 public static final class LionItem implements Comparable<LionItem> {
184 private final String key;
185 private final String value;
186
187 /**
188 * Creates a lion item with the given key and value.
189 *
190 * @param key the key
191 * @param value the value
192 */
193 private LionItem(String key, String value) {
194 checkNotNull(key);
195 checkNotNull(value);
196 this.key = key;
197 this.value = value;
198 }
199
200 @Override
201 public String toString() {
202 return "LionItem{key=" + key + ", value=\"" + value + "\"}";
203 }
204
205 @Override
206 public int compareTo(LionItem o) {
207 return key.compareTo(o.key);
208 }
209
Ray Milkey1567b532018-02-05 10:25:32 -0800210 @Override
211 public boolean equals(Object obj) {
212
213 if (obj == null) {
214 return false;
215 }
216 if (getClass() != obj.getClass()) {
217 return false;
218 }
219 final LionBundle.LionItem that = (LionBundle.LionItem) obj;
220 return Objects.equal(this.key, that.key);
221 }
222
223 @Override
224 public int hashCode() {
225 return Objects.hashCode(this.key);
226 }
227
Simon Hunte556e942017-06-19 15:35:44 -0700228 /**
229 * Returns the key.
230 *
231 * @return the key
232 */
233 public String key() {
234 return key;
235 }
236
237 /**
238 * Returns the value.
239 *
240 * @return the value
241 */
242 public String value() {
243 return value;
244 }
245 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700246}