Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 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 | |
| 18 | package org.onosproject.ui.lion; |
| 19 | |
Ray Milkey | 1567b53 | 2018-02-05 10:25:32 -0800 | [diff] [blame] | 20 | import com.google.common.base.Objects; |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSortedMap; |
| 22 | import com.google.common.collect.ImmutableSortedSet; |
| 23 | |
| 24 | import java.util.HashMap; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Encapsulates a bundle of localization strings. |
| 33 | */ |
| 34 | public final class LionBundle { |
| 35 | |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 36 | private final String id; |
| 37 | private final Set<LionItem> items; |
| 38 | private final Map<String, String> mapped; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 39 | |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 40 | 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 Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 55 | * Returns the bundle's identifier. |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 56 | * |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 57 | * @return the bundle's ID |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 58 | */ |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 59 | public String id() { |
| 60 | return id; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 63 | /** |
| 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 Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 71 | |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 72 | @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 Hunt | e6c55dd | 2017-06-21 14:33:02 -0700 | [diff] [blame] | 88 | /** |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 89 | * 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 Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 101 | * 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 Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 112 | * 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 Hunt | e6c55dd | 2017-06-21 14:33:02 -0700 | [diff] [blame] | 121 | * Dump the contents of the bundle. |
| 122 | * |
| 123 | * @return dumped contents |
| 124 | */ |
| 125 | public String dump() { |
| 126 | return mapped.toString(); |
| 127 | } |
| 128 | |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 129 | // === -------------------------------------------------------------------- |
| 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 Milkey | 1567b53 | 2018-02-05 10:25:32 -0800 | [diff] [blame] | 210 | @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 Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 228 | /** |
| 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 Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 246 | } |