| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.i18n.phonenumbers.metadata.table; |
| 17 |
|
| 18 |
import static com.google.common.base.Preconditions.checkArgument; |
| 19 |
import static com.google.common.base.Preconditions.checkNotNull; |
| 20 |
import static com.google.common.base.Preconditions.checkState; |
| 21 |
import static com.google.common.collect.ImmutableList.toImmutableList; |
| 22 |
import static com.google.i18n.phonenumbers.metadata.RangeSpecification.ALL_DIGITS_MASK; |
| 23 |
import static java.lang.Integer.numberOfTrailingZeros; |
| 24 |
import static java.util.Comparator.comparing; |
| 25 |
|
| 26 |
import com.google.auto.value.AutoValue; |
| 27 |
import com.google.common.collect.ImmutableList; |
| 28 |
import com.google.common.collect.ImmutableSortedSet; |
| 29 |
import com.google.i18n.phonenumbers.metadata.DigitSequence; |
| 30 |
import com.google.i18n.phonenumbers.metadata.RangeSpecification; |
| 31 |
import com.google.i18n.phonenumbers.metadata.RangeTree; |
| 32 |
import com.google.i18n.phonenumbers.metadata.RangeTree.DfaEdge; |
| 33 |
import com.google.i18n.phonenumbers.metadata.RangeTree.DfaNode; |
| 34 |
import com.google.i18n.phonenumbers.metadata.RangeTree.DfaVisitor; |
| 35 |
import com.google.i18n.phonenumbers.metadata.RangeTreeFactorizer; |
| 36 |
import com.google.i18n.phonenumbers.metadata.RangeTreeFactorizer.MergeStrategy; |
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.Comparator; |
| 39 |
import java.util.List; |
| 40 |
import java.util.NavigableSet; |
| 41 |
import java.util.Set; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@AutoValue |
| 49 |
public abstract class RangeKey { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final Comparator<RangeKey> ORDERING = |
| 56 |
comparing(RangeKey::getPrefix, comparing(s -> s.min().toString())) |
| 57 |
.thenComparing(RangeKey::getLengths, comparing(NavigableSet::first)); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static RangeKey create(RangeSpecification prefix, Set<Integer> lengths) { |
| 64 |
checkArgument(prefix.length() == 0 || prefix.getBitmask(prefix.length() - 1) != ALL_DIGITS_MASK, |
| 65 |
"prefix cannot end with an 'any' edge: %s", prefix); |
| 66 |
ImmutableSortedSet<Integer> sorted = ImmutableSortedSet.copyOf(lengths); |
| 67 |
checkArgument(sorted.first() >= prefix.length(), |
| 68 |
"lengths cannot be shorter than the prefix: %s - %s", prefix, lengths); |
| 69 |
return new AutoValue_RangeKey(prefix, sorted); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static ImmutableList<RangeKey> decompose(RangeTree tree) { |
| 79 |
List<RangeKey> keys = new ArrayList<>(); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
for (RangeTree f : RangeTreeFactorizer.factor(tree, MergeStrategy.ALLOW_EDGE_SPLITTING)) { |
| 90 |
KeyVisitor.visit(f, keys); |
| 91 |
} |
| 92 |
return ImmutableList.sortedCopyOf(ORDERING, keys); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private static class KeyVisitor implements DfaVisitor { |
| 102 |
private static void visit(RangeTree tree, List<RangeKey> keys) { |
| 103 |
KeyVisitor v = new KeyVisitor(keys); |
| 104 |
tree.accept(v); |
| 105 |
|
| 106 |
int lengthMask = v.lengthMask; |
| 107 |
|
| 108 |
|
| 109 |
if (tree.getInitial().canTerminate()) { |
| 110 |
lengthMask |= 1; |
| 111 |
} |
| 112 |
if (lengthMask != 0) { |
| 113 |
|
| 114 |
keys.add(new AutoValue_RangeKey(RangeSpecification.empty(), buildLengths(lengthMask))); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
private final List<RangeKey> keys; |
| 120 |
|
| 121 |
private RangeSpecification path = RangeSpecification.empty(); |
| 122 |
|
| 123 |
|
| 124 |
private int lengthMask = 0; |
| 125 |
|
| 126 |
private KeyVisitor(List<RangeKey> keys) { |
| 127 |
this.keys = checkNotNull(keys); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void visit(DfaNode source, DfaEdge edge, DfaNode target) { |
| 132 |
checkState(lengthMask == 0, |
| 133 |
"during downward tree traversal, length mask should be zero (was %s)", lengthMask); |
| 134 |
RangeSpecification oldPath = path; |
| 135 |
path = path.extendByMask(edge.getDigitMask()); |
| 136 |
if (target.equals(RangeTree.getTerminal())) { |
| 137 |
lengthMask = (1 << path.length()); |
| 138 |
|
| 139 |
maybeEmitKey(); |
| 140 |
} else { |
| 141 |
target.accept(this); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
if (target.canTerminate()) { |
| 146 |
lengthMask |= (1 << path.length()); |
| 147 |
} |
| 148 |
maybeEmitKey(); |
| 149 |
} |
| 150 |
path = oldPath; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private void maybeEmitKey() { |
| 157 |
if (lengthMask != 0 && path.getBitmask(path.length() - 1) != ALL_DIGITS_MASK) { |
| 158 |
keys.add(new AutoValue_RangeKey(path, buildLengths(lengthMask))); |
| 159 |
lengthMask = 0; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public abstract RangeSpecification getPrefix(); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public abstract ImmutableSortedSet<Integer> getLengths(); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public final ImmutableList<RangeSpecification> asRangeSpecifications() { |
| 182 |
RangeSpecification s = getPrefix(); |
| 183 |
return getLengths().stream() |
| 184 |
.map(n -> s.extendByLength(n - s.length())) |
| 185 |
.collect(toImmutableList()); |
| 186 |
} |
| 187 |
|
| 188 |
public final RangeTree asRangeTree() { |
| 189 |
RangeSpecification s = getPrefix(); |
| 190 |
return RangeTree.from(getLengths().stream().map(n -> s.extendByLength(n - s.length()))); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public boolean contains(DigitSequence prefix, Integer length) { |
| 197 |
return asRangeSpecifications().stream() |
| 198 |
.anyMatch( |
| 199 |
specification -> |
| 200 |
specification.matches( |
| 201 |
prefix.extendBy(DigitSequence.zeros(length - prefix.length())))); |
| 202 |
} |
| 203 |
|
| 204 |
private static ImmutableSortedSet<Integer> buildLengths(int lengthMask) { |
| 205 |
checkArgument(lengthMask != 0); |
| 206 |
ImmutableSortedSet.Builder<Integer> lengths = ImmutableSortedSet.naturalOrder(); |
| 207 |
do { |
| 208 |
int length = numberOfTrailingZeros(lengthMask); |
| 209 |
lengths.add(length); |
| 210 |
|
| 211 |
lengthMask &= ~(1 << length); |
| 212 |
} while (lengthMask != 0); |
| 213 |
return lengths.build(); |
| 214 |
} |
| 215 |
} |