blob: 5b6f1103328c7e9ca3f11493f1367b731ce01279 [file] [log] [blame]
Simon Hunt0c85f112017-06-12 21:02:17 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
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 Huntd6d3ad32017-06-21 15:27:06 -070088 * Returns an immutable set of the items in this bundle.
89 *
90 * @return the items in this bundle
91 */
92 public Set<LionItem> getItems() {
93 return items;
94 }
95
96 /**
Simon Hunte6c55dd2017-06-21 14:33:02 -070097 * Dump the contents of the bundle.
98 *
99 * @return dumped contents
100 */
101 public String dump() {
102 return mapped.toString();
103 }
104
Simon Hunte556e942017-06-19 15:35:44 -0700105 // === --------------------------------------------------------------------
106
107 /**
108 * Builder of Lion Bundles.
109 */
110 public static final class Builder {
111 private final String id;
112 private final Set<LionItem> items = new HashSet<>();
113
114 /**
115 * Creates a builder of Lion Bundles.
116 *
117 * @param id the bundle's identifier
118 */
119 public Builder(String id) {
120 this.id = id;
121 }
122
123 /**
124 * Returns the bundle ID.
125 *
126 * @return the bundle ID
127 */
128 public String id() {
129 return id;
130 }
131
132 /**
133 * Adds an item to the bundle.
134 *
135 * @param key the item key
136 * @param value the item value
137 * @return self, for chaining
138 */
139 public Builder addItem(String key, String value) {
140 items.add(new LionItem(key, value));
141 return this;
142 }
143
144 /**
145 * Builds the lion bundle from this builder instance.
146 *
147 * @return the lion bundle
148 */
149 public LionBundle build() {
150 return new LionBundle(id, items);
151 }
152 }
153
154 // === --------------------------------------------------------------------
155
156 /**
157 * Represents a single localization item.
158 */
159 public static final class LionItem implements Comparable<LionItem> {
160 private final String key;
161 private final String value;
162
163 /**
164 * Creates a lion item with the given key and value.
165 *
166 * @param key the key
167 * @param value the value
168 */
169 private LionItem(String key, String value) {
170 checkNotNull(key);
171 checkNotNull(value);
172 this.key = key;
173 this.value = value;
174 }
175
176 @Override
177 public String toString() {
178 return "LionItem{key=" + key + ", value=\"" + value + "\"}";
179 }
180
181 @Override
182 public int compareTo(LionItem o) {
183 return key.compareTo(o.key);
184 }
185
186 /**
187 * Returns the key.
188 *
189 * @return the key
190 */
191 public String key() {
192 return key;
193 }
194
195 /**
196 * Returns the value.
197 *
198 * @return the value
199 */
200 public String value() {
201 return value;
202 }
203 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700204}