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