• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 package org.unicode.icu.tool.cldrtoicu;
4 
5 import static com.google.common.base.Preconditions.checkArgument;
6 import static com.google.common.collect.ImmutableList.toImmutableList;
7 
8 import java.util.Objects;
9 import java.util.stream.Stream;
10 
11 import com.google.common.collect.ImmutableList;
12 
13 /**
14  * A resource bundle value containing a sequence of elements. This is a very thin wrapper over an
15  * immutable list, with a few additional constraints (e.g. cannot be empty).
16  *
17  * <p>Immutable and thread safe.
18  */
19 public final class RbValue {
20     private final ImmutableList<String> elements;
21     private final int elementsPerLine;
22 
23     /** Returns a resource bundle value of the given elements. */
of(String... elements)24     public static RbValue of(String... elements) {
25         return new RbValue(ImmutableList.copyOf(elements), 1);
26     }
27 
28     /** Returns a resource bundle value of the given elements. */
of(Iterable<String> elements)29     public static RbValue of(Iterable<String> elements) {
30         return new RbValue(ImmutableList.copyOf(elements), 1);
31     }
32 
33     /** Returns a resource bundle value of the given elements by consuming the given stream. */
of(Stream<String> elements)34     public static RbValue of(Stream<String> elements) {
35         return new RbValue(elements.collect(toImmutableList()), 1);
36     }
37 
RbValue(ImmutableList<String> elements, int elementsPerLine)38     private RbValue(ImmutableList<String> elements, int elementsPerLine) {
39         checkArgument(!elements.isEmpty(), "Resource bundle values cannot be empty");
40         checkArgument(elementsPerLine > 0, "invalid elements per line: %s", elementsPerLine);
41         this.elements = elements;
42         this.elementsPerLine = elementsPerLine;
43     }
44 
elementsPerLine(int n)45     public RbValue elementsPerLine(int n) {
46         return new RbValue(elements, n);
47     }
48 
49     /** Returns the non-empty list of value elements. */
getElements()50     public ImmutableList<String> getElements() {
51         return elements;
52     }
53 
54     /**
55      * Returns whether this is a single element value. Singleton values are treated different when
56      * writing out ICU data files.
57      */
isSingleton()58     boolean isSingleton() {
59         return elements.size() == 1;
60     }
61 
getElementsPerLine()62     int getElementsPerLine() {
63         return elementsPerLine;
64     }
65 
hashCode()66     @Override public int hashCode() {
67         return Objects.hashCode(elements);
68     }
69 
equals(Object obj)70     @Override public boolean equals(Object obj) {
71         return obj instanceof RbValue && elements.equals(((RbValue) obj).elements);
72     }
73 
toString()74     @Override public String toString() {
75         return elements.toString();
76     }
77 }
78