1 package org.unicode.cldr.util; 2 3 import java.util.Collection; 4 import java.util.Collections; 5 import java.util.EnumSet; 6 import java.util.LinkedHashSet; 7 import java.util.List; 8 9 import com.google.common.base.Splitter; 10 import com.google.common.collect.ImmutableList; 11 import com.ibm.icu.dev.util.CollectionUtilities; 12 13 public final class PreferredAndAllowedHour implements Comparable<PreferredAndAllowedHour> { 14 15 public enum HourStyle { 16 H, Hb(H), HB(H), k, h, hb(h), hB(h), K; 17 public final HourStyle base; 18 HourStyle()19 HourStyle() { 20 base = this; 21 } 22 HourStyle(HourStyle base)23 HourStyle(HourStyle base) { 24 this.base = base; 25 } 26 isHourCharacter(String c)27 public static boolean isHourCharacter(String c) { 28 try { 29 HourStyle.valueOf(c); 30 return true; 31 } catch (Exception e) { 32 return false; 33 } 34 } 35 } 36 37 public final HourStyle preferred; 38 public final List<HourStyle> allowed; 39 PreferredAndAllowedHour(char preferred, Collection<Character> allowed)40 public PreferredAndAllowedHour(char preferred, Collection<Character> allowed) { 41 this(HourStyle.valueOf(String.valueOf(preferred)), mungeSet(allowed)); 42 } 43 PreferredAndAllowedHour(Collection<HourStyle> allowed)44 public PreferredAndAllowedHour(Collection<HourStyle> allowed) { 45 this(allowed.iterator().next(), allowed); 46 } 47 PreferredAndAllowedHour(HourStyle preferred, Collection<HourStyle> allowed)48 public PreferredAndAllowedHour(HourStyle preferred, Collection<HourStyle> allowed) { 49 if (preferred == null) { 50 throw new NullPointerException(); 51 } 52 if (!allowed.contains(preferred)) { 53 throw new IllegalArgumentException("Allowed (" + allowed + 54 ") must contain preferred(" + preferred + 55 ")"); 56 } 57 this.preferred = preferred; 58 this.allowed = ImmutableList.copyOf(new LinkedHashSet<>(allowed)); 59 } 60 PreferredAndAllowedHour(String preferred2, String allowedString)61 public PreferredAndAllowedHour(String preferred2, String allowedString) { 62 this(HourStyle.valueOf(preferred2), mungeOperands(allowedString)); 63 } 64 mungeSet(Collection<Character> allowed)65 private static EnumSet<HourStyle> mungeSet(Collection<Character> allowed) { 66 EnumSet<HourStyle> temp = EnumSet.noneOf(HourStyle.class); 67 for (char c : allowed) { 68 temp.add(HourStyle.valueOf(String.valueOf(c))); 69 } 70 return temp; 71 } 72 73 static final Splitter SPACE_SPLITTER = Splitter.on(' ').trimResults(); 74 mungeOperands(String allowedString)75 private static LinkedHashSet<HourStyle> mungeOperands(String allowedString) { 76 LinkedHashSet<HourStyle> allowed = new LinkedHashSet<>(); 77 for (String s : SPACE_SPLITTER.split(allowedString)) { 78 allowed.add(HourStyle.valueOf(s)); 79 } 80 return allowed; 81 } 82 83 @Override compareTo(PreferredAndAllowedHour arg0)84 public int compareTo(PreferredAndAllowedHour arg0) { 85 int diff = preferred.compareTo(arg0.preferred); 86 if (diff != 0) return diff; 87 return CollectionUtilities.compare(allowed.iterator(), arg0.allowed.iterator()); 88 } 89 90 @Override toString()91 public String toString() { 92 return toString(Collections.singleton("?")); 93 } 94 toString(Collection<String> regions)95 public String toString(Collection<String> regions) { 96 return "<hours preferred=\"" 97 + preferred 98 + "\" allowed=\"" 99 + CollectionUtilities.join(allowed, " ") 100 + "\" regions=\"" 101 + CollectionUtilities.join(regions, " ") 102 + "\"/>"; 103 } 104 105 @Override equals(Object obj)106 public boolean equals(Object obj) { 107 return obj instanceof PreferredAndAllowedHour && compareTo((PreferredAndAllowedHour) obj) == 0; 108 } 109 }