• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2017 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 package ohos.global.icu.impl.locale;
5 
6 import java.io.BufferedReader;
7 import java.io.File;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.net.URL;
11 import java.nio.charset.Charset;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.LinkedHashMap;
19 import java.util.LinkedHashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
24 import java.util.TreeMap;
25 import java.util.TreeSet;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 
29 import ohos.global.icu.util.ICUException;
30 import ohos.global.icu.util.ICUUncheckedIOException;
31 
32 /**
33  * Stub class to make migration easier until we get either Guava or a higher level of Java.
34  * @hide exposed on OHOS
35  */
36 public class XCldrStub {
37 
38     /**
39      * @hide exposed on OHOS
40      */
41     public static class Multimap<K, V> {
42         private final Map<K,Set<V>> map;
43         private final Class<Set<V>> setClass;
44 
45         @SuppressWarnings("unchecked")
Multimap(Map<K,Set<V>> map, Class<?> setClass)46         private Multimap(Map<K,Set<V>> map, Class<?> setClass) {
47             this.map = map;
48             this.setClass = (Class<Set<V>>) (setClass != null
49                     ? setClass
50                             : HashSet.class);
51         }
52         @SafeVarargs
53         @SuppressWarnings("varargs")    // Not supported by Eclipse, but we need this for javac
putAll(K key, V... values)54         public final Multimap<K, V> putAll(K key, V... values) {
55             if (values.length != 0) {
56                 createSetIfMissing(key).addAll(Arrays.asList(values));
57             }
58             return this;
59         }
putAll(K key, Collection<V> values)60         public void putAll(K key, Collection<V> values) {
61             if (!values.isEmpty()) {
62                 createSetIfMissing(key).addAll(values);
63             }
64         }
putAll(Collection<K> keys, V value)65         public void putAll(Collection<K> keys, V value) {
66             for (K key : keys) {
67                 put(key, value);
68             }
69         }
putAll(Multimap<K, V> source)70         public void putAll(Multimap<K, V> source) {
71             for (Entry<K, Set<V>> entry : source.map.entrySet()) {
72                 putAll(entry.getKey(), entry.getValue());
73             }
74         }
put(K key, V value)75         public void put(K key, V value) {
76             createSetIfMissing(key).add(value);
77         }
createSetIfMissing(K key)78         private Set<V> createSetIfMissing(K key) {
79             Set<V> old = map.get(key);
80             if (old == null) {
81                 map.put(key, old = getInstance());
82             }
83             return old;
84         }
getInstance()85         private Set<V> getInstance() {
86             try {
87                 return setClass.newInstance();
88             } catch (Exception e) {
89                 throw new ICUException(e);
90             }
91         }
get(K key)92         public Set<V> get(K key) {
93             Set<V> result = map.get(key);
94             return result; //  == null ? Collections.<V>emptySet() : result;
95         }
keySet()96         public Set<K> keySet() {
97             return map.keySet();
98         }
asMap()99         public Map<K, Set<V>> asMap() {
100             return map;
101         }
values()102         public Set<V> values() {
103             Collection<Set<V>> values = map.values();
104             if (values.size() == 0) {
105                 return Collections.<V>emptySet();
106             }
107             Set<V> result = getInstance();
108             for ( Set<V> valueSet : values) {
109                 result.addAll(valueSet);
110             }
111             return result;
112         }
size()113         public int size() {
114             return map.size();
115         }
entries()116         public Iterable<Entry<K, V>> entries() {
117             return new MultimapIterator<>(map);
118         }
119         @Override
equals(Object obj)120         public boolean equals(Object obj) {
121             return this == obj ||
122                     (obj != null
123                     && obj.getClass() == this.getClass()
124                     && map.equals(((Multimap<?,?>) obj).map));
125         }
126 
127         @Override
hashCode()128         public int hashCode() {
129             return map.hashCode();
130         }
131     }
132 
133     /**
134      * @hide exposed on OHOS
135      */
136     public static class Multimaps {
invertFrom(Multimap<V, K> source, R target)137         public static <K, V, R extends Multimap<K, V>> R invertFrom(Multimap<V, K> source, R target) {
138             for (Entry<V, Set<K>> entry : source.asMap().entrySet()) {
139                 target.putAll(entry.getValue(), entry.getKey());
140             }
141             return target;
142         }
invertFrom(Map<V, K> source, R target)143         public static <K, V, R extends Multimap<K, V>> R invertFrom(Map<V, K> source, R target) {
144             for (Entry<V, K> entry : source.entrySet()) {
145                 target.put(entry.getValue(), entry.getKey());
146             }
147             return target;
148         }
149         /**
150          * Warning, not functionally the same as Guava; only for use in invertFrom.
151          */
forMap(Map<K,V> map)152         public static <K, V> Map<K,V> forMap(Map<K,V> map) {
153             return map;
154         }
155     }
156 
157     private static class MultimapIterator<K,V> implements Iterator<Entry<K,V>>, Iterable<Entry<K,V>> {
158         private final Iterator<Entry<K, Set<V>>> it1;
159         private Iterator<V> it2 = null;
160         private final ReusableEntry<K,V> entry = new ReusableEntry<>();
161 
MultimapIterator(Map<K,Set<V>> map)162         private MultimapIterator(Map<K,Set<V>> map) {
163             it1 = map.entrySet().iterator();
164         }
165         @Override
hasNext()166         public boolean hasNext() {
167             return it1.hasNext() || it2 != null && it2.hasNext();
168         }
169         @Override
next()170         public Entry<K, V> next() {
171             if (it2 != null && it2.hasNext()) {
172                 entry.value = it2.next();
173             } else {
174                 Entry<K, Set<V>> e = it1.next();
175                 entry.key = e.getKey();
176                 it2 = e.getValue().iterator();
177             }
178             return entry;
179         }
180         @Override
iterator()181         public Iterator<Entry<K, V>> iterator() {
182             return this;
183         }
184         @Override
remove()185         public void remove() {
186             throw new UnsupportedOperationException();
187         }
188     }
189 
190     private static class ReusableEntry<K,V> implements Entry<K,V> {
191         K key;
192         V value;
193         @Override
getKey()194         public K getKey() {
195             return key;
196         }
197         @Override
getValue()198         public V getValue() {
199             return value;
200         }
201         @Override
setValue(V value)202         public V setValue(V value) {
203             throw new UnsupportedOperationException();
204         }
205     }
206 
207     /**
208      * @hide exposed on OHOS
209      */
210     public static class HashMultimap<K, V> extends Multimap<K, V> {
HashMultimap()211         private HashMultimap() {
212             super(new HashMap<K, Set<V>>(), HashSet.class);
213         }
create()214         public static <K, V> HashMultimap<K, V> create() {
215             return new HashMultimap<>();
216         }
217     }
218 
219     /**
220      * @hide exposed on OHOS
221      */
222     public static class TreeMultimap<K, V> extends Multimap<K, V> {
TreeMultimap()223         private TreeMultimap() {
224             super(new TreeMap<K, Set<V>>(), TreeSet.class);
225         }
create()226         public static <K, V> TreeMultimap<K, V> create() {
227             return new TreeMultimap<>();
228         }
229     }
230 
231     /**
232      * @hide exposed on OHOS
233      */
234     public static class LinkedHashMultimap<K, V> extends Multimap<K, V> {
LinkedHashMultimap()235         private LinkedHashMultimap() {
236             super(new LinkedHashMap<K, Set<V>>(), LinkedHashSet.class);
237         }
create()238         public static <K, V> LinkedHashMultimap<K, V> create() {
239             return new LinkedHashMultimap<>();
240         }
241     }
242 
243 
244     //    public static class Counter<T> implements Iterable<T>{
245     //        private Map<T,Long> data;
246     //        @Override
247     //        public Iterator<T> iterator() {
248     //            return data.keySet().iterator();
249     //        }
250     //        public long get(T s) {
251     //            Long result = data.get(s);
252     //            return result != null ? result : 0L;
253     //        }
254     //        public void add(T item, int count) {
255     //            Long result = data.get(item);
256     //            data.put(item, result == null ? count : result + count);
257     //        }
258     //    }
259 
join(T[] source, String separator)260     public static <T> String join(T[] source, String separator) {
261         StringBuilder result = new StringBuilder();
262         for (int i = 0; i < source.length; ++i) {
263             if (i != 0) result.append(separator);
264             result.append(source[i]);
265         }
266         return result.toString();
267     }
268 
join(Iterable<T> source, String separator)269     public static <T> String join(Iterable<T> source, String separator) {
270         StringBuilder result = new StringBuilder();
271         boolean first = true;
272         for (T item : source) {
273             if (!first) result.append(separator);
274             else first = false;
275             result.append(item.toString());
276         }
277         return result.toString();
278     }
279 
280     /**
281      * @hide exposed on OHOS
282      */
283     public static class CollectionUtilities {
join(U source, String separator)284         public static <T, U extends Iterable<T>> String join(U source, String separator) {
285             return XCldrStub.join(source, separator);
286         }
287     }
288 
289     /**
290      * @hide exposed on OHOS
291      */
292     public static class Joiner {
293         private final String separator;
Joiner(String separator)294         private Joiner(String separator) {
295             this.separator = separator;
296         }
on(String separator)297         public static final Joiner on(String separator) {
298             return new Joiner(separator);
299         }
join(T[] source)300         public <T> String join(T[] source) {
301             return XCldrStub.join(source, separator);
302         }
join(Iterable<T> source)303         public <T> String join(Iterable<T> source) {
304             return XCldrStub.join(source, separator);
305         }
306     }
307 
308     /**
309      * @hide exposed on OHOS
310      */
311     public static class Splitter {
312         Pattern pattern;
313         boolean trimResults = false;
Splitter(char c)314         public Splitter(char c) {
315             this(Pattern.compile("\\Q" + c + "\\E"));
316         }
Splitter(Pattern p)317         public Splitter(Pattern p) {
318             pattern = p;
319         }
on(char c)320         public static Splitter on(char c) {
321             return new Splitter(c);
322         }
on(Pattern p)323         public static Splitter on(Pattern p) {
324             return new Splitter(p);
325         }
splitToList(String input)326         public List<String> splitToList(String input) {
327             String[] items = pattern.split(input);
328             if (trimResults) {
329                 for (int i = 0; i < items.length; ++i) {
330                     items[i] = items[i].trim();
331                 }
332             }
333             return Arrays.asList(items);
334         }
trimResults()335         public Splitter trimResults() {
336             trimResults = true;
337             return this;
338         }
split(String input)339         public Iterable<String> split(String input) {
340             return splitToList(input);
341         }
342     }
343 
344     /**
345      * @hide exposed on OHOS
346      */
347     public static class ImmutableSet {
copyOf(Set<T> values)348         public static <T> Set<T> copyOf(Set<T> values) {
349             return Collections.unmodifiableSet(new LinkedHashSet<>(values)); // copy set for safety, preserve order
350         }
351     }
352     /**
353      * @hide exposed on OHOS
354      */
355     public static class ImmutableMap {
copyOf(Map<K,V> values)356         public static <K,V> Map<K,V> copyOf(Map<K,V> values) {
357             return Collections.unmodifiableMap(new LinkedHashMap<>(values)); // copy set for safety, preserve order
358         }
359     }
360     /**
361      * @hide exposed on OHOS
362      */
363     public static class ImmutableMultimap {
copyOf(Multimap<K,V> values)364         public static <K,V> Multimap<K,V> copyOf(Multimap<K,V> values) {
365             LinkedHashMap<K, Set<V>> temp = new LinkedHashMap<>(); // semi-deep copy, preserve order
366             for (Entry<K, Set<V>> entry : values.asMap().entrySet()) {
367                 Set<V> value = entry.getValue();
368                 temp.put(entry.getKey(), value.size() == 1
369                         ? Collections.singleton(value.iterator().next())
370                                 : Collections.unmodifiableSet(new LinkedHashSet<>(value)));
371             }
372             return new Multimap<>(Collections.unmodifiableMap(temp), null);
373         }
374     }
375 
376     /**
377      * @hide exposed on OHOS
378      */
379     public static class FileUtilities {
380         public static final Charset UTF8 = Charset.forName("utf-8");
381 
openFile(Class<?> class1, String file)382         public static BufferedReader openFile(Class<?> class1, String file) {
383             return openFile(class1, file, UTF8);
384         }
385 
openFile(Class<?> class1, String file, Charset charset)386         public static BufferedReader openFile(Class<?> class1, String file, Charset charset) {
387             // URL path = null;
388             // String externalForm = null;
389             try {
390                 final InputStream resourceAsStream = class1.getResourceAsStream(file);
391                 if (charset == null) {
392                     charset = UTF8;
393                 }
394                 InputStreamReader reader = new InputStreamReader(resourceAsStream, charset);
395                 BufferedReader bufferedReader = new BufferedReader(reader, 1024 * 64);
396                 return bufferedReader;
397             } catch (Exception e) {
398                 String className = class1 == null ? null : class1.getCanonicalName();
399                 String canonicalName = null;
400                 try {
401                     String relativeFileName = getRelativeFileName(class1, "../util/");
402                     canonicalName = new File(relativeFileName).getCanonicalPath();
403                 } catch (Exception e1) {
404                     throw new ICUUncheckedIOException("Couldn't open file: " + file + "; relative to class: "
405                             + className, e);
406                 }
407                 throw new ICUUncheckedIOException("Couldn't open file " + file + "; in path " + canonicalName + "; relative to class: "
408                         + className, e);
409             }
410         }
getRelativeFileName(Class<?> class1, String filename)411         public static String getRelativeFileName(Class<?> class1, String filename) {
412             URL resource = class1 == null ?
413                     FileUtilities.class.getResource(filename) : class1.getResource(filename);
414                     String resourceString = resource.toString();
415                     if (resourceString.startsWith("file:")) {
416                         return resourceString.substring(5);
417                     } else if (resourceString.startsWith("jar:file:")) {
418                         return resourceString.substring(9);
419                     } else {
420                         throw new ICUUncheckedIOException("File not found: " + resourceString);
421                     }
422         }
423     }
424 
425     /**
426      * @hide exposed on OHOS
427      */
428     static public class RegexUtilities {
findMismatch(Matcher m, CharSequence s)429         public static int findMismatch(Matcher m, CharSequence s) {
430             int i;
431             for (i = 1; i < s.length(); ++i) {
432                 boolean matches = m.reset(s.subSequence(0, i)).matches();
433                 if (!matches && !m.hitEnd()) {
434                     break;
435                 }
436             }
437             return i - 1;
438         }
showMismatch(Matcher m, CharSequence s)439         public static String showMismatch(Matcher m, CharSequence s) {
440             int failPoint = findMismatch(m, s);
441             String show = s.subSequence(0, failPoint) + "☹" + s.subSequence(failPoint, s.length());
442             return show;
443         }
444     }
445 
446     /**
447      * @hide exposed on OHOS
448      */
449     public interface Predicate<T> {
450         /**
451          * Evaluates this predicate on the given argument.
452          *
453          * @param t the input argument
454          * @return {@code true} if the input argument matches the predicate,
455          * otherwise {@code false}
456          */
test(T t)457         boolean test(T t);
458     }
459 }