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