blob: 56fc4bc601dbca07cacfc855063319136bb9ad24 [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 Hunta58d8942017-08-11 12:51:14 -0700100 * Converts the given enum constant to lowercase and then uses that as the
101 * key to invoke {@link #getSafe(String)}.
102 *
103 * @param enumConst the constant to use as the key
104 * @return the localized value (or a wrapped key placeholder)
105 */
106 public String getSafe(Enum<?> enumConst) {
107 return getSafe(enumConst.name().toLowerCase());
108 }
109
110 /**
Simon Huntd6d3ad32017-06-21 15:27:06 -0700111 * Returns an immutable set of the items in this bundle.
112 *
113 * @return the items in this bundle
114 */
115 public Set<LionItem> getItems() {
116 return items;
117 }
118
119 /**
Simon Hunte6c55dd2017-06-21 14:33:02 -0700120 * Dump the contents of the bundle.
121 *
122 * @return dumped contents
123 */
124 public String dump() {
125 return mapped.toString();
126 }
127
Simon Hunte556e942017-06-19 15:35:44 -0700128 // === --------------------------------------------------------------------
129
130 /**
131 * Builder of Lion Bundles.
132 */
133 public static final class Builder {
134 private final String id;
135 private final Set<LionItem> items = new HashSet<>();
136
137 /**
138 * Creates a builder of Lion Bundles.
139 *
140 * @param id the bundle's identifier
141 */
142 public Builder(String id) {
143 this.id = id;
144 }
145
146 /**
147 * Returns the bundle ID.
148 *
149 * @return the bundle ID
150 */
151 public String id() {
152 return id;
153 }
154
155 /**
156 * Adds an item to the bundle.
157 *
158 * @param key the item key
159 * @param value the item value
160 * @return self, for chaining
161 */
162 public Builder addItem(String key, String value) {
163 items.add(new LionItem(key, value));
164 return this;
165 }
166
167 /**
168 * Builds the lion bundle from this builder instance.
169 *
170 * @return the lion bundle
171 */
172 public LionBundle build() {
173 return new LionBundle(id, items);
174 }
175 }
176
177 // === --------------------------------------------------------------------
178
179 /**
180 * Represents a single localization item.
181 */
182 public static final class LionItem implements Comparable<LionItem> {
183 private final String key;
184 private final String value;
185
186 /**
187 * Creates a lion item with the given key and value.
188 *
189 * @param key the key
190 * @param value the value
191 */
192 private LionItem(String key, String value) {
193 checkNotNull(key);
194 checkNotNull(value);
195 this.key = key;
196 this.value = value;
197 }
198
199 @Override
200 public String toString() {
201 return "LionItem{key=" + key + ", value=\"" + value + "\"}";
202 }
203
204 @Override
205 public int compareTo(LionItem o) {
206 return key.compareTo(o.key);
207 }
208
209 /**
210 * Returns the key.
211 *
212 * @return the key
213 */
214 public String key() {
215 return key;
216 }
217
218 /**
219 * Returns the value.
220 *
221 * @return the value
222 */
223 public String value() {
224 return value;
225 }
226 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700227}