Codebase list guava-libraries / eacfdab
New upstream version 23.3 Emmanuel Bourg 4 years ago
62 changed file(s) with 1992 addition(s) and 461 deletion(s). Raw diff Collapse all Expand all
1717
1818 ## Latest release
1919
20 The most recent release is [Guava 23.1][current release], released September 27,
20 The most recent release is [Guava 23.2][current release], released October 11,
2121 2017.
2222
2323 The Maven group ID is `com.google.guava`, and the artifact ID is `guava`. Use
24 version `23.1-jre` for the JRE flavor, or `23.1-android` for the Android flavor.
24 version `23.2-jre` for the JRE flavor, or `23.2-android` for the Android flavor.
2525
2626 To add a dependency on Guava using Maven, use the following:
2727
2929 <dependency>
3030 <groupId>com.google.guava</groupId>
3131 <artifactId>guava</artifactId>
32 <version>23.1-jre</version>
32 <version>23.2-jre</version>
3333 <!-- or, for Android: -->
34 <version>23.1-android</version>
34 <version>23.2-android</version>
3535 </dependency>
3636 ```
3737
3939
4040 ```
4141 dependencies {
42 compile 'com.google.guava:guava:23.1-jre'
42 compile 'com.google.guava:guava:23.2-jre'
4343 // or, for Android:
44 compile 'com.google.guava:guava:23.1-android'
44 compile 'com.google.guava:guava:23.2-android'
4545 }
4646 ```
4747
9494 correctly in other environments. For the Android flavor, our unit tests run on
9595 API level 15 (Ice Cream Sandwich).
9696
97 [current release]: https://github.com/google/guava/releases/tag/v23.1
97 [current release]: https://github.com/google/guava/releases/tag/v23.2
9898 [guava-snapshot-api-docs]: http://google.github.io/guava/releases/snapshot-jre/api/docs/
9999 [guava-snapshot-api-diffs]: http://google.github.io/guava/releases/snapshot-jre/api/diffs/
100100 [Guava Explained]: https://github.com/google/guava/wiki/Home
44 <parent>
55 <groupId>com.google.guava</groupId>
66 <artifactId>guava-parent</artifactId>
7 <version>23.2-android</version>
7 <version>23.3-android</version>
88 </parent>
99 <artifactId>guava</artifactId>
1010 <packaging>bundle</packaging>
14971497
14981498 /** Implementation of {@link #digit()}. */
14991499 private static final class Digit extends RangesMatcher {
1500 // Plug the following UnicodeSet pattern into
1501 // https://unicode.org/cldr/utility/list-unicodeset.jsp
1502 // [[:Nd:]&[:nv=0:]&[\u0000-\uFFFF]]
1503 // and get the zeroes from there.
15001504
15011505 // Must be in ascending order.
15021506 private static final String ZEROES =
1503 "0\u0660\u06f0\u07c0\u0966\u09e6\u0a66\u0ae6\u0b66"
1504 + "\u0be6\u0c66\u0ce6\u0d66\u0e50\u0ed0\u0f20\u1040\u1090\u17e0\u1810"
1505 + "\u1946\u19d0\u1b50\u1bb0\u1c40\u1c50\ua620\ua8d0\ua900\uaa50\uff10";
1507 "0\u0660\u06f0\u07c0\u0966\u09e6\u0a66\u0ae6\u0b66\u0be6\u0c66\u0ce6\u0d66\u0de6"
1508 + "\u0e50\u0ed0\u0f20\u1040\u1090\u17e0\u1810\u1946\u19d0\u1a80\u1a90\u1b50\u1bb0"
1509 + "\u1c40\u1c50\ua620\ua8d0\ua900\ua9d0\ua9f0\uaa50\uabf0\uff10";
15061510
15071511 private static char[] zeroes() {
15081512 return ZEROES.toCharArray();
16201624
16211625 /** Implementation of {@link #invisible()}. */
16221626 private static final class Invisible extends RangesMatcher {
1623
1627 // Plug the following UnicodeSet pattern into
1628 // https://unicode.org/cldr/utility/list-unicodeset.jsp
1629 // [[[:Zs:][:Zl:][:Zp:][:Cc:][:Cf:][:Cs:][:Co:]]&[\u0000-\uFFFF]]
1630 // with the "Abbreviate" option, and get the ranges from there.
16241631 private static final String RANGE_STARTS =
1625 "\u0000\u007f\u00ad\u0600\u061c\u06dd\u070f\u1680\u180e\u2000\u2028\u205f\u2066\u2067"
1626 + "\u2068\u2069\u206a\u3000\ud800\ufeff\ufff9\ufffa";
1627 private static final String RANGE_ENDS =
1628 "\u0020\u00a0\u00ad\u0604\u061c\u06dd\u070f\u1680\u180e\u200f\u202f\u2064\u2066\u2067"
1629 + "\u2068\u2069\u206f\u3000\uf8ff\ufeff\ufff9\ufffb";
1632 "\u0000\u007f\u00ad\u0600\u061c\u06dd\u070f\u08e2\u1680\u180e\u2000\u2028\u205f\u2066"
1633 + "\u3000\ud800\ufeff\ufff9";
1634 private static final String RANGE_ENDS = // inclusive ends
1635 "\u0020\u00a0\u00ad\u0605\u061c\u06dd\u070f\u08e2\u1680\u180e\u200f\u202f\u2064\u206f"
1636 + "\u3000\uf8ff\ufeff\ufffb";
16301637
16311638 static final Invisible INSTANCE = new Invisible();
16321639
3131 *
3232 * @author Louis Wasserman
3333 * @since 15.0
34 * @deprecated Use {@link com.google.common.graph.Traverser} instead. All instance methods except
35 * for {@link #inOrderTraversal} have their equivalent on the result of {@code
36 * Traverser.forTree(tree)} where {@code tree} implements {@code SuccessorsFunction}, which has
37 * a similar API as {@link #children}.
38 * <p>This class is scheduled to be removed in January 2018.
3439 */
40 @Deprecated
3541 @Beta
3642 @GwtCompatible
3743 public abstract class BinaryTreeTraverser<T> extends TreeTraverser<T> {
6262 *
6363 * @author Louis Wasserman
6464 * @since 15.0
65 * @deprecated Use {@link com.google.common.graph.Traverser} instead. All instance methods have
66 * their equivalent on the result of {@code Traverser.forTree(tree)} where {@code tree}
67 * implements {@code SuccessorsFunction}, which has a similar API as {@link #children} or can be
68 * the same lambda function as passed into {@link #using(Function)}.
69 * <p>This class is scheduled to be removed in January 2018.
6570 */
71 @Deprecated
6672 @Beta
6773 @GwtCompatible
6874 public abstract class TreeTraverser<T> {
7480 * {@code TreeTraverser} and implement its {@link #children} method directly.
7581 *
7682 * @since 20.0
77 */
83 * @deprecated Use {@link com.google.common.graph.Traverser#forTree} instead. If you are using a
84 * lambda, these methods have exactly the same signature.
85 */
86 @Deprecated
7887 public static <T> TreeTraverser<T> using(
7988 final Function<T, ? extends Iterable<T>> nodeToChildrenFunction) {
8089 checkNotNull(nodeToChildrenFunction);
92101 public abstract Iterable<T> children(T root);
93102
94103 /**
95 * Returns an unmodifiable iterable over the nodes in a tree structure, using pre-order
96 * traversal. That is, each node's subtrees are traversed after the node itself is returned.
97 *
98 * <p>No guarantees are made about the behavior of the traversal when nodes change while
99 * iteration is in progress or when the iterators generated by {@link #children} are advanced.
100 */
104 * Returns an unmodifiable iterable over the nodes in a tree structure, using pre-order traversal.
105 * That is, each node's subtrees are traversed after the node itself is returned.
106 *
107 * <p>No guarantees are made about the behavior of the traversal when nodes change while iteration
108 * is in progress or when the iterators generated by {@link #children} are advanced.
109 *
110 * @deprecated Use {@link com.google.common.graph.Traverser#depthFirstPreOrder} instead, which has
111 * the same behavior.
112 */
113 @Deprecated
101114 public final FluentIterable<T> preOrderTraversal(final T root) {
102115 checkNotNull(root);
103116 return new FluentIterable<T>() {
145158 * Returns an unmodifiable iterable over the nodes in a tree structure, using post-order
146159 * traversal. That is, each node's subtrees are traversed before the node itself is returned.
147160 *
148 * <p>No guarantees are made about the behavior of the traversal when nodes change while
149 * iteration is in progress or when the iterators generated by {@link #children} are advanced.
150 */
161 * <p>No guarantees are made about the behavior of the traversal when nodes change while iteration
162 * is in progress or when the iterators generated by {@link #children} are advanced.
163 *
164 * @deprecated Use {@link com.google.common.graph.Traverser#depthFirstPostOrder} instead, which
165 * has the same behavior.
166 */
167 @Deprecated
151168 public final FluentIterable<T> postOrderTraversal(final T root) {
152169 checkNotNull(root);
153170 return new FluentIterable<T>() {
205222 * Returns an unmodifiable iterable over the nodes in a tree structure, using breadth-first
206223 * traversal. That is, all the nodes of depth 0 are returned, then depth 1, then 2, and so on.
207224 *
208 * <p>No guarantees are made about the behavior of the traversal when nodes change while
209 * iteration is in progress or when the iterators generated by {@link #children} are advanced.
210 */
225 * <p>No guarantees are made about the behavior of the traversal when nodes change while iteration
226 * is in progress or when the iterators generated by {@link #children} are advanced.
227 *
228 * @deprecated Use {@link com.google.common.graph.Traverser#breadthFirst} instead, which has the
229 * same behavior.
230 */
231 @Deprecated
211232 public final FluentIterable<T> breadthFirstTraversal(final T root) {
212233 checkNotNull(root);
213234 return new FluentIterable<T>() {
167167 *
168168 * <p>See <a href="https://en.wikipedia.org/wiki/Breadth-first_search">Wikipedia</a> for more
169169 * info.
170 *
171 * @throws IllegalArgumentException if {@code startNode} is not an element of the graph
170172 */
171173 public abstract Iterable<N> breadthFirst(N startNode);
172174
198200 * }</pre>
199201 *
200202 * <p>See <a href="https://en.wikipedia.org/wiki/Depth-first_search">Wikipedia</a> for more info.
203 *
204 * @throws IllegalArgumentException if {@code startNode} is not an element of the graph
201205 */
202206 public abstract Iterable<N> depthFirstPreOrder(N startNode);
203207
229233 * }</pre>
230234 *
231235 * <p>See <a href="https://en.wikipedia.org/wiki/Depth-first_search">Wikipedia</a> for more info.
236 *
237 * @throws IllegalArgumentException if {@code startNode} is not an element of the graph
232238 */
233239 public abstract Iterable<N> depthFirstPostOrder(N startNode);
234240
241247
242248 @Override
243249 public Iterable<N> breadthFirst(final N startNode) {
250 checkNotNull(startNode);
251 checkThatNodeIsInGraph(startNode);
244252 return new Iterable<N>() {
245253 @Override
246254 public Iterator<N> iterator() {
251259
252260 @Override
253261 public Iterable<N> depthFirstPreOrder(final N startNode) {
262 checkNotNull(startNode);
263 checkThatNodeIsInGraph(startNode);
254264 return new Iterable<N>() {
255265 @Override
256266 public Iterator<N> iterator() {
261271
262272 @Override
263273 public Iterable<N> depthFirstPostOrder(final N startNode) {
274 checkNotNull(startNode);
275 checkThatNodeIsInGraph(startNode);
264276 return new Iterable<N>() {
265277 @Override
266278 public Iterator<N> iterator() {
267279 return new DepthFirstIterator(startNode, Order.POSTORDER);
268280 }
269281 };
282 }
283
284 @SuppressWarnings("CheckReturnValue")
285 private void checkThatNodeIsInGraph(N startNode) {
286 // successors() throws an IllegalArgumentException for nodes that are not an element of the
287 // graph.
288 graph.successors(startNode);
270289 }
271290
272291 private final class BreadthFirstIterator extends UnmodifiableIterator<N> {
359378
360379 @Override
361380 public Iterable<N> breadthFirst(final N startNode) {
381 checkNotNull(startNode);
382 checkThatNodeIsInTree(startNode);
362383 return new Iterable<N>() {
363384 @Override
364385 public Iterator<N> iterator() {
369390
370391 @Override
371392 public Iterable<N> depthFirstPreOrder(final N startNode) {
393 checkNotNull(startNode);
394 checkThatNodeIsInTree(startNode);
372395 return new Iterable<N>() {
373396 @Override
374397 public Iterator<N> iterator() {
379402
380403 @Override
381404 public Iterable<N> depthFirstPostOrder(final N startNode) {
405 checkNotNull(startNode);
406 checkThatNodeIsInTree(startNode);
382407 return new Iterable<N>() {
383408 @Override
384409 public Iterator<N> iterator() {
385410 return new DepthFirstPostOrderIterator(startNode);
386411 }
387412 };
413 }
414
415 @SuppressWarnings("CheckReturnValue")
416 private void checkThatNodeIsInTree(N startNode) {
417 // successors() throws an IllegalArgumentException for nodes that are not an element of the
418 // graph.
419 tree.successors(startNode);
388420 }
389421
390422 private final class BreadthFirstIterator extends UnmodifiableIterator<N> {
2222 import com.google.common.base.Ascii;
2323 import com.google.common.base.CharMatcher;
2424 import com.google.common.base.Joiner;
25 import com.google.common.base.Optional;
2526 import com.google.common.base.Splitter;
2627 import com.google.common.collect.ImmutableList;
2728 import com.google.thirdparty.publicsuffix.PublicSuffixPatterns;
29 import com.google.thirdparty.publicsuffix.PublicSuffixType;
2830 import java.util.List;
2931 import javax.annotation.Nullable;
3032
3133 /**
32 * An immutable well-formed internet domain name, such as {@code com} or {@code
33 * foo.co.uk}. Only syntactic analysis is performed; no DNS lookups or other network interactions
34 * take place. Thus there is no guarantee that the domain actually exists on the internet.
34 * An immutable well-formed internet domain name, such as {@code com} or {@code foo.co.uk}. Only
35 * syntactic analysis is performed; no DNS lookups or other network interactions take place. Thus
36 * there is no guarantee that the domain actually exists on the internet.
3537 *
3638 * <p>One common use of this class is to determine whether a given string is likely to represent an
3739 * addressable domain on the web -- that is, for a candidate string {@code "xxx"}, might browsing to
3941 * done by determining whether the domain ended with a {@linkplain #isPublicSuffix() public suffix}
4042 * but was not itself a public suffix. However, this test is no longer accurate. There are many
4143 * domains which are both public suffixes and addressable as hosts; {@code "uk.com"} is one example.
42 * As a result, the only useful test to determine if a domain is a plausible web host is
43 * {@link #hasPublicSuffix()}. This will return {@code true} for many domains which (currently) are
44 * not hosts, such as {@code "com"}, but given that any public suffix may become a host without
45 * warning, it is better to err on the side of permissiveness and thus avoid spurious rejection of
46 * valid sites.
44 * Using the subset of public suffixes that are {@linkplain #isRegistrySuffix() registry suffixes},
45 * one can get a better result, as only a few registry suffixes are addressable. However, the most
46 * useful test to determine if a domain is a plausible web host is {@link #hasPublicSuffix()}. This
47 * will return {@code true} for many domains which (currently) are not hosts, such as {@code "com"},
48 * but given that any public suffix may become a host without warning, it is better to err on the
49 * side of permissiveness and thus avoid spurious rejection of valid sites. Of course, to actually
50 * determine addressability of any host, clients of this class will need to perform their own DNS
51 * lookups.
4752 *
4853 * <p>During construction, names are normalized in two ways:
4954 *
5055 * <ol>
51 * <li>ASCII uppercase characters are converted to lowercase.
52 * <li>Unicode dot separators other than the ASCII period ({@code '.'}) are converted to the ASCII
53 * period.
56 * <li>ASCII uppercase characters are converted to lowercase.
57 * <li>Unicode dot separators other than the ASCII period ({@code '.'}) are converted to the ASCII
58 * period.
5459 * </ol>
5560 *
5661 * <p>The normalized values will be returned from {@link #toString()} and {@link #parts()}, and will
5762 * be reflected in the result of {@link #equals(Object)}.
5863 *
5964 * <p><a href="http://en.wikipedia.org/wiki/Internationalized_domain_name">Internationalized domain
60 * names</a> such as {@code 网络.cn} are supported, as are the equivalent
61 * <a href="http://en.wikipedia.org/wiki/Internationalized_domain_name">IDNA Punycode-encoded</a>
65 * names</a> such as {@code 网络.cn} are supported, as are the equivalent <a
66 * href="http://en.wikipedia.org/wiki/Internationalized_domain_name">IDNA Punycode-encoded</a>
6267 * versions.
6368 *
6469 * @author Craig Berry
7378 private static final Joiner DOT_JOINER = Joiner.on('.');
7479
7580 /**
76 * Value of {@link #publicSuffixIndex} which indicates that no public suffix was found.
77 */
78 private static final int NO_PUBLIC_SUFFIX_FOUND = -1;
81 * Value of {@link #publicSuffixIndex} or {@link #registrySuffixIndex} which indicates that no
82 * relevant suffix was found.
83 */
84 private static final int NO_SUFFIX_FOUND = -1;
7985
8086 private static final String DOT_REGEX = "\\.";
8187
111117
112118 /**
113119 * The index in the {@link #parts()} list at which the public suffix begins. For example, for the
114 * domain name {@code www.google.co.uk}, the value would be 2 (the index of the {@code co} part).
115 * The value is negative (specifically, {@link #NO_PUBLIC_SUFFIX_FOUND}) if no public suffix was
116 * found.
120 * domain name {@code myblog.blogspot.co.uk}, the value would be 1 (the index of the {@code
121 * blogspot} part). The value is negative (specifically, {@link #NO_SUFFIX_FOUND}) if no public
122 * suffix was found.
117123 */
118124 private final int publicSuffixIndex;
125
126 /**
127 * The index in the {@link #parts()} list at which the registry suffix begins. For example, for
128 * the domain name {@code myblog.blogspot.co.uk}, the value would be 2 (the index of the {@code
129 * co} part). The value is negative (specifically, {@link #NO_SUFFIX_FOUND}) if no registry suffix
130 * was found.
131 */
132 private final int registrySuffixIndex;
119133
120134 /**
121135 * Constructor used to implement {@link #from(String)}, and from subclasses.
139153 checkArgument(parts.size() <= MAX_PARTS, "Domain has too many parts: '%s'", name);
140154 checkArgument(validateSyntax(parts), "Not a valid domain name: '%s'", name);
141155
142 this.publicSuffixIndex = findPublicSuffix();
143 }
144
145 /**
146 * Returns the index of the leftmost part of the public suffix, or -1 if not found. Note that the
147 * value defined as the "public suffix" may not be a public suffix according to
148 * {@link #isPublicSuffix()} if the domain ends with an excluded domain pattern such as
149 * {@code "nhs.uk"}.
150 */
151 private int findPublicSuffix() {
156 this.publicSuffixIndex = findSuffixOfType(Optional.<PublicSuffixType>absent());
157 this.registrySuffixIndex = findSuffixOfType(Optional.of(PublicSuffixType.REGISTRY));
158 }
159
160 /**
161 * Returns the index of the leftmost part of the suffix, or -1 if not found. Note that the value
162 * defined as a suffix may not produce {@code true} results from {@link #isPublicSuffix()} or
163 * {@link #isRegistrySuffix()} if the domain ends with an excluded domain pattern such as {@code
164 * "nhs.uk"}.
165 *
166 * <p>If a {@code desiredType} is specified, this method only finds suffixes of the given type.
167 * Otherwise, it finds the first suffix of any type.
168 */
169 private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
152170 final int partsSize = parts.size();
153171
154172 for (int i = 0; i < partsSize; i++) {
155173 String ancestorName = DOT_JOINER.join(parts.subList(i, partsSize));
156174
157 if (PublicSuffixPatterns.EXACT.containsKey(ancestorName)) {
175 if (matchesType(
176 desiredType, Optional.fromNullable(PublicSuffixPatterns.EXACT.get(ancestorName)))) {
158177 return i;
159178 }
160179
165184 return i + 1;
166185 }
167186
168 if (matchesWildcardPublicSuffix(ancestorName)) {
187 if (matchesWildcardSuffixType(desiredType, ancestorName)) {
169188 return i;
170189 }
171190 }
172191
173 return NO_PUBLIC_SUFFIX_FOUND;
192 return NO_SUFFIX_FOUND;
174193 }
175194
176195 /**
177196 * Returns an instance of {@link InternetDomainName} after lenient validation. Specifically,
178197 * validation against <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>
179 * ("Internationalizing Domain Names in Applications") is skipped, while validation against
180 * <a href="http://www.ietf.org/rfc/rfc1035.txt">RFC 1035</a> is relaxed in the following ways:
198 * ("Internationalizing Domain Names in Applications") is skipped, while validation against <a
199 * href="http://www.ietf.org/rfc/rfc1035.txt">RFC 1035</a> is relaxed in the following ways:
200 *
181201 * <ul>
182 * <li>Any part containing non-ASCII characters is considered valid.
183 * <li>Underscores ('_') are permitted wherever dashes ('-') are permitted.
184 * <li>Parts other than the final part may start with a digit, as mandated by
185 * <a href="https://tools.ietf.org/html/rfc1123#section-2">RFC 1123</a>.
202 * <li>Any part containing non-ASCII characters is considered valid.
203 * <li>Underscores ('_') are permitted wherever dashes ('-') are permitted.
204 * <li>Parts other than the final part may start with a digit, as mandated by <a
205 * href="https://tools.ietf.org/html/rfc1123#section-2">RFC 1123</a>.
186206 * </ul>
187207 *
188208 *
189209 * @param domain A domain name (not IP address)
190 * @throws IllegalArgumentException if {@code name} is not syntactically valid according to
191 * {@link #isValid}
210 * @throws IllegalArgumentException if {@code name} is not syntactically valid according to {@link
211 * #isValid}
192212 * @since 10.0 (previously named {@code fromLenient})
193213 */
194214 public static InternetDomainName from(String domain) {
291311 /**
292312 * Indicates whether this domain name represents a <i>public suffix</i>, as defined by the Mozilla
293313 * Foundation's <a href="http://publicsuffix.org/">Public Suffix List</a> (PSL). A public suffix
294 * is one under which Internet users can directly register names, such as {@code com},
295 * {@code co.uk} or {@code pvt.k12.wy.us}. Examples of domain names that are <i>not</i> public
296 * suffixes include {@code google}, {@code google.com} and {@code foo.co.uk}.
314 * is one under which Internet users can directly register names, such as {@code com}, {@code
315 * co.uk} or {@code pvt.k12.wy.us}. Examples of domain names that are <i>not</i> public suffixes
316 * include {@code google.com}, {@code foo.co.uk}, and {@code myblog.blogspot.com}.
317 *
318 * <p>Public suffixes are a proper superset of {@linkplain #isRegistrySuffix() registry suffixes}.
319 * The list of public suffixes additionally contains privately owned domain names under which
320 * Internet users can register subdomains. An example of a public suffix that is not a registry
321 * suffix is {@code blogspot.com}. Note that it is true that all public suffixes <i>have</i>
322 * registry suffixes, since domain name registries collectively control all internet domain names.
323 *
324 * <p>For considerations on whether the public suffix or registry suffix designation is more
325 * suitable for your application, see <a
326 * href="https://github.com/google/guava/wiki/InternetDomainNameExplained">this article</a>.
297327 *
298328 * @return {@code true} if this domain name appears exactly on the public suffix list
299329 * @since 6.0
304334
305335 /**
306336 * Indicates whether this domain name ends in a {@linkplain #isPublicSuffix() public suffix},
307 * including if it is a public suffix itself. For example, returns {@code true} for
308 * {@code www.google.com}, {@code foo.co.uk} and {@code com}, but not for {@code google} or
309 * {@code google.foo}. This is the recommended method for determining whether a domain is
310 * potentially an addressable host.
337 * including if it is a public suffix itself. For example, returns {@code true} for {@code
338 * www.google.com}, {@code foo.co.uk} and {@code com}, but not for {@code invalid} or {@code
339 * google.invalid}. This is the recommended method for determining whether a domain is potentially
340 * an addressable host.
341 *
342 * <p>Note that this method is equivalent to {@link #hasRegistrySuffix()} because all registry
343 * suffixes are public suffixes <i>and</i> all public suffixes have registry suffixes.
311344 *
312345 * @since 6.0
313346 */
314347 public boolean hasPublicSuffix() {
315 return publicSuffixIndex != NO_PUBLIC_SUFFIX_FOUND;
348 return publicSuffixIndex != NO_SUFFIX_FOUND;
316349 }
317350
318351 /**
327360
328361 /**
329362 * Indicates whether this domain name ends in a {@linkplain #isPublicSuffix() public suffix},
330 * while not being a public suffix itself. For example, returns {@code true} for
331 * {@code www.google.com}, {@code foo.co.uk} and {@code bar.ca.us}, but not for {@code google},
332 * {@code com}, or {@code
333 * google.foo}.
334 *
335 * <p><b>Warning:</b> a {@code false} result from this method does not imply that the domain does
336 * not represent an addressable host, as many public suffixes are also addressable hosts. Use
337 * {@link #hasPublicSuffix()} for that test.
363 * while not being a public suffix itself. For example, returns {@code true} for {@code
364 * www.google.com}, {@code foo.co.uk} and {@code myblog.blogspot.com}, but not for {@code com},
365 * {@code co.uk}, {@code google.invalid}, or {@code blogspot.com}.
338366 *
339367 * <p>This method can be used to determine whether it will probably be possible to set cookies on
340368 * the domain, though even that depends on individual browsers' implementations of cookie
348376
349377 /**
350378 * Indicates whether this domain name is composed of exactly one subdomain component followed by a
351 * {@linkplain #isPublicSuffix() public suffix}. For example, returns {@code true} for
352 * {@code google.com} and {@code foo.co.uk}, but not for {@code www.google.com} or {@code co.uk}.
353 *
354 * <p><b>Warning:</b> A {@code true} result from this method does not imply that the domain is at
355 * the highest level which is addressable as a host, as many public suffixes are also addressable
356 * hosts. For example, the domain {@code bar.uk.com} has a public suffix of {@code uk.com}, so it
357 * would return {@code true} from this method. But {@code uk.com} is itself an addressable host.
379 * {@linkplain #isPublicSuffix() public suffix}. For example, returns {@code true} for {@code
380 * google.com} {@code foo.co.uk}, and {@code myblog.blogspot.com}, but not for {@code
381 * www.google.com}, {@code co.uk}, or {@code blogspot.com}.
358382 *
359383 * <p>This method can be used to determine whether a domain is probably the highest level for
360384 * which cookies may be set, though even that depends on individual browsers' implementations of
367391 }
368392
369393 /**
370 * Returns the portion of this domain name that is one level beneath the public suffix. For
371 * example, for {@code x.adwords.google.co.uk} it returns {@code google.co.uk}, since
372 * {@code co.uk} is a public suffix.
394 * Returns the portion of this domain name that is one level beneath the {@linkplain
395 * #isPublicSuffix() public suffix}. For example, for {@code x.adwords.google.co.uk} it returns
396 * {@code google.co.uk}, since {@code co.uk} is a public suffix. Similarly, for {@code
397 * myblog.blogspot.com} it returns the same domain, {@code myblog.blogspot.com}, since {@code
398 * blogspot.com} is a public suffix.
373399 *
374400 * <p>If {@link #isTopPrivateDomain()} is true, the current domain name instance is returned.
375 *
376 * <p>This method should not be used to determine the topmost parent domain which is addressable
377 * as a host, as many public suffixes are also addressable hosts. For example, the domain
378 * {@code foo.bar.uk.com} has a public suffix of {@code uk.com}, so it would return
379 * {@code bar.uk.com} from this method. But {@code uk.com} is itself an addressable host.
380401 *
381402 * <p>This method can be used to determine the probable highest level parent domain for which
382403 * cookies may be set, though even that depends on individual browsers' implementations of cookie
391412 }
392413 checkState(isUnderPublicSuffix(), "Not under a public suffix: %s", name);
393414 return ancestor(publicSuffixIndex - 1);
415 }
416
417 /**
418 * Indicates whether this domain name represents a <i>registry suffix</i>, as defined by a subset
419 * of the Mozilla Foundation's <a href="http://publicsuffix.org/">Public Suffix List</a> (PSL). A
420 * registry suffix is one under which Internet users can directly register names via a domain name
421 * registrar, and have such registrations lawfully protected by internet-governing bodies such as
422 * ICANN. Examples of registry suffixes include {@code com}, {@code co.uk}, and {@code
423 * pvt.k12.wy.us}. Examples of domain names that are <i>not</i> registry suffixes include {@code
424 * google.com} and {@code foo.co.uk}.
425 *
426 * <p>Registry suffixes are a proper subset of {@linkplain #isPublicSuffix() public suffixes}. The
427 * list of public suffixes additionally contains privately owned domain names under which Internet
428 * users can register subdomains. An example of a public suffix that is not a registry suffix is
429 * {@code blogspot.com}. Note that it is true that all public suffixes <i>have</i> registry
430 * suffixes, since domain name registries collectively control all internet domain names.
431 *
432 * <p>For considerations on whether the public suffix or registry suffix designation is more
433 * suitable for your application, see <a
434 * href="https://github.com/google/guava/wiki/InternetDomainNameExplained">this article</a>.
435 *
436 * @return {@code true} if this domain name appears exactly on the public suffix list as part of
437 * the registry suffix section (labelled "ICANN").
438 * @since 23.3
439 */
440 public boolean isRegistrySuffix() {
441 return registrySuffixIndex == 0;
442 }
443
444 /**
445 * Indicates whether this domain name ends in a {@linkplain #isRegistrySuffix() registry suffix},
446 * including if it is a registry suffix itself. For example, returns {@code true} for {@code
447 * www.google.com}, {@code foo.co.uk} and {@code com}, but not for {@code invalid} or {@code
448 * google.invalid}.
449 *
450 * <p>Note that this method is equivalent to {@link #hasPublicSuffix()} because all registry
451 * suffixes are public suffixes <i>and</i> all public suffixes have registry suffixes.
452 *
453 * @since 23.3
454 */
455 public boolean hasRegistrySuffix() {
456 return registrySuffixIndex != NO_SUFFIX_FOUND;
457 }
458
459 /**
460 * Returns the {@linkplain #isRegistrySuffix() registry suffix} portion of the domain name, or
461 * {@code null} if no registry suffix is present.
462 *
463 * @since 23.3
464 */
465 public InternetDomainName registrySuffix() {
466 return hasRegistrySuffix() ? ancestor(registrySuffixIndex) : null;
467 }
468
469 /**
470 * Indicates whether this domain name ends in a {@linkplain #isRegistrySuffix() registry suffix},
471 * while not being a registry suffix itself. For example, returns {@code true} for {@code
472 * www.google.com}, {@code foo.co.uk} and {@code blogspot.com}, but not for {@code com}, {@code
473 * co.uk}, or {@code google.invalid}.
474 *
475 * @since 23.3
476 */
477 public boolean isUnderRegistrySuffix() {
478 return registrySuffixIndex > 0;
479 }
480
481 /**
482 * Indicates whether this domain name is composed of exactly one subdomain component followed by a
483 * {@linkplain #isRegistrySuffix() registry suffix}. For example, returns {@code true} for {@code
484 * google.com}, {@code foo.co.uk}, and {@code blogspot.com}, but not for {@code www.google.com},
485 * {@code co.uk}, or {@code myblog.blogspot.com}.
486 *
487 * <p><b>Warning:</b> This method should not be used to determine the probable highest level
488 * parent domain for which cookies may be set. Use {@link #topPrivateDomain()} for that purpose.
489 *
490 * @since 23.3
491 */
492 public boolean isTopDomainUnderRegistrySuffix() {
493 return registrySuffixIndex == 1;
494 }
495
496 /**
497 * Returns the portion of this domain name that is one level beneath the {@linkplain
498 * #isRegistrySuffix() registry suffix}. For example, for {@code x.adwords.google.co.uk} it
499 * returns {@code google.co.uk}, since {@code co.uk} is a registry suffix. Similarly, for {@code
500 * myblog.blogspot.com} it returns {@code blogspot.com}, since {@code com} is a registry suffix.
501 *
502 * <p>If {@link #isTopDomainUnderRegistrySuffix()} is true, the current domain name instance is
503 * returned.
504 *
505 * <p><b>Warning:</b> This method should not be used to determine whether a domain is probably the
506 * highest level for which cookies may be set. Use {@link #isTopPrivateDomain()} for that purpose.
507 *
508 * @throws IllegalStateException if this domain does not end with a registry suffix
509 * @since 23.3
510 */
511 public InternetDomainName topDomainUnderRegistrySuffix() {
512 if (isTopDomainUnderRegistrySuffix()) {
513 return this;
514 }
515 checkState(isUnderRegistrySuffix(), "Not under a registry suffix: %s", name);
516 return ancestor(registrySuffixIndex - 1);
394517 }
395518
396519 /**
468591 }
469592
470593 /**
471 * Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})?
472 */
473 private static boolean matchesWildcardPublicSuffix(String domain) {
594 * Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})? If a {@code
595 * desiredType} is specified, the wildcard pattern must also match that type.
596 */
597 private static boolean matchesWildcardSuffixType(
598 Optional<PublicSuffixType> desiredType, String domain) {
474599 final String[] pieces = domain.split(DOT_REGEX, 2);
475 return pieces.length == 2 && PublicSuffixPatterns.UNDER.containsKey(pieces[1]);
600 return pieces.length == 2
601 && matchesType(
602 desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(pieces[1])));
603 }
604
605 /**
606 * If a {@code desiredType} is specified, returns true only if the {@code actualType} is
607 * identical. Otherwise, returns true as long as {@code actualType} is present.
608 */
609 private static boolean matchesType(
610 Optional<PublicSuffixType> desiredType, Optional<PublicSuffixType> actualType) {
611 return desiredType.isPresent() ? desiredType.equals(actualType) : actualType.isPresent();
476612 }
477613
478614 /**
88 * Source:
99 * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/extra/AtomicDouble.java?revision=1.13
1010 * (Modified to adapt to guava coding conventions and
11 * to use AtomicLongFieldUpdater instead of sun.misc.Unsafe)
11 * to use AtomicLong instead of sun.misc.Unsafe)
1212 */
1313
1414 package com.google.common.util.concurrent;
1616 import static java.lang.Double.doubleToRawLongBits;
1717 import static java.lang.Double.longBitsToDouble;
1818
19 import com.google.common.annotations.GwtIncompatible;
2019 import com.google.errorprone.annotations.CanIgnoreReturnValue;
21 import com.google.j2objc.annotations.ReflectionSupport;
22 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
20 import java.util.concurrent.atomic.AtomicLong;
2321
2422 /**
2523 * A {@code double} value that may be updated atomically. See the {@link
5250 * @author Martin Buchholz
5351 * @since 11.0
5452 */
55 @GwtIncompatible
56 @ReflectionSupport(value = ReflectionSupport.Level.FULL)
5753 public class AtomicDouble extends Number implements java.io.Serializable {
5854 private static final long serialVersionUID = 0L;
5955
60 private transient volatile long value;
61
62 private static final AtomicLongFieldUpdater<AtomicDouble> updater =
63 AtomicLongFieldUpdater.newUpdater(AtomicDouble.class, "value");
56 // We would use AtomicLongFieldUpdater, but it has issues on some Android devices.
57 private transient AtomicLong value;
6458
6559 /**
6660 * Creates a new {@code AtomicDouble} with the given initial value.
6862 * @param initialValue the initial value
6963 */
7064 public AtomicDouble(double initialValue) {
71 value = doubleToRawLongBits(initialValue);
65 value = new AtomicLong(doubleToRawLongBits(initialValue));
7266 }
7367
7468 /**
7569 * Creates a new {@code AtomicDouble} with initial value {@code 0.0}.
7670 */
7771 public AtomicDouble() {
78 // assert doubleToRawLongBits(0.0) == 0L;
72 this(0.0);
7973 }
8074
8175 /**
8478 * @return the current value
8579 */
8680 public final double get() {
87 return longBitsToDouble(value);
81 return longBitsToDouble(value.get());
8882 }
8983
9084 /**
9488 */
9589 public final void set(double newValue) {
9690 long next = doubleToRawLongBits(newValue);
97 value = next;
91 value.set(next);
9892 }
9993
10094 /**
10498 */
10599 public final void lazySet(double newValue) {
106100 long next = doubleToRawLongBits(newValue);
107 updater.lazySet(this, next);
101 value.lazySet(next);
108102 }
109103
110104 /**
115109 */
116110 public final double getAndSet(double newValue) {
117111 long next = doubleToRawLongBits(newValue);
118 return longBitsToDouble(updater.getAndSet(this, next));
112 return longBitsToDouble(value.getAndSet(next));
119113 }
120114
121115 /**
129123 * the actual value was not bitwise equal to the expected value.
130124 */
131125 public final boolean compareAndSet(double expect, double update) {
132 return updater.compareAndSet(this,
126 return value.compareAndSet(
133127 doubleToRawLongBits(expect),
134128 doubleToRawLongBits(update));
135129 }
150144 * @return {@code true} if successful
151145 */
152146 public final boolean weakCompareAndSet(double expect, double update) {
153 return updater.weakCompareAndSet(this,
147 return value.weakCompareAndSet(
154148 doubleToRawLongBits(expect),
155149 doubleToRawLongBits(update));
156150 }
164158 @CanIgnoreReturnValue
165159 public final double getAndAdd(double delta) {
166160 while (true) {
167 long current = value;
161 long current = value.get();
168162 double currentVal = longBitsToDouble(current);
169163 double nextVal = currentVal + delta;
170164 long next = doubleToRawLongBits(nextVal);
171 if (updater.compareAndSet(this, current, next)) {
165 if (value.compareAndSet(current, next)) {
172166 return currentVal;
173167 }
174168 }
183177 @CanIgnoreReturnValue
184178 public final double addAndGet(double delta) {
185179 while (true) {
186 long current = value;
180 long current = value.get();
187181 double currentVal = longBitsToDouble(current);
188182 double nextVal = currentVal + delta;
189183 long next = doubleToRawLongBits(nextVal);
190 if (updater.compareAndSet(this, current, next)) {
184 if (value.compareAndSet(current, next)) {
191185 return nextVal;
192186 }
193187 }
250244 private void readObject(java.io.ObjectInputStream s)
251245 throws java.io.IOException, ClassNotFoundException {
252246 s.defaultReadObject();
253
247 value = new AtomicLong();
254248 set(s.readDouble());
255249 }
256250 }
0 /*
1 * Copyright (C) 2017 The Guava Authors
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4 * in compliance with the License. You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software distributed under the License
9 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10 * or implied. See the License for the specific language governing permissions and limitations under
11 * the License.
12 */
13
14 package com.google.common.util.concurrent;
15
16 import java.util.Date;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.locks.Condition;
19
20 /** Forwarding wrapper around a {@code Condition}. */
21 abstract class ForwardingCondition implements Condition {
22 abstract Condition delegate();
23
24 @Override
25 public void await() throws InterruptedException {
26 delegate().await();
27 }
28
29 @Override
30 public void awaitUninterruptibly() {
31 delegate().awaitUninterruptibly();
32 }
33
34 @Override
35 public long awaitNanos(long nanosTimeout) throws InterruptedException {
36 return delegate().awaitNanos(nanosTimeout);
37 }
38
39 @Override
40 public boolean await(long time, TimeUnit unit) throws InterruptedException {
41 return delegate().await(time, unit);
42 }
43
44 @Override
45 public boolean awaitUntil(Date deadline) throws InterruptedException {
46 return delegate().awaitUntil(deadline);
47 }
48
49 @Override
50 public void signal() {
51 delegate().signal();
52 }
53
54 @Override
55 public void signalAll() {
56 delegate().signalAll();
57 }
58 }
0 /*
1 * Copyright (C) 2017 The Guava Authors
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4 * in compliance with the License. You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software distributed under the License
9 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10 * or implied. See the License for the specific language governing permissions and limitations under
11 * the License.
12 */
13
14 package com.google.common.util.concurrent;
15
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.locks.Condition;
18 import java.util.concurrent.locks.Lock;
19
20 /** Forwarding wrapper around a {@code Lock}. */
21 abstract class ForwardingLock implements Lock {
22 abstract Lock delegate();
23
24 @Override
25 public void lock() {
26 delegate().lock();
27 }
28
29 @Override
30 public void lockInterruptibly() throws InterruptedException {
31 delegate().lockInterruptibly();
32 }
33
34 @Override
35 public boolean tryLock() {
36 return delegate().tryLock();
37 }
38
39 @Override
40 public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
41 return delegate().tryLock(time, unit);
42 }
43
44 @Override
45 public void unlock() {
46 delegate().unlock();
47 }
48
49 @Override
50 public Condition newCondition() {
51 return delegate().newCondition();
52 }
53 }
6161 Collections.synchronizedList(new ArrayList<PerListenerQueue<L>>());
6262
6363 /** Method reference-compatible listener event. */
64 public interface Event<L> {
64 interface Event<L> {
6565 /** Call a method on the listener. */
6666 void call(L listener);
6767 }
420420 * trucking. If an {@code Error} is thrown, the error will propagate and execution will stop until
421421 * the next time a task is submitted.
422422 *
423 * @deprecated Use {@link #newSequentialExecutor}. This method is scheduled for removal in
424 * January 2018.
423425 * @since 23.1
426 */
427 @Beta
428 @Deprecated
429 @GwtIncompatible
430 public static Executor sequentialExecutor(Executor delegate) {
431 return new SequentialExecutor(delegate);
432 }
433
434 /**
435 * Returns an {@link Executor} that runs each task executed sequentially, such that no two tasks
436 * are running concurrently.
437 *
438 * <p>The executor uses {@code delegate} in order to {@link Executor#execute execute} each task in
439 * turn, and does not create any threads of its own.
440 *
441 * <p>After execution starts on the {@code delegate} {@link Executor}, tasks are polled and
442 * executed from the queue until there are no more tasks. The thread will not be released until
443 * there are no more tasks to run.
444 *
445 * <p>If a task is {@linkplain Thread#interrupt interrupted}, execution of subsequent tasks
446 * continues. {@code RuntimeException}s thrown by tasks are simply logged and the executor keeps
447 * trucking. If an {@code Error} is thrown, the error will propagate and execution will stop until
448 * the next time a task is submitted.
449 *
450 * @since 23.3 (since 23.1 as {@link #sequentialExecutor(Executor)})
424451 */
425452 @Beta
426453 @GwtIncompatible
427 public static Executor sequentialExecutor(Executor delegate) {
454 public static Executor newSequentialExecutor(Executor delegate) {
428455 return new SequentialExecutor(delegate);
429456 }
430457
5050
5151 private final QueueWorker worker = new QueueWorker();
5252
53 /** Use {@link MoreExecutors#sequentialExecutor} */
53 /** Use {@link MoreExecutors#newSequentialExecutor} */
5454 SequentialExecutor(Executor executor) {
5555 this.executor = Preconditions.checkNotNull(executor);
5656 }
3434 import java.util.concurrent.ConcurrentMap;
3535 import java.util.concurrent.Semaphore;
3636 import java.util.concurrent.atomic.AtomicReferenceArray;
37 import java.util.concurrent.locks.Condition;
3738 import java.util.concurrent.locks.Lock;
3839 import java.util.concurrent.locks.ReadWriteLock;
3940 import java.util.concurrent.locks.ReentrantLock;
5051 * <p>The guarantee provided by this class is that equal keys lead to the same lock (or semaphore),
5152 * i.e. {@code if (key1.equals(key2))} then {@code striped.get(key1) == striped.get(key2)} (assuming
5253 * {@link Object#hashCode()} is correctly implemented for the keys). Note that if {@code key1} is
53 * <strong>not</strong> equal to {@code key2}, it is <strong>not</strong> guaranteed that
54 * {@code striped.get(key1) != striped.get(key2)}; the elements might nevertheless be mapped to the
55 * same lock. The lower the number of stripes, the higher the probability of this happening.
54 * <strong>not</strong> equal to {@code key2}, it is <strong>not</strong> guaranteed that {@code
55 * striped.get(key1) != striped.get(key2)}; the elements might nevertheless be mapped to the same
56 * lock. The lower the number of stripes, the higher the probability of this happening.
5657 *
5758 * <p>There are three flavors of this class: {@code Striped<Lock>}, {@code Striped<Semaphore>}, and
58 * {@code Striped<ReadWriteLock>}. For each type, two implementations are offered:
59 * {@linkplain #lock(int) strong} and {@linkplain #lazyWeakLock(int) weak} {@code Striped<Lock>},
60 * {@linkplain #semaphore(int, int) strong} and {@linkplain #lazyWeakSemaphore(int, int) weak}
61 * {@code Striped<Semaphore>}, and {@linkplain #readWriteLock(int) strong} and
62 * {@linkplain #lazyWeakReadWriteLock(int) weak} {@code Striped<ReadWriteLock>}. <i>Strong</i> means
63 * that all stripes (locks/semaphores) are initialized eagerly, and are not reclaimed unless
64 * {@code Striped} itself is reclaimable. <i>Weak</i> means that locks/semaphores are created
65 * lazily, and they are allowed to be reclaimed if nobody is holding on to them. This is useful, for
66 * example, if one wants to create a {@code
67 * Striped<Lock>} of many locks, but worries that in most cases only a small portion of these would
68 * be in use.
59 * {@code Striped<ReadWriteLock>}. For each type, two implementations are offered: {@linkplain
60 * #lock(int) strong} and {@linkplain #lazyWeakLock(int) weak} {@code Striped<Lock>}, {@linkplain
61 * #semaphore(int, int) strong} and {@linkplain #lazyWeakSemaphore(int, int) weak} {@code
62 * Striped<Semaphore>}, and {@linkplain #readWriteLock(int) strong} and {@linkplain
63 * #lazyWeakReadWriteLock(int) weak} {@code Striped<ReadWriteLock>}. <i>Strong</i> means that all
64 * stripes (locks/semaphores) are initialized eagerly, and are not reclaimed unless {@code Striped}
65 * itself is reclaimable. <i>Weak</i> means that locks/semaphores are created lazily, and they are
66 * allowed to be reclaimed if nobody is holding on to them. This is useful, for example, if one
67 * wants to create a {@code Striped<Lock>} of many locks, but worries that in most cases only a
68 * small portion of these would be in use.
6969 *
7070 * <p>Prior to this class, one might be tempted to use {@code Map<K, Lock>}, where {@code K}
7171 * represents the task. This maximizes concurrency by having each unique key mapped to a unique
9292 private Striped() {}
9393
9494 /**
95 * Returns the stripe that corresponds to the passed key. It is always guaranteed that if
96 * {@code key1.equals(key2)}, then {@code get(key1) == get(key2)}.
95 * Returns the stripe that corresponds to the passed key. It is always guaranteed that if {@code
96 * key1.equals(key2)}, then {@code get(key1) == get(key2)}.
9797 *
9898 * @param key an arbitrary, non-null key
9999 * @return the stripe that the passed key corresponds to
114114 */
115115 abstract int indexFor(Object key);
116116
117 /**
118 * Returns the total number of stripes in this instance.
119 */
117 /** Returns the total number of stripes in this instance. */
120118 public abstract int size();
121119
122120 /**
123 * Returns the stripes that correspond to the passed objects, in ascending (as per
124 * {@link #getAt(int)}) order. Thus, threads that use the stripes in the order returned by this
125 * method are guaranteed to not deadlock each other.
126 *
127 * <p>It should be noted that using a {@code Striped<L>} with relatively few stripes, and
128 * {@code bulkGet(keys)} with a relative large number of keys can cause an excessive number of
129 * shared stripes (much like the birthday paradox, where much fewer than anticipated birthdays are
130 * needed for a pair of them to match). Please consider carefully the implications of the number
131 * of stripes, the intended concurrency level, and the typical number of keys used in a
132 * {@code bulkGet(keys)} operation. See <a href="http://www.mathpages.com/home/kmath199.htm">Balls
133 * in Bins model</a> for mathematical formulas that can be used to estimate the probability of
121 * Returns the stripes that correspond to the passed objects, in ascending (as per {@link
122 * #getAt(int)}) order. Thus, threads that use the stripes in the order returned by this method
123 * are guaranteed to not deadlock each other.
124 *
125 * <p>It should be noted that using a {@code Striped<L>} with relatively few stripes, and {@code
126 * bulkGet(keys)} with a relative large number of keys can cause an excessive number of shared
127 * stripes (much like the birthday paradox, where much fewer than anticipated birthdays are needed
128 * for a pair of them to match). Please consider carefully the implications of the number of
129 * stripes, the intended concurrency level, and the typical number of keys used in a {@code
130 * bulkGet(keys)} operation. See <a href="http://www.mathpages.com/home/kmath199.htm">Balls in
131 * Bins model</a> for mathematical formulas that can be used to estimate the probability of
134132 * collisions.
135133 *
136134 * @param keys arbitrary non-null keys
283281 * @return a new {@code Striped<ReadWriteLock>}
284282 */
285283 public static Striped<ReadWriteLock> lazyWeakReadWriteLock(int stripes) {
286 return lazy(stripes, READ_WRITE_LOCK_SUPPLIER);
287 }
288
289 // ReentrantReadWriteLock is large enough to make padding probably unnecessary
284 return lazy(stripes, WEAK_SAFE_READ_WRITE_LOCK_SUPPLIER);
285 }
286
290287 private static final Supplier<ReadWriteLock> READ_WRITE_LOCK_SUPPLIER =
291288 new Supplier<ReadWriteLock>() {
292289 @Override
294291 return new ReentrantReadWriteLock();
295292 }
296293 };
294
295 private static final Supplier<ReadWriteLock> WEAK_SAFE_READ_WRITE_LOCK_SUPPLIER =
296 new Supplier<ReadWriteLock>() {
297 @Override
298 public ReadWriteLock get() {
299 return new WeakSafeReadWriteLock();
300 }
301 };
302
303 /**
304 * ReadWriteLock implementation whose read and write locks retain a reference back to this lock.
305 * Otherwise, a reference to just the read lock or just the write lock would not suffice to ensure
306 * the {@code ReadWriteLock} is retained.
307 */
308 private static final class WeakSafeReadWriteLock implements ReadWriteLock {
309 private final ReadWriteLock delegate;
310
311 WeakSafeReadWriteLock() {
312 this.delegate = new ReentrantReadWriteLock();
313 }
314
315 @Override
316 public Lock readLock() {
317 return new WeakSafeLock(delegate.readLock(), this);
318 }
319
320 @Override
321 public Lock writeLock() {
322 return new WeakSafeLock(delegate.writeLock(), this);
323 }
324 }
325
326 /** Lock object that ensures a strong reference is retained to a specified object. */
327 private static final class WeakSafeLock extends ForwardingLock {
328 private final Lock delegate;
329
330 @SuppressWarnings("unused")
331 private final WeakSafeReadWriteLock strongReference;
332
333 WeakSafeLock(Lock delegate, WeakSafeReadWriteLock strongReference) {
334 this.delegate = delegate;
335 this.strongReference = strongReference;
336 }
337
338 @Override
339 Lock delegate() {
340 return delegate;
341 }
342
343 @Override
344 public Condition newCondition() {
345 return new WeakSafeCondition(delegate.newCondition(), strongReference);
346 }
347 }
348
349 /** Condition object that ensures a strong reference is retained to a specified object. */
350 private static final class WeakSafeCondition extends ForwardingCondition {
351 private final Condition delegate;
352
353 @SuppressWarnings("unused")
354 private final WeakSafeReadWriteLock strongReference;
355
356 WeakSafeCondition(Condition delegate, WeakSafeReadWriteLock strongReference) {
357 this.delegate = delegate;
358 this.strongReference = strongReference;
359 }
360
361 @Override
362 Condition delegate() {
363 return delegate;
364 }
365 }
297366
298367 private abstract static class PowerOfTwoStriped<L> extends Striped<L> {
299368 /** Capacity (power of two) minus one, for fast mod evaluation */
456525 }
457526 }
458527
459 /**
460 * A bit mask were all bits are set.
461 */
528 /** A bit mask were all bits are set. */
462529 private static final int ALL_SET = ~0;
463530
464531 private static int ceilToPowerOfTwo(int x) {
1313
1414 package com.google.thirdparty.publicsuffix;
1515
16 import com.google.common.annotations.Beta;
1617 import com.google.common.annotations.GwtCompatible;
1718
1819 /**
19 * Specifies the type of a top-level domain definition.
20 * <b>Do not use this class directly. For access to public-suffix information,
21 * use {@link com.google.common.net.InternetDomainName}.</b>
22 *
23 * <p>Specifies the type of a top-level domain definition.
24 *
25 * @since 23.3
2026 */
27 @Beta
2128 @GwtCompatible
22 enum PublicSuffixType {
29 public enum PublicSuffixType {
2330
24 /** private definition of a top-level domain */
31 /** Public suffix that is provided by a private company, e.g. "blogspot.com" */
2532 PRIVATE(':', ','),
26 /** ICANN definition of a top-level domain */
27 ICANN('!', '?');
33 /** Public suffix that is backed by an ICANN-style domain name registry */
34 REGISTRY('!', '?');
2835
2936 /** The character used for an inner node in the trie encoding */
3037 private final char innerNodeCode;
5663 }
5764
5865 static PublicSuffixType fromIsPrivate(boolean isPrivate) {
59 return isPrivate ? PRIVATE : ICANN;
66 return isPrivate ? PRIVATE : REGISTRY;
6067 }
6168 }
1717 import com.google.common.base.Joiner;
1818 import com.google.common.collect.ImmutableMap;
1919 import com.google.common.collect.Lists;
20
2120 import java.util.List;
2221
2322 /**
7372 stack.add(0, reverse(encoded.subSequence(0, idx)));
7473
7574 if (c == '!' || c == '?' || c == ':' || c == ',') {
76 // '!' represents an interior node that represents an ICANN entry in the map.
77 // '?' represents a leaf node, which represents an ICANN entry in map.
75 // '!' represents an interior node that represents a REGISTRY entry in the map.
76 // '?' represents a leaf node, which represents a REGISTRY entry in map.
7877 // ':' represents an interior node that represents a private entry in the map
7978 // ',' represents a leaf node, which represents a private entry in the map.
8079 String domain = PREFIX_JOINER.join(stack);
44 <parent>
55 <groupId>com.google.guava</groupId>
66 <artifactId>guava-parent</artifactId>
7 <version>23.2-android</version>
7 <version>23.3-android</version>
88 </parent>
99 <artifactId>guava-testlib</artifactId>
1010 <name>Guava Testing Library</name>
44 <parent>
55 <groupId>com.google.guava</groupId>
66 <artifactId>guava-parent</artifactId>
7 <version>23.2-android</version>
7 <version>23.3-android</version>
88 </parent>
99 <artifactId>guava-tests</artifactId>
1010 <name>Guava Unit Tests</name>
149149 assertNotNull(getClass().getResource("internal/Finalizer.class"));
150150 }
151151
152 public void testFinalizeClassHasNoNestedClases() throws Exception {
152 public void testFinalizeClassHasNoNestedClasses() throws Exception {
153153 // Ensure that the Finalizer class has no nested classes.
154154 // See https://code.google.com/p/guava-libraries/issues/detail?id=1505
155155 assertEquals(Collections.emptyList(), Arrays.asList(Finalizer.class.getDeclaredClasses()));
9999 assertThat(letters).containsExactly("", "a", "b", "c").inOrder();
100100 }
101101
102 public void testCharacterSplitWithMultitpleLetters() {
102 public void testCharacterSplitWithMultipleLetters() {
103103 Iterable<String> testCharacteringMotto = Splitter.on('-').split(
104104 "Testing-rocks-Debugging-sucks");
105105 assertThat(testCharacteringMotto)
114114 createDirectedGraph("hd", "he", "hg", "da", "db", "dc", "gf");
115115
116116 /**
117 * Two disjoint tree-shaped graphs that look as follows (all edges are directed facing downwards):
118 *
119 * <pre>{@code
120 * a c
121 * | |
122 * | |
123 * b d
124 * }</pre>
125 */
126 private static final SuccessorsFunction<Character> TWO_TREES = createDirectedGraph("ab", "cd");
127
128 /**
129 * A graph consisting of a single root {@code a}:
130 *
131 * <pre>{@code
132 * a
133 * }</pre>
134 */
135 private static final SuccessorsFunction<Character> SINGLE_ROOT = createSingleRootGraph();
136
137 /**
117138 * A graph that is not a tree (for example, it has two antiparallel edge between {@code e} and
118139 * {@code f} and thus has a cycle) but is a valid input to {@link Traverser#forTree} when starting
119140 * e.g. at node {@code a} (all edges without an arrow are directed facing downwards):
198219 assertEqualCharNodes(traverser.breadthFirst('a'), "a");
199220 }
200221
222 @Test
223 public void forGraph_breadthFirst_twoTrees() {
224 Iterable<Character> result = Traverser.forGraph(TWO_TREES).breadthFirst('a');
225
226 assertEqualCharNodes(result, "ab");
227 }
228
229 @Test
230 public void forGraph_breadthFirst_singleRoot() {
231 Iterable<Character> result = Traverser.forGraph(SINGLE_ROOT).breadthFirst('a');
232
233 assertEqualCharNodes(result, "a");
234 }
235
236 @Test
237 public void forGraph_breadthFirst_emptyGraph() {
238 try {
239 Traverser.forGraph(createDirectedGraph()).breadthFirst('a');
240 fail("Expected IllegalArgumentException");
241 } catch (IllegalArgumentException expected) {
242 }
243 }
244
201245 /**
202246 * Checks that the elements of the iterable are calculated on the fly. Concretely, that means that
203247 * {@link SuccessorsFunction#successors(Object)} can only be called for a subset of all nodes.
208252 Iterable<Character> result = Traverser.forGraph(graph).breadthFirst('a');
209253
210254 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
211 assertThat(graph.requestedNodes).containsExactly('a', 'b');
255 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b');
212256
213257 // Iterate again to see if calculation is done again
214258 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
215 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b');
259 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b');
216260 }
217261
218262 @Test
269313 }
270314
271315 @Test
316 public void forGraph_depthFirstPreOrder_twoTrees() {
317 Iterable<Character> result = Traverser.forGraph(TWO_TREES).depthFirstPreOrder('a');
318
319 assertEqualCharNodes(result, "ab");
320 }
321
322 @Test
323 public void forGraph_depthFirstPreOrder_singleRoot() {
324 Iterable<Character> result = Traverser.forGraph(SINGLE_ROOT).depthFirstPreOrder('a');
325
326 assertEqualCharNodes(result, "a");
327 }
328
329 @Test
330 public void forGraph_depthFirstPreOrder_emptyGraph() {
331 try {
332 Traverser.forGraph(createDirectedGraph()).depthFirstPreOrder('a');
333 fail("Expected IllegalArgumentException");
334 } catch (IllegalArgumentException expected) {
335 }
336 }
337
338 @Test
272339 public void forGraph_depthFirstPreOrder_iterableIsLazy() {
273340 RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH);
274341 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder('a');
275342
276343 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
277 assertThat(graph.requestedNodes).containsExactly('a', 'b', 'd');
344 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'd');
278345
279346 // Iterate again to see if calculation is done again
280347 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
281 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b', 'd', 'd');
348 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'd', 'd');
282349 }
283350
284351 @Test
334401 }
335402
336403 @Test
404 public void forGraph_depthFirstPostOrder_twoTrees() {
405 Iterable<Character> result = Traverser.forGraph(TWO_TREES).depthFirstPostOrder('a');
406
407 assertEqualCharNodes(result, "ba");
408 }
409
410 @Test
411 public void forGraph_depthFirstPostOrder_singleRoot() {
412 Iterable<Character> result = Traverser.forGraph(SINGLE_ROOT).depthFirstPostOrder('a');
413
414 assertEqualCharNodes(result, "a");
415 }
416
417 @Test
418 public void forGraph_depthFirstPostOrder_emptyGraph() {
419 try {
420 Traverser.forGraph(createDirectedGraph()).depthFirstPostOrder('a');
421 fail("Expected IllegalArgumentException");
422 } catch (IllegalArgumentException expected) {
423 }
424 }
425
426 @Test
337427 public void forGraph_depthFirstPostOrder_iterableIsLazy() {
338428 RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH);
339429 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder('a');
340430
341431 assertEqualCharNodes(Iterables.limit(result, 2), "db");
342 assertThat(graph.requestedNodes).containsExactly('a', 'b', 'd');
432 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'd');
343433
344434 // Iterate again to see if calculation is done again
345435 assertEqualCharNodes(Iterables.limit(result, 2), "db");
346 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b', 'd', 'd');
436 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'd', 'd');
347437 }
348438
349439 @Test
437527 }
438528
439529 @Test
530 public void forTree_breadthFirst_twoTrees() {
531 Iterable<Character> result = Traverser.forTree(TWO_TREES).breadthFirst('a');
532
533 assertEqualCharNodes(result, "ab");
534 }
535
536 @Test
537 public void forTree_breadthFirst_singleRoot() {
538 Iterable<Character> result = Traverser.forTree(SINGLE_ROOT).breadthFirst('a');
539
540 assertEqualCharNodes(result, "a");
541 }
542
543 @Test
544 public void forTree_breadthFirst_emptyGraph() {
545 try {
546 Traverser.forTree(createDirectedGraph()).breadthFirst('a');
547 fail("Expected IllegalArgumentException");
548 } catch (IllegalArgumentException expected) {
549 }
550 }
551
552 @Test
440553 public void forTree_breadthFirst_iterableIsLazy() {
441554 RequestSavingGraph graph = new RequestSavingGraph(TREE);
442555 Iterable<Character> result = Traverser.forGraph(graph).breadthFirst('h');
443556
444557 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
445 assertThat(graph.requestedNodes).containsExactly('h', 'd');
558 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd');
446559
447560 // Iterate again to see if calculation is done again
448561 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
449 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'd');
562 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd');
450563 }
451564
452565 @Test
477590 }
478591
479592 @Test
593 public void forTree_depthFirstPreOrder_twoTrees() {
594 Iterable<Character> result = Traverser.forTree(TWO_TREES).depthFirstPreOrder('a');
595
596 assertEqualCharNodes(result, "ab");
597 }
598
599 @Test
600 public void forTree_depthFirstPreOrder_singleRoot() {
601 Iterable<Character> result = Traverser.forTree(SINGLE_ROOT).depthFirstPreOrder('a');
602
603 assertEqualCharNodes(result, "a");
604 }
605
606 @Test
607 public void forTree_depthFirstPreOrder_emptyGraph() {
608 try {
609 Traverser.forTree(createDirectedGraph()).depthFirstPreOrder('a');
610 fail("Expected IllegalArgumentException");
611 } catch (IllegalArgumentException expected) {
612 }
613 }
614
615 @Test
480616 public void forTree_depthFirstPreOrder_iterableIsLazy() {
481617 RequestSavingGraph graph = new RequestSavingGraph(TREE);
482618 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder('h');
483619
484620 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
485 assertThat(graph.requestedNodes).containsExactly('h', 'd', 'a');
621 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'a');
486622
487623 // Iterate again to see if calculation is done again
488624 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
489 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'd', 'a', 'a');
625 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd', 'a', 'a');
490626 }
491627
492628 @Test
517653 }
518654
519655 @Test
656 public void forTree_depthFirstPostOrder_twoTrees() {
657 Iterable<Character> result = Traverser.forTree(TWO_TREES).depthFirstPostOrder('a');
658
659 assertEqualCharNodes(result, "ba");
660 }
661
662 @Test
663 public void forTree_depthFirstPostOrder_singleRoot() {
664 Iterable<Character> result = Traverser.forTree(SINGLE_ROOT).depthFirstPostOrder('a');
665
666 assertEqualCharNodes(result, "a");
667 }
668
669 @Test
670 public void forTree_depthFirstPostOrder_emptyGraph() {
671 try {
672 Traverser.forTree(createDirectedGraph()).depthFirstPostOrder('a');
673 fail("Expected IllegalArgumentException");
674 } catch (IllegalArgumentException expected) {
675 }
676 }
677
678 @Test
520679 public void forTree_depthFirstPostOrder_iterableIsLazy() {
521680 RequestSavingGraph graph = new RequestSavingGraph(TREE);
522681 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder('h');
523682
524683 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
525 assertThat(graph.requestedNodes).containsExactly('h', 'd', 'a', 'b');
684 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'a', 'b');
526685
527686 // Iterate again to see if calculation is done again
528687 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
529 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'd', 'a', 'a', 'b', 'b');
688 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd', 'a', 'a', 'b', 'b');
530689 }
531690
532691 private static SuccessorsFunction<Character> createDirectedGraph(String... edges) {
560719 return new SuccessorsFunction<Character>() {
561720 @Override
562721 public Iterable<? extends Character> successors(Character node) {
722 checkArgument(
723 graphMap.containsKey(node) || graphMap.containsValue(node),
724 "Node %s is not an element of this graph",
725 node);
563726 return Ordering.natural().immutableSortedCopy(graphMap.get(node));
564727 }
565728 };
729 }
730
731 private static ImmutableGraph<Character> createSingleRootGraph() {
732 MutableGraph<Character> graph = GraphBuilder.directed().build();
733 graph.addNode('a');
734 return ImmutableGraph.copyOf(graph);
566735 }
567736
568737 private static void assertEqualCharNodes(Iterable<Character> result, String expectedCharacters) {
5858 assertCrc(0x113fdb5c, descending);
5959 }
6060
61 public void testScsiReadCommad() {
61 public void testScsiReadCommand() {
6262 // Test SCSI read command.
6363 byte[] scsiReadCommand = new byte[] {
6464 0x01, (byte) 0xc0, 0x00, 0x00,
1919 import static com.google.common.math.MathTesting.FINITE_DOUBLE_CANDIDATES;
2020 import static com.google.common.math.MathTesting.POSITIVE_FINITE_DOUBLE_CANDIDATES;
2121
22 import java.lang.reflect.Method;
2223 import java.math.BigInteger;
2324 import junit.framework.TestCase;
24 import sun.misc.FpUtils;
2525
2626 /**
2727 * Tests for {@link DoubleUtils}.
2929 * @author Louis Wasserman
3030 */
3131 public class DoubleUtilsTest extends TestCase {
32 @AndroidIncompatible // no FpUtils
33 public void testNextDown() {
32 @AndroidIncompatible // no FpUtils and no Math.nextDown in old versions
33 public void testNextDown() throws Exception {
34 Method jdkNextDown = getJdkNextDown();
3435 for (double d : FINITE_DOUBLE_CANDIDATES) {
35 assertEquals(FpUtils.nextDown(d), DoubleUtils.nextDown(d));
36 assertEquals(jdkNextDown.invoke(null, d), DoubleUtils.nextDown(d));
37 }
38 }
39
40 private static Method getJdkNextDown() throws Exception {
41 try {
42 return Math.class.getMethod("nextDown", double.class);
43 } catch (NoSuchMethodException expectedBeforeJava8) {
44 return Class.forName("sun.misc.FpUtils").getMethod("nextDown", double.class);
3645 }
3746 }
3847
9595 ALMOST_TOO_MANY_LEVELS + "com",
9696 ALMOST_TOO_LONG + ".c");
9797
98 private static final ImmutableSet<String> PS = ImmutableSet.of(
99 "com",
100 "co.uk",
101 "foo.bd",
102 "xxxxxx.bd",
103 "org.mK",
104 "us",
105 "uk\uFF61com.", // Alternate dot character
106 "\u7f51\u7edc.Cn", // "网络.Cn"
107 "j\u00f8rpeland.no", // "jorpeland.no" (first o slashed)
108 "xn--jrpeland-54a.no" // IDNA (punycode) encoding of above
109 );
98 private static final ImmutableSet<String> RS =
99 ImmutableSet.of(
100 "com",
101 "co.uk",
102 "foo.bd",
103 "xxxxxx.bd",
104 "org.mK",
105 "us",
106 "co.uk.", // Trailing dot
107 "co\uFF61uk", // Alternate dot character
108 "\u7f51\u7edc.Cn", // "网络.Cn"
109 "j\u00f8rpeland.no", // "jorpeland.no" (first o slashed)
110 "xn--jrpeland-54a.no"); // IDNA (punycode) encoding of above
111
112 private static final ImmutableSet<String> PS_NOT_RS =
113 ImmutableSet.of("blogspot.com", "blogspot.co.uk", "uk.com");
114
115 private static final ImmutableSet<String> PS =
116 ImmutableSet.<String>builder().addAll(RS).addAll(PS_NOT_RS).build();
110117
111118 private static final ImmutableSet<String> NO_PS = ImmutableSet.of(
112119 "www", "foo.ihopethiswillneverbeapublicsuffix", "x.y.z");
113120
114 private static final ImmutableSet<String> NON_PS = ImmutableSet.of(
115 "foo.bar.com", "foo.ca", "foo.bar.ca",
116 "foo.bar.co.il", "state.CA.us", "www.state.pa.us", "pvt.k12.ca.us",
117 "www.google.com", "www4.yahoo.co.uk", "home.netscape.com",
118 "web.MIT.edu", "foo.eDu.au", "utenti.blah.IT", "dominio.com.co");
121 /**
122 * Having a public suffix is equivalent to having a registry suffix, because all registry suffixes
123 * are public suffixes, and all public suffixes have registry suffixes.
124 */
125 private static final ImmutableSet<String> NO_RS = NO_PS;
126
127 private static final ImmutableSet<String> NON_PS =
128 ImmutableSet.of(
129 "foo.bar.com",
130 "foo.ca",
131 "foo.bar.ca",
132 "foo.blogspot.com",
133 "foo.blogspot.co.uk",
134 "foo.uk.com",
135 "foo.bar.co.il",
136 "state.CA.us",
137 "www.state.pa.us",
138 "pvt.k12.ca.us",
139 "www.google.com",
140 "www4.yahoo.co.uk",
141 "home.netscape.com",
142 "web.MIT.edu",
143 "foo.eDu.au",
144 "utenti.blah.IT",
145 "dominio.com.co");
146
147 private static final ImmutableSet<String> NON_RS =
148 ImmutableSet.<String>builder().addAll(NON_PS).addAll(PS_NOT_RS).build();
149
150 private static final ImmutableSet<String> TOP_UNDER_REGISTRY_SUFFIX =
151 ImmutableSet.of("google.com", "foo.Co.uk", "foo.ca.us.");
119152
120153 private static final ImmutableSet<String> TOP_PRIVATE_DOMAIN =
121 ImmutableSet.of("google.com", "foo.Co.uk", "foo.ca.us.");
154 ImmutableSet.of("google.com", "foo.Co.uk", "foo.ca.us.", "foo.blogspot.com");
155
156 private static final ImmutableSet<String> UNDER_TOP_UNDER_REGISTRY_SUFFIX =
157 ImmutableSet.of("foo.bar.google.com", "a.b.co.uk", "x.y.ca.us");
122158
123159 private static final ImmutableSet<String> UNDER_PRIVATE_DOMAIN =
124 ImmutableSet.of("foo.bar.google.com", "a.b.co.uk", "x.y.ca.us");
160 ImmutableSet.of("foo.bar.google.com", "a.b.co.uk", "x.y.ca.us", "a.b.blogspot.com");
125161
126162 private static final ImmutableSet<String> VALID_IP_ADDRS = ImmutableSet.of(
127163 "1.2.3.4", "127.0.0.1", "::1", "2001:db8::1");
191227 "a\u7f51\u7edcA.\u7f51\u7edc.Cn" // "a网络A.网络.Cn"
192228 );
193229
230 private static final ImmutableSet<String> SOMEWHERE_UNDER_RS =
231 ImmutableSet.<String>builder().addAll(SOMEWHERE_UNDER_PS).addAll(PS_NOT_RS).build();
232
194233 public void testValid() {
195234 for (String name : VALID_NAME) {
196235 InternetDomainName.from(name);
261300 assertTrue(name, domain.hasPublicSuffix());
262301 assertTrue(name, domain.isUnderPublicSuffix());
263302 assertFalse(name, domain.isTopPrivateDomain());
303 }
304 }
305
306 public void testRegistrySuffix() {
307 for (String name : RS) {
308 final InternetDomainName domain = InternetDomainName.from(name);
309 assertTrue(name, domain.isRegistrySuffix());
310 assertTrue(name, domain.hasRegistrySuffix());
311 assertFalse(name, domain.isUnderRegistrySuffix());
312 assertFalse(name, domain.isTopDomainUnderRegistrySuffix());
313 assertEquals(domain, domain.registrySuffix());
314 }
315
316 for (String name : NO_RS) {
317 final InternetDomainName domain = InternetDomainName.from(name);
318 assertFalse(name, domain.isRegistrySuffix());
319 assertFalse(name, domain.hasRegistrySuffix());
320 assertFalse(name, domain.isUnderRegistrySuffix());
321 assertFalse(name, domain.isTopDomainUnderRegistrySuffix());
322 assertNull(domain.registrySuffix());
323 }
324
325 for (String name : NON_RS) {
326 final InternetDomainName domain = InternetDomainName.from(name);
327 assertFalse(name, domain.isRegistrySuffix());
328 assertTrue(name, domain.hasRegistrySuffix());
329 assertTrue(name, domain.isUnderRegistrySuffix());
330 }
331 }
332
333 public void testUnderRegistrySuffix() {
334 for (String name : SOMEWHERE_UNDER_RS) {
335 final InternetDomainName domain = InternetDomainName.from(name);
336 assertFalse(name, domain.isRegistrySuffix());
337 assertTrue(name, domain.hasRegistrySuffix());
338 assertTrue(name, domain.isUnderRegistrySuffix());
339 }
340 }
341
342 public void testTopDomainUnderRegistrySuffix() {
343 for (String name : TOP_UNDER_REGISTRY_SUFFIX) {
344 final InternetDomainName domain = InternetDomainName.from(name);
345 assertFalse(name, domain.isRegistrySuffix());
346 assertTrue(name, domain.hasRegistrySuffix());
347 assertTrue(name, domain.isUnderRegistrySuffix());
348 assertTrue(name, domain.isTopDomainUnderRegistrySuffix());
349 assertEquals(domain.parent(), domain.registrySuffix());
350 }
351 }
352
353 public void testUnderTopDomainUnderRegistrySuffix() {
354 for (String name : UNDER_TOP_UNDER_REGISTRY_SUFFIX) {
355 final InternetDomainName domain = InternetDomainName.from(name);
356 assertFalse(name, domain.isRegistrySuffix());
357 assertTrue(name, domain.hasRegistrySuffix());
358 assertTrue(name, domain.isUnderRegistrySuffix());
359 assertFalse(name, domain.isTopDomainUnderRegistrySuffix());
264360 }
265361 }
266362
362458 }
363459 }
364460
365 public void testExclusion() {
461 public void testPublicSuffixExclusion() {
366462 InternetDomainName domain = InternetDomainName.from("foo.city.yokohama.jp");
367463 assertTrue(domain.hasPublicSuffix());
368464 assertEquals("yokohama.jp", domain.publicSuffix().toString());
371467 assertFalse(domain.publicSuffix().isPublicSuffix());
372468 }
373469
374 public void testMultipleUnders() {
470 public void testPublicSuffixMultipleUnders() {
375471 // PSL has both *.uk and *.sch.uk; the latter should win.
376472 // See http://code.google.com/p/guava-libraries/issues/detail?id=1176
377473
379475 assertTrue(domain.hasPublicSuffix());
380476 assertEquals("essex.sch.uk", domain.publicSuffix().toString());
381477 assertEquals("www.essex.sch.uk", domain.topPrivateDomain().toString());
478 }
479
480 public void testRegistrySuffixExclusion() {
481 InternetDomainName domain = InternetDomainName.from("foo.city.yokohama.jp");
482 assertTrue(domain.hasRegistrySuffix());
483 assertEquals("yokohama.jp", domain.registrySuffix().toString());
484
485 // Behold the weirdness!
486 assertFalse(domain.registrySuffix().isRegistrySuffix());
487 }
488
489 public void testRegistrySuffixMultipleUnders() {
490 // PSL has both *.uk and *.sch.uk; the latter should win.
491 // See http://code.google.com/p/guava-libraries/issues/detail?id=1176
492
493 InternetDomainName domain = InternetDomainName.from("www.essex.sch.uk");
494 assertTrue(domain.hasRegistrySuffix());
495 assertEquals("essex.sch.uk", domain.registrySuffix().toString());
496 assertEquals("www.essex.sch.uk", domain.topDomainUnderRegistrySuffix().toString());
382497 }
383498
384499 public void testEquality() {
345345 testSortDescending(new double[] {1, 2}, new double[] {2, 1});
346346 testSortDescending(new double[] {1, 3, 1}, new double[] {3, 1, 1});
347347 testSortDescending(new double[] {-1, 1, -2, 2}, new double[] {2, 1, -1, -2});
348 testSortDescending(
349 new double[] {-1, 1, Double.NaN, -2, -0, 0, 2},
350 new double[] {Double.NaN, 2, 1, 0, -0, -1, -2});
348 if (sortWorksWithNaN()) {
349 testSortDescending(
350 new double[] {-1, 1, Double.NaN, -2, -0, 0, 2},
351 new double[] {Double.NaN, 2, 1, 0, -0, -1, -2});
352 }
351353 }
352354
353355 public void testSortDescendingIndexed() {
357359 testSortDescending(new double[] {1, 3, 1}, 0, 2, new double[] {3, 1, 1});
358360 testSortDescending(new double[] {1, 3, 1}, 0, 1, new double[] {1, 3, 1});
359361 testSortDescending(new double[] {-1, -2, 1, 2}, 1, 3, new double[] {-1, 1, -2, 2});
360 testSortDescending(
361 new double[] {-1, 1, Double.NaN, -2, 2}, 1, 4, new double[] {-1, Double.NaN, 1, -2, 2});
362 if (sortWorksWithNaN()) {
363 testSortDescending(
364 new double[] {-1, 1, Double.NaN, -2, 2}, 1, 4, new double[] {-1, Double.NaN, 1, -2, 2});
365 }
362366 }
363367
364368 private static void testSortDescending(double[] input, double[] expectedOutput) {
378382 for (int i = 0; i < input.length; i++) {
379383 assertEquals(0, Double.compare(expectedOutput[i], input[i]));
380384 }
385 }
386
387 @GwtIncompatible // works with real browsers but fails with HtmlUnit
388 public void testSortWorksWithNaNNonGwt() {
389 assertTrue(sortWorksWithNaN());
381390 }
382391
383392 @GwtIncompatible // SerializableTester
652661 } catch (NullPointerException expected) {
653662 }
654663 }
664
665 // HtmlUnit looks to be sorting double[] wrongly. We detect that and skip our tests there.
666 private static boolean sortWorksWithNaN() {
667 double[] array = new double[] {NaN, 0};
668 Arrays.sort(array);
669 return array[0] == 0;
670 }
655671 }
342342 testSortDescending(new float[] {1, 2}, new float[] {2, 1});
343343 testSortDescending(new float[] {1, 3, 1}, new float[] {3, 1, 1});
344344 testSortDescending(new float[] {-1, 1, -2, 2}, new float[] {2, 1, -1, -2});
345 testSortDescending(
346 new float[] {-1, 1, Float.NaN, -2, -0, 0, 2}, new float[] {Float.NaN, 2, 1, 0, -0, -1, -2});
345 if (sortWorksWithNaN()) {
346 testSortDescending(
347 new float[] {-1, 1, Float.NaN, -2, -0, 0, 2}, new float[] {Float.NaN, 2, 1, 0, -0, -1, -2});
348 }
347349 }
348350
349351 public void testSortDescendingIndexed() {
353355 testSortDescending(new float[] {1, 3, 1}, 0, 2, new float[] {3, 1, 1});
354356 testSortDescending(new float[] {1, 3, 1}, 0, 1, new float[] {1, 3, 1});
355357 testSortDescending(new float[] {-1, -2, 1, 2}, 1, 3, new float[] {-1, 1, -2, 2});
356 testSortDescending(
357 new float[] {-1, 1, Float.NaN, -2, 2}, 1, 4, new float[] {-1, Float.NaN, 1, -2, 2});
358 if (sortWorksWithNaN()) {
359 testSortDescending(
360 new float[] {-1, 1, Float.NaN, -2, 2}, 1, 4, new float[] {-1, Float.NaN, 1, -2, 2});
361 }
358362 }
359363
360364 private static void testSortDescending(float[] input, float[] expectedOutput) {
374378 for (int i = 0; i < input.length; i++) {
375379 assertEquals(0, Float.compare(expectedOutput[i], input[i]));
376380 }
381 }
382
383 @GwtIncompatible // works with real browsers but fails with HtmlUnit
384 public void testSortWorksWithNaNNonGwt() {
385 assertTrue(sortWorksWithNaN());
377386 }
378387
379388 @GwtIncompatible // SerializableTester
628637 } catch (NullPointerException expected) {
629638 }
630639 }
640
641 // HtmlUnit looks to be sorting float[] wrongly. We detect that and skip our tests there.
642 private static boolean sortWorksWithNaN() {
643 float[] array = new float[] {NaN, 0};
644 Arrays.sort(array);
645 return array[0] == 0;
646 }
631647 }
5252 Exception exception = new SampleCheckedException();
5353 try {
5454 timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
55 fail("Excpected ExecutionException");
55 fail("Expected ExecutionException");
5656 } catch (ExecutionException e) {
5757 assertThat(e.getCause()).isEqualTo(exception);
5858 }
6262 Exception exception = new RuntimeException("test");
6363 try {
6464 timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
65 fail("Excpected UncheckedExecutionException");
65 fail("Expected UncheckedExecutionException");
6666 } catch (UncheckedExecutionException e) {
6767 assertThat(e.getCause()).isEqualTo(exception);
6868 }
8484 RuntimeException exception = new RuntimeException("test");
8585 try {
8686 timeLimiter.runWithTimeout(runnableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
87 fail("Excpected UncheckedExecutionException");
87 fail("Expected UncheckedExecutionException");
8888 } catch (UncheckedExecutionException e) {
8989 assertThat(e.getCause()).isEqualTo(exception);
9090 }
9595 try {
9696 timeLimiter.runUninterruptiblyWithTimeout(
9797 runnableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
98 fail("Excpected UncheckedExecutionException");
98 fail("Expected UncheckedExecutionException");
9999 } catch (UncheckedExecutionException e) {
100100 assertThat(e.getCause()).isEqualTo(exception);
101101 }
571571 verify(service).shutdown();
572572 }
573573
574 public void testGetExitingExcutorService_executorSetToUseDaemonThreads() {
574 public void testGetExitingExecutorService_executorSetToUseDaemonThreads() {
575575 TestApplication application = new TestApplication();
576576 ThreadPoolExecutor executor = new ThreadPoolExecutor(
577577 1, 2, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
579579 assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon());
580580 }
581581
582 public void testGetExitingExcutorService_executorDelegatesToOriginal() {
582 public void testGetExitingExecutorService_executorDelegatesToOriginal() {
583583 TestApplication application = new TestApplication();
584584 ThreadPoolExecutor executor = mock(ThreadPoolExecutor.class);
585585 ThreadFactory threadFactory = mock(ThreadFactory.class);
588588 verify(executor).execute(EMPTY_RUNNABLE);
589589 }
590590
591 public void testGetExitingExcutorService_shutdownHookRegistered() throws InterruptedException {
591 public void testGetExitingExecutorService_shutdownHookRegistered() throws InterruptedException {
592592 TestApplication application = new TestApplication();
593593 ThreadPoolExecutor executor = mock(ThreadPoolExecutor.class);
594594 ThreadFactory threadFactory = mock(ThreadFactory.class);
598598 verify(executor).shutdown();
599599 }
600600
601 public void testGetExitingScheduledExcutorService_executorSetToUseDaemonThreads() {
601 public void testGetExitingScheduledExecutorService_executorSetToUseDaemonThreads() {
602602 TestApplication application = new TestApplication();
603603 ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
604604 assertNotNull(application.getExitingScheduledExecutorService(executor));
605605 assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon());
606606 }
607607
608 public void testGetExitingScheduledExcutorService_executorDelegatesToOriginal() {
608 public void testGetExitingScheduledExecutorService_executorDelegatesToOriginal() {
609609 TestApplication application = new TestApplication();
610610 ScheduledThreadPoolExecutor executor = mock(ScheduledThreadPoolExecutor.class);
611611 ThreadFactory threadFactory = mock(ThreadFactory.class);
614614 verify(executor).execute(EMPTY_RUNNABLE);
615615 }
616616
617 public void testGetScheduledExitingExcutorService_shutdownHookRegistered()
617 public void testGetScheduledExitingExecutorService_shutdownHookRegistered()
618618 throws InterruptedException {
619619 TestApplication application = new TestApplication();
620620 ScheduledThreadPoolExecutor executor = mock(ScheduledThreadPoolExecutor.class);
114114 }
115115 }
116116
117 public void testWeakReadWrite() {
118 Striped<ReadWriteLock> striped = Striped.lazyWeakReadWriteLock(1000);
119 Object key = new Object();
120 Lock readLock = striped.get(key).readLock();
121 WeakReference<Object> garbage = new WeakReference<>(new Object());
122 GcFinalization.awaitClear(garbage);
123 Lock writeLock = striped.get(key).writeLock();
124 readLock.lock();
125 assertFalse(writeLock.tryLock());
126 readLock.unlock();
127 }
128
117129 public void testStrongImplementations() {
118130 for (Striped<?> striped : strongImplementations()) {
119131 WeakReference<Object> weakRef = new WeakReference<>(striped.get(new Object()));
1010 </parent>
1111 <groupId>com.google.guava</groupId>
1212 <artifactId>guava-parent</artifactId>
13 <version>23.2-android</version>
13 <version>23.3-android</version>
1414 <packaging>pom</packaging>
1515 <name>Guava Maven Parent</name>
1616 <url>https://github.com/google/guava</url>
44 <parent>
55 <groupId>com.google.guava</groupId>
66 <artifactId>guava-parent</artifactId>
7 <version>23.2-jre</version>
7 <version>23.3-jre</version>
88 </parent>
99 <artifactId>guava</artifactId>
1010 <packaging>bundle</packaging>
14971497
14981498 /** Implementation of {@link #digit()}. */
14991499 private static final class Digit extends RangesMatcher {
1500 // Plug the following UnicodeSet pattern into
1501 // https://unicode.org/cldr/utility/list-unicodeset.jsp
1502 // [[:Nd:]&[:nv=0:]&[\u0000-\uFFFF]]
1503 // and get the zeroes from there.
15001504
15011505 // Must be in ascending order.
15021506 private static final String ZEROES =
1503 "0\u0660\u06f0\u07c0\u0966\u09e6\u0a66\u0ae6\u0b66"
1504 + "\u0be6\u0c66\u0ce6\u0d66\u0e50\u0ed0\u0f20\u1040\u1090\u17e0\u1810"
1505 + "\u1946\u19d0\u1b50\u1bb0\u1c40\u1c50\ua620\ua8d0\ua900\uaa50\uff10";
1507 "0\u0660\u06f0\u07c0\u0966\u09e6\u0a66\u0ae6\u0b66\u0be6\u0c66\u0ce6\u0d66\u0de6"
1508 + "\u0e50\u0ed0\u0f20\u1040\u1090\u17e0\u1810\u1946\u19d0\u1a80\u1a90\u1b50\u1bb0"
1509 + "\u1c40\u1c50\ua620\ua8d0\ua900\ua9d0\ua9f0\uaa50\uabf0\uff10";
15061510
15071511 private static char[] zeroes() {
15081512 return ZEROES.toCharArray();
16201624
16211625 /** Implementation of {@link #invisible()}. */
16221626 private static final class Invisible extends RangesMatcher {
1623
1627 // Plug the following UnicodeSet pattern into
1628 // https://unicode.org/cldr/utility/list-unicodeset.jsp
1629 // [[[:Zs:][:Zl:][:Zp:][:Cc:][:Cf:][:Cs:][:Co:]]&[\u0000-\uFFFF]]
1630 // with the "Abbreviate" option, and get the ranges from there.
16241631 private static final String RANGE_STARTS =
1625 "\u0000\u007f\u00ad\u0600\u061c\u06dd\u070f\u1680\u180e\u2000\u2028\u205f\u2066\u2067"
1626 + "\u2068\u2069\u206a\u3000\ud800\ufeff\ufff9\ufffa";
1627 private static final String RANGE_ENDS =
1628 "\u0020\u00a0\u00ad\u0604\u061c\u06dd\u070f\u1680\u180e\u200f\u202f\u2064\u2066\u2067"
1629 + "\u2068\u2069\u206f\u3000\uf8ff\ufeff\ufff9\ufffb";
1632 "\u0000\u007f\u00ad\u0600\u061c\u06dd\u070f\u08e2\u1680\u180e\u2000\u2028\u205f\u2066"
1633 + "\u3000\ud800\ufeff\ufff9";
1634 private static final String RANGE_ENDS = // inclusive ends
1635 "\u0020\u00a0\u00ad\u0605\u061c\u06dd\u070f\u08e2\u1680\u180e\u200f\u202f\u2064\u206f"
1636 + "\u3000\uf8ff\ufeff\ufffb";
16301637
16311638 static final Invisible INSTANCE = new Invisible();
16321639
3232 *
3333 * @author Louis Wasserman
3434 * @since 15.0
35 * @deprecated Use {@link com.google.common.graph.Traverser} instead. All instance methods except
36 * for {@link #inOrderTraversal} have their equivalent on the result of {@code
37 * Traverser.forTree(tree)} where {@code tree} implements {@code SuccessorsFunction}, which has
38 * a similar API as {@link #children}.
39 * <p>This class is scheduled to be removed in January 2018.
3540 */
41 @Deprecated
3642 @Beta
3743 @GwtCompatible
3844 public abstract class BinaryTreeTraverser<T> extends TreeTraverser<T> {
6363 *
6464 * @author Louis Wasserman
6565 * @since 15.0
66 * @deprecated Use {@link com.google.common.graph.Traverser} instead. All instance methods have
67 * their equivalent on the result of {@code Traverser.forTree(tree)} where {@code tree}
68 * implements {@code SuccessorsFunction}, which has a similar API as {@link #children} or can be
69 * the same lambda function as passed into {@link #using(Function)}.
70 * <p>This class is scheduled to be removed in January 2018.
6671 */
72 @Deprecated
6773 @Beta
6874 @GwtCompatible
6975 public abstract class TreeTraverser<T> {
7581 * {@code TreeTraverser} and implement its {@link #children} method directly.
7682 *
7783 * @since 20.0
78 */
84 * @deprecated Use {@link com.google.common.graph.Traverser#forTree} instead. If you are using a
85 * lambda, these methods have exactly the same signature.
86 */
87 @Deprecated
7988 public static <T> TreeTraverser<T> using(
8089 final Function<T, ? extends Iterable<T>> nodeToChildrenFunction) {
8190 checkNotNull(nodeToChildrenFunction);
93102 public abstract Iterable<T> children(T root);
94103
95104 /**
96 * Returns an unmodifiable iterable over the nodes in a tree structure, using pre-order
97 * traversal. That is, each node's subtrees are traversed after the node itself is returned.
98 *
99 * <p>No guarantees are made about the behavior of the traversal when nodes change while
100 * iteration is in progress or when the iterators generated by {@link #children} are advanced.
101 */
105 * Returns an unmodifiable iterable over the nodes in a tree structure, using pre-order traversal.
106 * That is, each node's subtrees are traversed after the node itself is returned.
107 *
108 * <p>No guarantees are made about the behavior of the traversal when nodes change while iteration
109 * is in progress or when the iterators generated by {@link #children} are advanced.
110 *
111 * @deprecated Use {@link com.google.common.graph.Traverser#depthFirstPreOrder} instead, which has
112 * the same behavior.
113 */
114 @Deprecated
102115 public final FluentIterable<T> preOrderTraversal(final T root) {
103116 checkNotNull(root);
104117 return new FluentIterable<T>() {
158171 * Returns an unmodifiable iterable over the nodes in a tree structure, using post-order
159172 * traversal. That is, each node's subtrees are traversed before the node itself is returned.
160173 *
161 * <p>No guarantees are made about the behavior of the traversal when nodes change while
162 * iteration is in progress or when the iterators generated by {@link #children} are advanced.
163 */
174 * <p>No guarantees are made about the behavior of the traversal when nodes change while iteration
175 * is in progress or when the iterators generated by {@link #children} are advanced.
176 *
177 * @deprecated Use {@link com.google.common.graph.Traverser#depthFirstPostOrder} instead, which
178 * has the same behavior.
179 */
180 @Deprecated
164181 public final FluentIterable<T> postOrderTraversal(final T root) {
165182 checkNotNull(root);
166183 return new FluentIterable<T>() {
230247 * Returns an unmodifiable iterable over the nodes in a tree structure, using breadth-first
231248 * traversal. That is, all the nodes of depth 0 are returned, then depth 1, then 2, and so on.
232249 *
233 * <p>No guarantees are made about the behavior of the traversal when nodes change while
234 * iteration is in progress or when the iterators generated by {@link #children} are advanced.
235 */
250 * <p>No guarantees are made about the behavior of the traversal when nodes change while iteration
251 * is in progress or when the iterators generated by {@link #children} are advanced.
252 *
253 * @deprecated Use {@link com.google.common.graph.Traverser#breadthFirst} instead, which has the
254 * same behavior.
255 */
256 @Deprecated
236257 public final FluentIterable<T> breadthFirstTraversal(final T root) {
237258 checkNotNull(root);
238259 return new FluentIterable<T>() {
167167 *
168168 * <p>See <a href="https://en.wikipedia.org/wiki/Breadth-first_search">Wikipedia</a> for more
169169 * info.
170 *
171 * @throws IllegalArgumentException if {@code startNode} is not an element of the graph
170172 */
171173 public abstract Iterable<N> breadthFirst(N startNode);
172174
198200 * }</pre>
199201 *
200202 * <p>See <a href="https://en.wikipedia.org/wiki/Depth-first_search">Wikipedia</a> for more info.
203 *
204 * @throws IllegalArgumentException if {@code startNode} is not an element of the graph
201205 */
202206 public abstract Iterable<N> depthFirstPreOrder(N startNode);
203207
229233 * }</pre>
230234 *
231235 * <p>See <a href="https://en.wikipedia.org/wiki/Depth-first_search">Wikipedia</a> for more info.
236 *
237 * @throws IllegalArgumentException if {@code startNode} is not an element of the graph
232238 */
233239 public abstract Iterable<N> depthFirstPostOrder(N startNode);
234240
241247
242248 @Override
243249 public Iterable<N> breadthFirst(final N startNode) {
250 checkNotNull(startNode);
251 checkThatNodeIsInGraph(startNode);
244252 return new Iterable<N>() {
245253 @Override
246254 public Iterator<N> iterator() {
251259
252260 @Override
253261 public Iterable<N> depthFirstPreOrder(final N startNode) {
262 checkNotNull(startNode);
263 checkThatNodeIsInGraph(startNode);
254264 return new Iterable<N>() {
255265 @Override
256266 public Iterator<N> iterator() {
261271
262272 @Override
263273 public Iterable<N> depthFirstPostOrder(final N startNode) {
274 checkNotNull(startNode);
275 checkThatNodeIsInGraph(startNode);
264276 return new Iterable<N>() {
265277 @Override
266278 public Iterator<N> iterator() {
267279 return new DepthFirstIterator(startNode, Order.POSTORDER);
268280 }
269281 };
282 }
283
284 @SuppressWarnings("CheckReturnValue")
285 private void checkThatNodeIsInGraph(N startNode) {
286 // successors() throws an IllegalArgumentException for nodes that are not an element of the
287 // graph.
288 graph.successors(startNode);
270289 }
271290
272291 private final class BreadthFirstIterator extends UnmodifiableIterator<N> {
359378
360379 @Override
361380 public Iterable<N> breadthFirst(final N startNode) {
381 checkNotNull(startNode);
382 checkThatNodeIsInTree(startNode);
362383 return new Iterable<N>() {
363384 @Override
364385 public Iterator<N> iterator() {
369390
370391 @Override
371392 public Iterable<N> depthFirstPreOrder(final N startNode) {
393 checkNotNull(startNode);
394 checkThatNodeIsInTree(startNode);
372395 return new Iterable<N>() {
373396 @Override
374397 public Iterator<N> iterator() {
379402
380403 @Override
381404 public Iterable<N> depthFirstPostOrder(final N startNode) {
405 checkNotNull(startNode);
406 checkThatNodeIsInTree(startNode);
382407 return new Iterable<N>() {
383408 @Override
384409 public Iterator<N> iterator() {
385410 return new DepthFirstPostOrderIterator(startNode);
386411 }
387412 };
413 }
414
415 @SuppressWarnings("CheckReturnValue")
416 private void checkThatNodeIsInTree(N startNode) {
417 // successors() throws an IllegalArgumentException for nodes that are not an element of the
418 // graph.
419 tree.successors(startNode);
388420 }
389421
390422 private final class BreadthFirstIterator extends UnmodifiableIterator<N> {
2222 import com.google.common.base.Ascii;
2323 import com.google.common.base.CharMatcher;
2424 import com.google.common.base.Joiner;
25 import com.google.common.base.Optional;
2526 import com.google.common.base.Splitter;
2627 import com.google.common.collect.ImmutableList;
2728 import com.google.thirdparty.publicsuffix.PublicSuffixPatterns;
29 import com.google.thirdparty.publicsuffix.PublicSuffixType;
2830 import java.util.List;
2931 import javax.annotation.Nullable;
3032
3133 /**
32 * An immutable well-formed internet domain name, such as {@code com} or {@code
33 * foo.co.uk}. Only syntactic analysis is performed; no DNS lookups or other network interactions
34 * take place. Thus there is no guarantee that the domain actually exists on the internet.
34 * An immutable well-formed internet domain name, such as {@code com} or {@code foo.co.uk}. Only
35 * syntactic analysis is performed; no DNS lookups or other network interactions take place. Thus
36 * there is no guarantee that the domain actually exists on the internet.
3537 *
3638 * <p>One common use of this class is to determine whether a given string is likely to represent an
3739 * addressable domain on the web -- that is, for a candidate string {@code "xxx"}, might browsing to
3941 * done by determining whether the domain ended with a {@linkplain #isPublicSuffix() public suffix}
4042 * but was not itself a public suffix. However, this test is no longer accurate. There are many
4143 * domains which are both public suffixes and addressable as hosts; {@code "uk.com"} is one example.
42 * As a result, the only useful test to determine if a domain is a plausible web host is
43 * {@link #hasPublicSuffix()}. This will return {@code true} for many domains which (currently) are
44 * not hosts, such as {@code "com"}, but given that any public suffix may become a host without
45 * warning, it is better to err on the side of permissiveness and thus avoid spurious rejection of
46 * valid sites.
44 * Using the subset of public suffixes that are {@linkplain #isRegistrySuffix() registry suffixes},
45 * one can get a better result, as only a few registry suffixes are addressable. However, the most
46 * useful test to determine if a domain is a plausible web host is {@link #hasPublicSuffix()}. This
47 * will return {@code true} for many domains which (currently) are not hosts, such as {@code "com"},
48 * but given that any public suffix may become a host without warning, it is better to err on the
49 * side of permissiveness and thus avoid spurious rejection of valid sites. Of course, to actually
50 * determine addressability of any host, clients of this class will need to perform their own DNS
51 * lookups.
4752 *
4853 * <p>During construction, names are normalized in two ways:
4954 *
5055 * <ol>
51 * <li>ASCII uppercase characters are converted to lowercase.
52 * <li>Unicode dot separators other than the ASCII period ({@code '.'}) are converted to the ASCII
53 * period.
56 * <li>ASCII uppercase characters are converted to lowercase.
57 * <li>Unicode dot separators other than the ASCII period ({@code '.'}) are converted to the ASCII
58 * period.
5459 * </ol>
5560 *
5661 * <p>The normalized values will be returned from {@link #toString()} and {@link #parts()}, and will
5762 * be reflected in the result of {@link #equals(Object)}.
5863 *
5964 * <p><a href="http://en.wikipedia.org/wiki/Internationalized_domain_name">Internationalized domain
60 * names</a> such as {@code 网络.cn} are supported, as are the equivalent
61 * <a href="http://en.wikipedia.org/wiki/Internationalized_domain_name">IDNA Punycode-encoded</a>
65 * names</a> such as {@code 网络.cn} are supported, as are the equivalent <a
66 * href="http://en.wikipedia.org/wiki/Internationalized_domain_name">IDNA Punycode-encoded</a>
6267 * versions.
6368 *
6469 * @author Craig Berry
7378 private static final Joiner DOT_JOINER = Joiner.on('.');
7479
7580 /**
76 * Value of {@link #publicSuffixIndex} which indicates that no public suffix was found.
77 */
78 private static final int NO_PUBLIC_SUFFIX_FOUND = -1;
81 * Value of {@link #publicSuffixIndex} or {@link #registrySuffixIndex} which indicates that no
82 * relevant suffix was found.
83 */
84 private static final int NO_SUFFIX_FOUND = -1;
7985
8086 private static final String DOT_REGEX = "\\.";
8187
111117
112118 /**
113119 * The index in the {@link #parts()} list at which the public suffix begins. For example, for the
114 * domain name {@code www.google.co.uk}, the value would be 2 (the index of the {@code co} part).
115 * The value is negative (specifically, {@link #NO_PUBLIC_SUFFIX_FOUND}) if no public suffix was
116 * found.
120 * domain name {@code myblog.blogspot.co.uk}, the value would be 1 (the index of the {@code
121 * blogspot} part). The value is negative (specifically, {@link #NO_SUFFIX_FOUND}) if no public
122 * suffix was found.
117123 */
118124 private final int publicSuffixIndex;
125
126 /**
127 * The index in the {@link #parts()} list at which the registry suffix begins. For example, for
128 * the domain name {@code myblog.blogspot.co.uk}, the value would be 2 (the index of the {@code
129 * co} part). The value is negative (specifically, {@link #NO_SUFFIX_FOUND}) if no registry suffix
130 * was found.
131 */
132 private final int registrySuffixIndex;
119133
120134 /**
121135 * Constructor used to implement {@link #from(String)}, and from subclasses.
139153 checkArgument(parts.size() <= MAX_PARTS, "Domain has too many parts: '%s'", name);
140154 checkArgument(validateSyntax(parts), "Not a valid domain name: '%s'", name);
141155
142 this.publicSuffixIndex = findPublicSuffix();
143 }
144
145 /**
146 * Returns the index of the leftmost part of the public suffix, or -1 if not found. Note that the
147 * value defined as the "public suffix" may not be a public suffix according to
148 * {@link #isPublicSuffix()} if the domain ends with an excluded domain pattern such as
149 * {@code "nhs.uk"}.
150 */
151 private int findPublicSuffix() {
156 this.publicSuffixIndex = findSuffixOfType(Optional.<PublicSuffixType>absent());
157 this.registrySuffixIndex = findSuffixOfType(Optional.of(PublicSuffixType.REGISTRY));
158 }
159
160 /**
161 * Returns the index of the leftmost part of the suffix, or -1 if not found. Note that the value
162 * defined as a suffix may not produce {@code true} results from {@link #isPublicSuffix()} or
163 * {@link #isRegistrySuffix()} if the domain ends with an excluded domain pattern such as {@code
164 * "nhs.uk"}.
165 *
166 * <p>If a {@code desiredType} is specified, this method only finds suffixes of the given type.
167 * Otherwise, it finds the first suffix of any type.
168 */
169 private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
152170 final int partsSize = parts.size();
153171
154172 for (int i = 0; i < partsSize; i++) {
155173 String ancestorName = DOT_JOINER.join(parts.subList(i, partsSize));
156174
157 if (PublicSuffixPatterns.EXACT.containsKey(ancestorName)) {
175 if (matchesType(
176 desiredType, Optional.fromNullable(PublicSuffixPatterns.EXACT.get(ancestorName)))) {
158177 return i;
159178 }
160179
165184 return i + 1;
166185 }
167186
168 if (matchesWildcardPublicSuffix(ancestorName)) {
187 if (matchesWildcardSuffixType(desiredType, ancestorName)) {
169188 return i;
170189 }
171190 }
172191
173 return NO_PUBLIC_SUFFIX_FOUND;
192 return NO_SUFFIX_FOUND;
174193 }
175194
176195 /**
177196 * Returns an instance of {@link InternetDomainName} after lenient validation. Specifically,
178197 * validation against <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>
179 * ("Internationalizing Domain Names in Applications") is skipped, while validation against
180 * <a href="http://www.ietf.org/rfc/rfc1035.txt">RFC 1035</a> is relaxed in the following ways:
198 * ("Internationalizing Domain Names in Applications") is skipped, while validation against <a
199 * href="http://www.ietf.org/rfc/rfc1035.txt">RFC 1035</a> is relaxed in the following ways:
200 *
181201 * <ul>
182 * <li>Any part containing non-ASCII characters is considered valid.
183 * <li>Underscores ('_') are permitted wherever dashes ('-') are permitted.
184 * <li>Parts other than the final part may start with a digit, as mandated by
185 * <a href="https://tools.ietf.org/html/rfc1123#section-2">RFC 1123</a>.
202 * <li>Any part containing non-ASCII characters is considered valid.
203 * <li>Underscores ('_') are permitted wherever dashes ('-') are permitted.
204 * <li>Parts other than the final part may start with a digit, as mandated by <a
205 * href="https://tools.ietf.org/html/rfc1123#section-2">RFC 1123</a>.
186206 * </ul>
187207 *
188208 *
189209 * @param domain A domain name (not IP address)
190 * @throws IllegalArgumentException if {@code name} is not syntactically valid according to
191 * {@link #isValid}
210 * @throws IllegalArgumentException if {@code name} is not syntactically valid according to {@link
211 * #isValid}
192212 * @since 10.0 (previously named {@code fromLenient})
193213 */
194214 public static InternetDomainName from(String domain) {
291311 /**
292312 * Indicates whether this domain name represents a <i>public suffix</i>, as defined by the Mozilla
293313 * Foundation's <a href="http://publicsuffix.org/">Public Suffix List</a> (PSL). A public suffix
294 * is one under which Internet users can directly register names, such as {@code com},
295 * {@code co.uk} or {@code pvt.k12.wy.us}. Examples of domain names that are <i>not</i> public
296 * suffixes include {@code google}, {@code google.com} and {@code foo.co.uk}.
314 * is one under which Internet users can directly register names, such as {@code com}, {@code
315 * co.uk} or {@code pvt.k12.wy.us}. Examples of domain names that are <i>not</i> public suffixes
316 * include {@code google.com}, {@code foo.co.uk}, and {@code myblog.blogspot.com}.
317 *
318 * <p>Public suffixes are a proper superset of {@linkplain #isRegistrySuffix() registry suffixes}.
319 * The list of public suffixes additionally contains privately owned domain names under which
320 * Internet users can register subdomains. An example of a public suffix that is not a registry
321 * suffix is {@code blogspot.com}. Note that it is true that all public suffixes <i>have</i>
322 * registry suffixes, since domain name registries collectively control all internet domain names.
323 *
324 * <p>For considerations on whether the public suffix or registry suffix designation is more
325 * suitable for your application, see <a
326 * href="https://github.com/google/guava/wiki/InternetDomainNameExplained">this article</a>.
297327 *
298328 * @return {@code true} if this domain name appears exactly on the public suffix list
299329 * @since 6.0
304334
305335 /**
306336 * Indicates whether this domain name ends in a {@linkplain #isPublicSuffix() public suffix},
307 * including if it is a public suffix itself. For example, returns {@code true} for
308 * {@code www.google.com}, {@code foo.co.uk} and {@code com}, but not for {@code google} or
309 * {@code google.foo}. This is the recommended method for determining whether a domain is
310 * potentially an addressable host.
337 * including if it is a public suffix itself. For example, returns {@code true} for {@code
338 * www.google.com}, {@code foo.co.uk} and {@code com}, but not for {@code invalid} or {@code
339 * google.invalid}. This is the recommended method for determining whether a domain is potentially
340 * an addressable host.
341 *
342 * <p>Note that this method is equivalent to {@link #hasRegistrySuffix()} because all registry
343 * suffixes are public suffixes <i>and</i> all public suffixes have registry suffixes.
311344 *
312345 * @since 6.0
313346 */
314347 public boolean hasPublicSuffix() {
315 return publicSuffixIndex != NO_PUBLIC_SUFFIX_FOUND;
348 return publicSuffixIndex != NO_SUFFIX_FOUND;
316349 }
317350
318351 /**
327360
328361 /**
329362 * Indicates whether this domain name ends in a {@linkplain #isPublicSuffix() public suffix},
330 * while not being a public suffix itself. For example, returns {@code true} for
331 * {@code www.google.com}, {@code foo.co.uk} and {@code bar.ca.us}, but not for {@code google},
332 * {@code com}, or {@code
333 * google.foo}.
334 *
335 * <p><b>Warning:</b> a {@code false} result from this method does not imply that the domain does
336 * not represent an addressable host, as many public suffixes are also addressable hosts. Use
337 * {@link #hasPublicSuffix()} for that test.
363 * while not being a public suffix itself. For example, returns {@code true} for {@code
364 * www.google.com}, {@code foo.co.uk} and {@code myblog.blogspot.com}, but not for {@code com},
365 * {@code co.uk}, {@code google.invalid}, or {@code blogspot.com}.
338366 *
339367 * <p>This method can be used to determine whether it will probably be possible to set cookies on
340368 * the domain, though even that depends on individual browsers' implementations of cookie
348376
349377 /**
350378 * Indicates whether this domain name is composed of exactly one subdomain component followed by a
351 * {@linkplain #isPublicSuffix() public suffix}. For example, returns {@code true} for
352 * {@code google.com} and {@code foo.co.uk}, but not for {@code www.google.com} or {@code co.uk}.
353 *
354 * <p><b>Warning:</b> A {@code true} result from this method does not imply that the domain is at
355 * the highest level which is addressable as a host, as many public suffixes are also addressable
356 * hosts. For example, the domain {@code bar.uk.com} has a public suffix of {@code uk.com}, so it
357 * would return {@code true} from this method. But {@code uk.com} is itself an addressable host.
379 * {@linkplain #isPublicSuffix() public suffix}. For example, returns {@code true} for {@code
380 * google.com} {@code foo.co.uk}, and {@code myblog.blogspot.com}, but not for {@code
381 * www.google.com}, {@code co.uk}, or {@code blogspot.com}.
358382 *
359383 * <p>This method can be used to determine whether a domain is probably the highest level for
360384 * which cookies may be set, though even that depends on individual browsers' implementations of
367391 }
368392
369393 /**
370 * Returns the portion of this domain name that is one level beneath the public suffix. For
371 * example, for {@code x.adwords.google.co.uk} it returns {@code google.co.uk}, since
372 * {@code co.uk} is a public suffix.
394 * Returns the portion of this domain name that is one level beneath the {@linkplain
395 * #isPublicSuffix() public suffix}. For example, for {@code x.adwords.google.co.uk} it returns
396 * {@code google.co.uk}, since {@code co.uk} is a public suffix. Similarly, for {@code
397 * myblog.blogspot.com} it returns the same domain, {@code myblog.blogspot.com}, since {@code
398 * blogspot.com} is a public suffix.
373399 *
374400 * <p>If {@link #isTopPrivateDomain()} is true, the current domain name instance is returned.
375 *
376 * <p>This method should not be used to determine the topmost parent domain which is addressable
377 * as a host, as many public suffixes are also addressable hosts. For example, the domain
378 * {@code foo.bar.uk.com} has a public suffix of {@code uk.com}, so it would return
379 * {@code bar.uk.com} from this method. But {@code uk.com} is itself an addressable host.
380401 *
381402 * <p>This method can be used to determine the probable highest level parent domain for which
382403 * cookies may be set, though even that depends on individual browsers' implementations of cookie
391412 }
392413 checkState(isUnderPublicSuffix(), "Not under a public suffix: %s", name);
393414 return ancestor(publicSuffixIndex - 1);
415 }
416
417 /**
418 * Indicates whether this domain name represents a <i>registry suffix</i>, as defined by a subset
419 * of the Mozilla Foundation's <a href="http://publicsuffix.org/">Public Suffix List</a> (PSL). A
420 * registry suffix is one under which Internet users can directly register names via a domain name
421 * registrar, and have such registrations lawfully protected by internet-governing bodies such as
422 * ICANN. Examples of registry suffixes include {@code com}, {@code co.uk}, and {@code
423 * pvt.k12.wy.us}. Examples of domain names that are <i>not</i> registry suffixes include {@code
424 * google.com} and {@code foo.co.uk}.
425 *
426 * <p>Registry suffixes are a proper subset of {@linkplain #isPublicSuffix() public suffixes}. The
427 * list of public suffixes additionally contains privately owned domain names under which Internet
428 * users can register subdomains. An example of a public suffix that is not a registry suffix is
429 * {@code blogspot.com}. Note that it is true that all public suffixes <i>have</i> registry
430 * suffixes, since domain name registries collectively control all internet domain names.
431 *
432 * <p>For considerations on whether the public suffix or registry suffix designation is more
433 * suitable for your application, see <a
434 * href="https://github.com/google/guava/wiki/InternetDomainNameExplained">this article</a>.
435 *
436 * @return {@code true} if this domain name appears exactly on the public suffix list as part of
437 * the registry suffix section (labelled "ICANN").
438 * @since 23.3
439 */
440 public boolean isRegistrySuffix() {
441 return registrySuffixIndex == 0;
442 }
443
444 /**
445 * Indicates whether this domain name ends in a {@linkplain #isRegistrySuffix() registry suffix},
446 * including if it is a registry suffix itself. For example, returns {@code true} for {@code
447 * www.google.com}, {@code foo.co.uk} and {@code com}, but not for {@code invalid} or {@code
448 * google.invalid}.
449 *
450 * <p>Note that this method is equivalent to {@link #hasPublicSuffix()} because all registry
451 * suffixes are public suffixes <i>and</i> all public suffixes have registry suffixes.
452 *
453 * @since 23.3
454 */
455 public boolean hasRegistrySuffix() {
456 return registrySuffixIndex != NO_SUFFIX_FOUND;
457 }
458
459 /**
460 * Returns the {@linkplain #isRegistrySuffix() registry suffix} portion of the domain name, or
461 * {@code null} if no registry suffix is present.
462 *
463 * @since 23.3
464 */
465 public InternetDomainName registrySuffix() {
466 return hasRegistrySuffix() ? ancestor(registrySuffixIndex) : null;
467 }
468
469 /**
470 * Indicates whether this domain name ends in a {@linkplain #isRegistrySuffix() registry suffix},
471 * while not being a registry suffix itself. For example, returns {@code true} for {@code
472 * www.google.com}, {@code foo.co.uk} and {@code blogspot.com}, but not for {@code com}, {@code
473 * co.uk}, or {@code google.invalid}.
474 *
475 * @since 23.3
476 */
477 public boolean isUnderRegistrySuffix() {
478 return registrySuffixIndex > 0;
479 }
480
481 /**
482 * Indicates whether this domain name is composed of exactly one subdomain component followed by a
483 * {@linkplain #isRegistrySuffix() registry suffix}. For example, returns {@code true} for {@code
484 * google.com}, {@code foo.co.uk}, and {@code blogspot.com}, but not for {@code www.google.com},
485 * {@code co.uk}, or {@code myblog.blogspot.com}.
486 *
487 * <p><b>Warning:</b> This method should not be used to determine the probable highest level
488 * parent domain for which cookies may be set. Use {@link #topPrivateDomain()} for that purpose.
489 *
490 * @since 23.3
491 */
492 public boolean isTopDomainUnderRegistrySuffix() {
493 return registrySuffixIndex == 1;
494 }
495
496 /**
497 * Returns the portion of this domain name that is one level beneath the {@linkplain
498 * #isRegistrySuffix() registry suffix}. For example, for {@code x.adwords.google.co.uk} it
499 * returns {@code google.co.uk}, since {@code co.uk} is a registry suffix. Similarly, for {@code
500 * myblog.blogspot.com} it returns {@code blogspot.com}, since {@code com} is a registry suffix.
501 *
502 * <p>If {@link #isTopDomainUnderRegistrySuffix()} is true, the current domain name instance is
503 * returned.
504 *
505 * <p><b>Warning:</b> This method should not be used to determine whether a domain is probably the
506 * highest level for which cookies may be set. Use {@link #isTopPrivateDomain()} for that purpose.
507 *
508 * @throws IllegalStateException if this domain does not end with a registry suffix
509 * @since 23.3
510 */
511 public InternetDomainName topDomainUnderRegistrySuffix() {
512 if (isTopDomainUnderRegistrySuffix()) {
513 return this;
514 }
515 checkState(isUnderRegistrySuffix(), "Not under a registry suffix: %s", name);
516 return ancestor(registrySuffixIndex - 1);
394517 }
395518
396519 /**
468591 }
469592
470593 /**
471 * Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})?
472 */
473 private static boolean matchesWildcardPublicSuffix(String domain) {
594 * Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})? If a {@code
595 * desiredType} is specified, the wildcard pattern must also match that type.
596 */
597 private static boolean matchesWildcardSuffixType(
598 Optional<PublicSuffixType> desiredType, String domain) {
474599 final String[] pieces = domain.split(DOT_REGEX, 2);
475 return pieces.length == 2 && PublicSuffixPatterns.UNDER.containsKey(pieces[1]);
600 return pieces.length == 2
601 && matchesType(
602 desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(pieces[1])));
603 }
604
605 /**
606 * If a {@code desiredType} is specified, returns true only if the {@code actualType} is
607 * identical. Otherwise, returns true as long as {@code actualType} is present.
608 */
609 private static boolean matchesType(
610 Optional<PublicSuffixType> desiredType, Optional<PublicSuffixType> actualType) {
611 return desiredType.isPresent() ? desiredType.equals(actualType) : actualType.isPresent();
476612 }
477613
478614 /**
0 /*
1 * Copyright (C) 2017 The Guava Authors
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4 * in compliance with the License. You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software distributed under the License
9 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10 * or implied. See the License for the specific language governing permissions and limitations under
11 * the License.
12 */
13
14 package com.google.common.util.concurrent;
15
16 import java.util.Date;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.locks.Condition;
19
20 /** Forwarding wrapper around a {@code Condition}. */
21 abstract class ForwardingCondition implements Condition {
22 abstract Condition delegate();
23
24 @Override
25 public void await() throws InterruptedException {
26 delegate().await();
27 }
28
29 @Override
30 public void awaitUninterruptibly() {
31 delegate().awaitUninterruptibly();
32 }
33
34 @Override
35 public long awaitNanos(long nanosTimeout) throws InterruptedException {
36 return delegate().awaitNanos(nanosTimeout);
37 }
38
39 @Override
40 public boolean await(long time, TimeUnit unit) throws InterruptedException {
41 return delegate().await(time, unit);
42 }
43
44 @Override
45 public boolean awaitUntil(Date deadline) throws InterruptedException {
46 return delegate().awaitUntil(deadline);
47 }
48
49 @Override
50 public void signal() {
51 delegate().signal();
52 }
53
54 @Override
55 public void signalAll() {
56 delegate().signalAll();
57 }
58 }
0 /*
1 * Copyright (C) 2017 The Guava Authors
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4 * in compliance with the License. You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software distributed under the License
9 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10 * or implied. See the License for the specific language governing permissions and limitations under
11 * the License.
12 */
13
14 package com.google.common.util.concurrent;
15
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.locks.Condition;
18 import java.util.concurrent.locks.Lock;
19
20 /** Forwarding wrapper around a {@code Lock}. */
21 abstract class ForwardingLock implements Lock {
22 abstract Lock delegate();
23
24 @Override
25 public void lock() {
26 delegate().lock();
27 }
28
29 @Override
30 public void lockInterruptibly() throws InterruptedException {
31 delegate().lockInterruptibly();
32 }
33
34 @Override
35 public boolean tryLock() {
36 return delegate().tryLock();
37 }
38
39 @Override
40 public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
41 return delegate().tryLock(time, unit);
42 }
43
44 @Override
45 public void unlock() {
46 delegate().unlock();
47 }
48
49 @Override
50 public Condition newCondition() {
51 return delegate().newCondition();
52 }
53 }
6161 Collections.synchronizedList(new ArrayList<PerListenerQueue<L>>());
6262
6363 /** Method reference-compatible listener event. */
64 public interface Event<L> {
64 interface Event<L> {
6565 /** Call a method on the listener. */
6666 void call(L listener);
6767 }
420420 * trucking. If an {@code Error} is thrown, the error will propagate and execution will stop until
421421 * the next time a task is submitted.
422422 *
423 * @deprecated Use {@link #newSequentialExecutor}. This method is scheduled for removal in
424 * January 2018.
423425 * @since 23.1
426 */
427 @Beta
428 @Deprecated
429 @GwtIncompatible
430 public static Executor sequentialExecutor(Executor delegate) {
431 return new SequentialExecutor(delegate);
432 }
433
434 /**
435 * Returns an {@link Executor} that runs each task executed sequentially, such that no two tasks
436 * are running concurrently.
437 *
438 * <p>The executor uses {@code delegate} in order to {@link Executor#execute execute} each task in
439 * turn, and does not create any threads of its own.
440 *
441 * <p>After execution starts on the {@code delegate} {@link Executor}, tasks are polled and
442 * executed from the queue until there are no more tasks. The thread will not be released until
443 * there are no more tasks to run.
444 *
445 * <p>If a task is {@linkplain Thread#interrupt interrupted}, execution of subsequent tasks
446 * continues. {@code RuntimeException}s thrown by tasks are simply logged and the executor keeps
447 * trucking. If an {@code Error} is thrown, the error will propagate and execution will stop until
448 * the next time a task is submitted.
449 *
450 * @since 23.3 (since 23.1 as {@link #sequentialExecutor(Executor)})
424451 */
425452 @Beta
426453 @GwtIncompatible
427 public static Executor sequentialExecutor(Executor delegate) {
454 public static Executor newSequentialExecutor(Executor delegate) {
428455 return new SequentialExecutor(delegate);
429456 }
430457
5050
5151 private final QueueWorker worker = new QueueWorker();
5252
53 /** Use {@link MoreExecutors#sequentialExecutor} */
53 /** Use {@link MoreExecutors#newSequentialExecutor} */
5454 SequentialExecutor(Executor executor) {
5555 this.executor = Preconditions.checkNotNull(executor);
5656 }
3434 import java.util.concurrent.ConcurrentMap;
3535 import java.util.concurrent.Semaphore;
3636 import java.util.concurrent.atomic.AtomicReferenceArray;
37 import java.util.concurrent.locks.Condition;
3738 import java.util.concurrent.locks.Lock;
3839 import java.util.concurrent.locks.ReadWriteLock;
3940 import java.util.concurrent.locks.ReentrantLock;
5051 * <p>The guarantee provided by this class is that equal keys lead to the same lock (or semaphore),
5152 * i.e. {@code if (key1.equals(key2))} then {@code striped.get(key1) == striped.get(key2)} (assuming
5253 * {@link Object#hashCode()} is correctly implemented for the keys). Note that if {@code key1} is
53 * <strong>not</strong> equal to {@code key2}, it is <strong>not</strong> guaranteed that
54 * {@code striped.get(key1) != striped.get(key2)}; the elements might nevertheless be mapped to the
55 * same lock. The lower the number of stripes, the higher the probability of this happening.
54 * <strong>not</strong> equal to {@code key2}, it is <strong>not</strong> guaranteed that {@code
55 * striped.get(key1) != striped.get(key2)}; the elements might nevertheless be mapped to the same
56 * lock. The lower the number of stripes, the higher the probability of this happening.
5657 *
5758 * <p>There are three flavors of this class: {@code Striped<Lock>}, {@code Striped<Semaphore>}, and
58 * {@code Striped<ReadWriteLock>}. For each type, two implementations are offered:
59 * {@linkplain #lock(int) strong} and {@linkplain #lazyWeakLock(int) weak} {@code Striped<Lock>},
60 * {@linkplain #semaphore(int, int) strong} and {@linkplain #lazyWeakSemaphore(int, int) weak}
61 * {@code Striped<Semaphore>}, and {@linkplain #readWriteLock(int) strong} and
62 * {@linkplain #lazyWeakReadWriteLock(int) weak} {@code Striped<ReadWriteLock>}. <i>Strong</i> means
63 * that all stripes (locks/semaphores) are initialized eagerly, and are not reclaimed unless
64 * {@code Striped} itself is reclaimable. <i>Weak</i> means that locks/semaphores are created
65 * lazily, and they are allowed to be reclaimed if nobody is holding on to them. This is useful, for
66 * example, if one wants to create a {@code
67 * Striped<Lock>} of many locks, but worries that in most cases only a small portion of these would
68 * be in use.
59 * {@code Striped<ReadWriteLock>}. For each type, two implementations are offered: {@linkplain
60 * #lock(int) strong} and {@linkplain #lazyWeakLock(int) weak} {@code Striped<Lock>}, {@linkplain
61 * #semaphore(int, int) strong} and {@linkplain #lazyWeakSemaphore(int, int) weak} {@code
62 * Striped<Semaphore>}, and {@linkplain #readWriteLock(int) strong} and {@linkplain
63 * #lazyWeakReadWriteLock(int) weak} {@code Striped<ReadWriteLock>}. <i>Strong</i> means that all
64 * stripes (locks/semaphores) are initialized eagerly, and are not reclaimed unless {@code Striped}
65 * itself is reclaimable. <i>Weak</i> means that locks/semaphores are created lazily, and they are
66 * allowed to be reclaimed if nobody is holding on to them. This is useful, for example, if one
67 * wants to create a {@code Striped<Lock>} of many locks, but worries that in most cases only a
68 * small portion of these would be in use.
6969 *
7070 * <p>Prior to this class, one might be tempted to use {@code Map<K, Lock>}, where {@code K}
7171 * represents the task. This maximizes concurrency by having each unique key mapped to a unique
9292 private Striped() {}
9393
9494 /**
95 * Returns the stripe that corresponds to the passed key. It is always guaranteed that if
96 * {@code key1.equals(key2)}, then {@code get(key1) == get(key2)}.
95 * Returns the stripe that corresponds to the passed key. It is always guaranteed that if {@code
96 * key1.equals(key2)}, then {@code get(key1) == get(key2)}.
9797 *
9898 * @param key an arbitrary, non-null key
9999 * @return the stripe that the passed key corresponds to
114114 */
115115 abstract int indexFor(Object key);
116116
117 /**
118 * Returns the total number of stripes in this instance.
119 */
117 /** Returns the total number of stripes in this instance. */
120118 public abstract int size();
121119
122120 /**
123 * Returns the stripes that correspond to the passed objects, in ascending (as per
124 * {@link #getAt(int)}) order. Thus, threads that use the stripes in the order returned by this
125 * method are guaranteed to not deadlock each other.
126 *
127 * <p>It should be noted that using a {@code Striped<L>} with relatively few stripes, and
128 * {@code bulkGet(keys)} with a relative large number of keys can cause an excessive number of
129 * shared stripes (much like the birthday paradox, where much fewer than anticipated birthdays are
130 * needed for a pair of them to match). Please consider carefully the implications of the number
131 * of stripes, the intended concurrency level, and the typical number of keys used in a
132 * {@code bulkGet(keys)} operation. See <a href="http://www.mathpages.com/home/kmath199.htm">Balls
133 * in Bins model</a> for mathematical formulas that can be used to estimate the probability of
121 * Returns the stripes that correspond to the passed objects, in ascending (as per {@link
122 * #getAt(int)}) order. Thus, threads that use the stripes in the order returned by this method
123 * are guaranteed to not deadlock each other.
124 *
125 * <p>It should be noted that using a {@code Striped<L>} with relatively few stripes, and {@code
126 * bulkGet(keys)} with a relative large number of keys can cause an excessive number of shared
127 * stripes (much like the birthday paradox, where much fewer than anticipated birthdays are needed
128 * for a pair of them to match). Please consider carefully the implications of the number of
129 * stripes, the intended concurrency level, and the typical number of keys used in a {@code
130 * bulkGet(keys)} operation. See <a href="http://www.mathpages.com/home/kmath199.htm">Balls in
131 * Bins model</a> for mathematical formulas that can be used to estimate the probability of
134132 * collisions.
135133 *
136134 * @param keys arbitrary non-null keys
283281 * @return a new {@code Striped<ReadWriteLock>}
284282 */
285283 public static Striped<ReadWriteLock> lazyWeakReadWriteLock(int stripes) {
286 return lazy(stripes, READ_WRITE_LOCK_SUPPLIER);
287 }
288
289 // ReentrantReadWriteLock is large enough to make padding probably unnecessary
284 return lazy(stripes, WEAK_SAFE_READ_WRITE_LOCK_SUPPLIER);
285 }
286
290287 private static final Supplier<ReadWriteLock> READ_WRITE_LOCK_SUPPLIER =
291288 new Supplier<ReadWriteLock>() {
292289 @Override
294291 return new ReentrantReadWriteLock();
295292 }
296293 };
294
295 private static final Supplier<ReadWriteLock> WEAK_SAFE_READ_WRITE_LOCK_SUPPLIER =
296 new Supplier<ReadWriteLock>() {
297 @Override
298 public ReadWriteLock get() {
299 return new WeakSafeReadWriteLock();
300 }
301 };
302
303 /**
304 * ReadWriteLock implementation whose read and write locks retain a reference back to this lock.
305 * Otherwise, a reference to just the read lock or just the write lock would not suffice to ensure
306 * the {@code ReadWriteLock} is retained.
307 */
308 private static final class WeakSafeReadWriteLock implements ReadWriteLock {
309 private final ReadWriteLock delegate;
310
311 WeakSafeReadWriteLock() {
312 this.delegate = new ReentrantReadWriteLock();
313 }
314
315 @Override
316 public Lock readLock() {
317 return new WeakSafeLock(delegate.readLock(), this);
318 }
319
320 @Override
321 public Lock writeLock() {
322 return new WeakSafeLock(delegate.writeLock(), this);
323 }
324 }
325
326 /** Lock object that ensures a strong reference is retained to a specified object. */
327 private static final class WeakSafeLock extends ForwardingLock {
328 private final Lock delegate;
329
330 @SuppressWarnings("unused")
331 private final WeakSafeReadWriteLock strongReference;
332
333 WeakSafeLock(Lock delegate, WeakSafeReadWriteLock strongReference) {
334 this.delegate = delegate;
335 this.strongReference = strongReference;
336 }
337
338 @Override
339 Lock delegate() {
340 return delegate;
341 }
342
343 @Override
344 public Condition newCondition() {
345 return new WeakSafeCondition(delegate.newCondition(), strongReference);
346 }
347 }
348
349 /** Condition object that ensures a strong reference is retained to a specified object. */
350 private static final class WeakSafeCondition extends ForwardingCondition {
351 private final Condition delegate;
352
353 @SuppressWarnings("unused")
354 private final WeakSafeReadWriteLock strongReference;
355
356 WeakSafeCondition(Condition delegate, WeakSafeReadWriteLock strongReference) {
357 this.delegate = delegate;
358 this.strongReference = strongReference;
359 }
360
361 @Override
362 Condition delegate() {
363 return delegate;
364 }
365 }
297366
298367 private abstract static class PowerOfTwoStriped<L> extends Striped<L> {
299368 /** Capacity (power of two) minus one, for fast mod evaluation */
456525 }
457526 }
458527
459 /**
460 * A bit mask were all bits are set.
461 */
528 /** A bit mask were all bits are set. */
462529 private static final int ALL_SET = ~0;
463530
464531 private static int ceilToPowerOfTwo(int x) {
1313
1414 package com.google.thirdparty.publicsuffix;
1515
16 import com.google.common.annotations.Beta;
1617 import com.google.common.annotations.GwtCompatible;
1718
1819 /**
19 * Specifies the type of a top-level domain definition.
20 * <b>Do not use this class directly. For access to public-suffix information,
21 * use {@link com.google.common.net.InternetDomainName}.</b>
22 *
23 * <p>Specifies the type of a top-level domain definition.
24 *
25 * @since 23.3
2026 */
27 @Beta
2128 @GwtCompatible
22 enum PublicSuffixType {
29 public enum PublicSuffixType {
2330
24 /** private definition of a top-level domain */
31 /** Public suffix that is provided by a private company, e.g. "blogspot.com" */
2532 PRIVATE(':', ','),
26 /** ICANN definition of a top-level domain */
27 ICANN('!', '?');
33 /** Public suffix that is backed by an ICANN-style domain name registry */
34 REGISTRY('!', '?');
2835
2936 /** The character used for an inner node in the trie encoding */
3037 private final char innerNodeCode;
5663 }
5764
5865 static PublicSuffixType fromIsPrivate(boolean isPrivate) {
59 return isPrivate ? PRIVATE : ICANN;
66 return isPrivate ? PRIVATE : REGISTRY;
6067 }
6168 }
1717 import com.google.common.base.Joiner;
1818 import com.google.common.collect.ImmutableMap;
1919 import com.google.common.collect.Lists;
20
2120 import java.util.List;
2221
2322 /**
7372 stack.add(0, reverse(encoded.subSequence(0, idx)));
7473
7574 if (c == '!' || c == '?' || c == ':' || c == ',') {
76 // '!' represents an interior node that represents an ICANN entry in the map.
77 // '?' represents a leaf node, which represents an ICANN entry in map.
75 // '!' represents an interior node that represents a REGISTRY entry in the map.
76 // '?' represents a leaf node, which represents a REGISTRY entry in map.
7877 // ':' represents an interior node that represents a private entry in the map
7978 // ',' represents a leaf node, which represents a private entry in the map.
8079 String domain = PREFIX_JOINER.join(stack);
44 <parent>
55 <groupId>com.google.guava</groupId>
66 <artifactId>guava-parent</artifactId>
7 <version>23.2-jre</version>
7 <version>23.3-jre</version>
88 </parent>
99 <artifactId>guava-gwt</artifactId>
1010 <name>Guava GWT compatible libs</name>
2424 <inherits name="com.google.common.escape.Escape"/>
2525
2626 <inherits name="com.google.thirdparty.publicsuffix.PublicSuffixPatterns"/>
27
28 <inherits name="com.google.thirdparty.publicsuffix.PublicSuffixType"/>
2729
2830 <inherits name="com.google.gwt.core.Core"/>
2931
8787 testCase.testCharacterSplitWithMatcherDelimiter();
8888 }
8989
90 public void testCharacterSplitWithMultitpleLetters() throws Exception {
91 com.google.common.base.SplitterTest testCase = new com.google.common.base.SplitterTest();
92 testCase.testCharacterSplitWithMultitpleLetters();
90 public void testCharacterSplitWithMultipleLetters() throws Exception {
91 com.google.common.base.SplitterTest testCase = new com.google.common.base.SplitterTest();
92 testCase.testCharacterSplitWithMultipleLetters();
9393 }
9494
9595 public void testCharacterSplitWithTrailingDelimiter() throws Exception {
2727 testCase.testEquality();
2828 }
2929
30 public void testExclusion() throws Exception {
31 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
32 testCase.testExclusion();
33 }
34
3530 public void testInvalid() throws Exception {
3631 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
3732 testCase.testInvalid();
4540 public void testIsValid() throws Exception {
4641 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
4742 testCase.testIsValid();
48 }
49
50 public void testMultipleUnders() throws Exception {
51 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
52 testCase.testMultipleUnders();
5343 }
5444
5545 public void testParent() throws Exception {
6757 testCase.testPublicSuffix();
6858 }
6959
60 public void testPublicSuffixExclusion() throws Exception {
61 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
62 testCase.testPublicSuffixExclusion();
63 }
64
65 public void testPublicSuffixMultipleUnders() throws Exception {
66 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
67 testCase.testPublicSuffixMultipleUnders();
68 }
69
70 public void testRegistrySuffix() throws Exception {
71 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
72 testCase.testRegistrySuffix();
73 }
74
75 public void testRegistrySuffixExclusion() throws Exception {
76 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
77 testCase.testRegistrySuffixExclusion();
78 }
79
80 public void testRegistrySuffixMultipleUnders() throws Exception {
81 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
82 testCase.testRegistrySuffixMultipleUnders();
83 }
84
7085 public void testToString() throws Exception {
7186 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
7287 testCase.testToString();
88 }
89
90 public void testTopDomainUnderRegistrySuffix() throws Exception {
91 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
92 testCase.testTopDomainUnderRegistrySuffix();
7393 }
7494
7595 public void testTopPrivateDomain() throws Exception {
87107 testCase.testUnderPublicSuffix();
88108 }
89109
110 public void testUnderRegistrySuffix() throws Exception {
111 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
112 testCase.testUnderRegistrySuffix();
113 }
114
115 public void testUnderTopDomainUnderRegistrySuffix() throws Exception {
116 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
117 testCase.testUnderTopDomainUnderRegistrySuffix();
118 }
119
90120 public void testValid() throws Exception {
91121 com.google.common.net.InternetDomainNameTest testCase = new com.google.common.net.InternetDomainNameTest();
92122 testCase.testValid();
44 <parent>
55 <groupId>com.google.guava</groupId>
66 <artifactId>guava-parent</artifactId>
7 <version>23.2-jre</version>
7 <version>23.3-jre</version>
88 </parent>
99 <artifactId>guava-testlib</artifactId>
1010 <name>Guava Testing Library</name>
44 <parent>
55 <groupId>com.google.guava</groupId>
66 <artifactId>guava-parent</artifactId>
7 <version>23.2-jre</version>
7 <version>23.3-jre</version>
88 </parent>
99 <artifactId>guava-tests</artifactId>
1010 <name>Guava Unit Tests</name>
149149 assertNotNull(getClass().getResource("internal/Finalizer.class"));
150150 }
151151
152 public void testFinalizeClassHasNoNestedClases() throws Exception {
152 public void testFinalizeClassHasNoNestedClasses() throws Exception {
153153 // Ensure that the Finalizer class has no nested classes.
154154 // See https://code.google.com/p/guava-libraries/issues/detail?id=1505
155155 assertEquals(Collections.emptyList(), Arrays.asList(Finalizer.class.getDeclaredClasses()));
9999 assertThat(letters).containsExactly("", "a", "b", "c").inOrder();
100100 }
101101
102 public void testCharacterSplitWithMultitpleLetters() {
102 public void testCharacterSplitWithMultipleLetters() {
103103 Iterable<String> testCharacteringMotto = Splitter.on('-').split(
104104 "Testing-rocks-Debugging-sucks");
105105 assertThat(testCharacteringMotto)
114114 createDirectedGraph("hd", "he", "hg", "da", "db", "dc", "gf");
115115
116116 /**
117 * Two disjoint tree-shaped graphs that look as follows (all edges are directed facing downwards):
118 *
119 * <pre>{@code
120 * a c
121 * | |
122 * | |
123 * b d
124 * }</pre>
125 */
126 private static final SuccessorsFunction<Character> TWO_TREES = createDirectedGraph("ab", "cd");
127
128 /**
129 * A graph consisting of a single root {@code a}:
130 *
131 * <pre>{@code
132 * a
133 * }</pre>
134 */
135 private static final SuccessorsFunction<Character> SINGLE_ROOT = createSingleRootGraph();
136
137 /**
117138 * A graph that is not a tree (for example, it has two antiparallel edge between {@code e} and
118139 * {@code f} and thus has a cycle) but is a valid input to {@link Traverser#forTree} when starting
119140 * e.g. at node {@code a} (all edges without an arrow are directed facing downwards):
198219 assertEqualCharNodes(traverser.breadthFirst('a'), "a");
199220 }
200221
222 @Test
223 public void forGraph_breadthFirst_twoTrees() {
224 Iterable<Character> result = Traverser.forGraph(TWO_TREES).breadthFirst('a');
225
226 assertEqualCharNodes(result, "ab");
227 }
228
229 @Test
230 public void forGraph_breadthFirst_singleRoot() {
231 Iterable<Character> result = Traverser.forGraph(SINGLE_ROOT).breadthFirst('a');
232
233 assertEqualCharNodes(result, "a");
234 }
235
236 @Test
237 public void forGraph_breadthFirst_emptyGraph() {
238 try {
239 Traverser.forGraph(createDirectedGraph()).breadthFirst('a');
240 fail("Expected IllegalArgumentException");
241 } catch (IllegalArgumentException expected) {
242 }
243 }
244
201245 /**
202246 * Checks that the elements of the iterable are calculated on the fly. Concretely, that means that
203247 * {@link SuccessorsFunction#successors(Object)} can only be called for a subset of all nodes.
208252 Iterable<Character> result = Traverser.forGraph(graph).breadthFirst('a');
209253
210254 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
211 assertThat(graph.requestedNodes).containsExactly('a', 'b');
255 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b');
212256
213257 // Iterate again to see if calculation is done again
214258 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
215 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b');
259 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b');
216260 }
217261
218262 @Test
269313 }
270314
271315 @Test
316 public void forGraph_depthFirstPreOrder_twoTrees() {
317 Iterable<Character> result = Traverser.forGraph(TWO_TREES).depthFirstPreOrder('a');
318
319 assertEqualCharNodes(result, "ab");
320 }
321
322 @Test
323 public void forGraph_depthFirstPreOrder_singleRoot() {
324 Iterable<Character> result = Traverser.forGraph(SINGLE_ROOT).depthFirstPreOrder('a');
325
326 assertEqualCharNodes(result, "a");
327 }
328
329 @Test
330 public void forGraph_depthFirstPreOrder_emptyGraph() {
331 try {
332 Traverser.forGraph(createDirectedGraph()).depthFirstPreOrder('a');
333 fail("Expected IllegalArgumentException");
334 } catch (IllegalArgumentException expected) {
335 }
336 }
337
338 @Test
272339 public void forGraph_depthFirstPreOrder_iterableIsLazy() {
273340 RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH);
274341 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder('a');
275342
276343 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
277 assertThat(graph.requestedNodes).containsExactly('a', 'b', 'd');
344 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'd');
278345
279346 // Iterate again to see if calculation is done again
280347 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
281 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b', 'd', 'd');
348 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'd', 'd');
282349 }
283350
284351 @Test
334401 }
335402
336403 @Test
404 public void forGraph_depthFirstPostOrder_twoTrees() {
405 Iterable<Character> result = Traverser.forGraph(TWO_TREES).depthFirstPostOrder('a');
406
407 assertEqualCharNodes(result, "ba");
408 }
409
410 @Test
411 public void forGraph_depthFirstPostOrder_singleRoot() {
412 Iterable<Character> result = Traverser.forGraph(SINGLE_ROOT).depthFirstPostOrder('a');
413
414 assertEqualCharNodes(result, "a");
415 }
416
417 @Test
418 public void forGraph_depthFirstPostOrder_emptyGraph() {
419 try {
420 Traverser.forGraph(createDirectedGraph()).depthFirstPostOrder('a');
421 fail("Expected IllegalArgumentException");
422 } catch (IllegalArgumentException expected) {
423 }
424 }
425
426 @Test
337427 public void forGraph_depthFirstPostOrder_iterableIsLazy() {
338428 RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH);
339429 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder('a');
340430
341431 assertEqualCharNodes(Iterables.limit(result, 2), "db");
342 assertThat(graph.requestedNodes).containsExactly('a', 'b', 'd');
432 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'd');
343433
344434 // Iterate again to see if calculation is done again
345435 assertEqualCharNodes(Iterables.limit(result, 2), "db");
346 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b', 'd', 'd');
436 assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'd', 'd');
347437 }
348438
349439 @Test
437527 }
438528
439529 @Test
530 public void forTree_breadthFirst_twoTrees() {
531 Iterable<Character> result = Traverser.forTree(TWO_TREES).breadthFirst('a');
532
533 assertEqualCharNodes(result, "ab");
534 }
535
536 @Test
537 public void forTree_breadthFirst_singleRoot() {
538 Iterable<Character> result = Traverser.forTree(SINGLE_ROOT).breadthFirst('a');
539
540 assertEqualCharNodes(result, "a");
541 }
542
543 @Test
544 public void forTree_breadthFirst_emptyGraph() {
545 try {
546 Traverser.forTree(createDirectedGraph()).breadthFirst('a');
547 fail("Expected IllegalArgumentException");
548 } catch (IllegalArgumentException expected) {
549 }
550 }
551
552 @Test
440553 public void forTree_breadthFirst_iterableIsLazy() {
441554 RequestSavingGraph graph = new RequestSavingGraph(TREE);
442555 Iterable<Character> result = Traverser.forGraph(graph).breadthFirst('h');
443556
444557 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
445 assertThat(graph.requestedNodes).containsExactly('h', 'd');
558 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd');
446559
447560 // Iterate again to see if calculation is done again
448561 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
449 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'd');
562 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd');
450563 }
451564
452565 @Test
477590 }
478591
479592 @Test
593 public void forTree_depthFirstPreOrder_twoTrees() {
594 Iterable<Character> result = Traverser.forTree(TWO_TREES).depthFirstPreOrder('a');
595
596 assertEqualCharNodes(result, "ab");
597 }
598
599 @Test
600 public void forTree_depthFirstPreOrder_singleRoot() {
601 Iterable<Character> result = Traverser.forTree(SINGLE_ROOT).depthFirstPreOrder('a');
602
603 assertEqualCharNodes(result, "a");
604 }
605
606 @Test
607 public void forTree_depthFirstPreOrder_emptyGraph() {
608 try {
609 Traverser.forTree(createDirectedGraph()).depthFirstPreOrder('a');
610 fail("Expected IllegalArgumentException");
611 } catch (IllegalArgumentException expected) {
612 }
613 }
614
615 @Test
480616 public void forTree_depthFirstPreOrder_iterableIsLazy() {
481617 RequestSavingGraph graph = new RequestSavingGraph(TREE);
482618 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder('h');
483619
484620 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
485 assertThat(graph.requestedNodes).containsExactly('h', 'd', 'a');
621 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'a');
486622
487623 // Iterate again to see if calculation is done again
488624 assertEqualCharNodes(Iterables.limit(result, 2), "hd");
489 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'd', 'a', 'a');
625 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd', 'a', 'a');
490626 }
491627
492628 @Test
517653 }
518654
519655 @Test
656 public void forTree_depthFirstPostOrder_twoTrees() {
657 Iterable<Character> result = Traverser.forTree(TWO_TREES).depthFirstPostOrder('a');
658
659 assertEqualCharNodes(result, "ba");
660 }
661
662 @Test
663 public void forTree_depthFirstPostOrder_singleRoot() {
664 Iterable<Character> result = Traverser.forTree(SINGLE_ROOT).depthFirstPostOrder('a');
665
666 assertEqualCharNodes(result, "a");
667 }
668
669 @Test
670 public void forTree_depthFirstPostOrder_emptyGraph() {
671 try {
672 Traverser.forTree(createDirectedGraph()).depthFirstPostOrder('a');
673 fail("Expected IllegalArgumentException");
674 } catch (IllegalArgumentException expected) {
675 }
676 }
677
678 @Test
520679 public void forTree_depthFirstPostOrder_iterableIsLazy() {
521680 RequestSavingGraph graph = new RequestSavingGraph(TREE);
522681 Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder('h');
523682
524683 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
525 assertThat(graph.requestedNodes).containsExactly('h', 'd', 'a', 'b');
684 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'a', 'b');
526685
527686 // Iterate again to see if calculation is done again
528687 assertEqualCharNodes(Iterables.limit(result, 2), "ab");
529 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'd', 'a', 'a', 'b', 'b');
688 assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd', 'a', 'a', 'b', 'b');
530689 }
531690
532691 private static SuccessorsFunction<Character> createDirectedGraph(String... edges) {
560719 return new SuccessorsFunction<Character>() {
561720 @Override
562721 public Iterable<? extends Character> successors(Character node) {
722 checkArgument(
723 graphMap.containsKey(node) || graphMap.containsValue(node),
724 "Node %s is not an element of this graph",
725 node);
563726 return Ordering.natural().immutableSortedCopy(graphMap.get(node));
564727 }
565728 };
729 }
730
731 private static ImmutableGraph<Character> createSingleRootGraph() {
732 MutableGraph<Character> graph = GraphBuilder.directed().build();
733 graph.addNode('a');
734 return ImmutableGraph.copyOf(graph);
566735 }
567736
568737 private static void assertEqualCharNodes(Iterable<Character> result, String expectedCharacters) {
5858 assertCrc(0x113fdb5c, descending);
5959 }
6060
61 public void testScsiReadCommad() {
61 public void testScsiReadCommand() {
6262 // Test SCSI read command.
6363 byte[] scsiReadCommand = new byte[] {
6464 0x01, (byte) 0xc0, 0x00, 0x00,
1919 import static com.google.common.math.MathTesting.FINITE_DOUBLE_CANDIDATES;
2020 import static com.google.common.math.MathTesting.POSITIVE_FINITE_DOUBLE_CANDIDATES;
2121
22 import java.lang.reflect.Method;
2223 import java.math.BigInteger;
2324 import junit.framework.TestCase;
2425
2829 * @author Louis Wasserman
2930 */
3031 public class DoubleUtilsTest extends TestCase {
31 @AndroidIncompatible // no FpUtils
32 public void testNextDown() {
32 @AndroidIncompatible // no FpUtils and no Math.nextDown in old versions
33 public void testNextDown() throws Exception {
34 Method jdkNextDown = getJdkNextDown();
3335 for (double d : FINITE_DOUBLE_CANDIDATES) {
34 assertEquals(Math.nextDown(d), DoubleUtils.nextDown(d));
36 assertEquals(jdkNextDown.invoke(null, d), DoubleUtils.nextDown(d));
37 }
38 }
39
40 private static Method getJdkNextDown() throws Exception {
41 try {
42 return Math.class.getMethod("nextDown", double.class);
43 } catch (NoSuchMethodException expectedBeforeJava8) {
44 return Class.forName("sun.misc.FpUtils").getMethod("nextDown", double.class);
3545 }
3646 }
3747
9595 ALMOST_TOO_MANY_LEVELS + "com",
9696 ALMOST_TOO_LONG + ".c");
9797
98 private static final ImmutableSet<String> PS = ImmutableSet.of(
99 "com",
100 "co.uk",
101 "foo.bd",
102 "xxxxxx.bd",
103 "org.mK",
104 "us",
105 "uk\uFF61com.", // Alternate dot character
106 "\u7f51\u7edc.Cn", // "网络.Cn"
107 "j\u00f8rpeland.no", // "jorpeland.no" (first o slashed)
108 "xn--jrpeland-54a.no" // IDNA (punycode) encoding of above
109 );
98 private static final ImmutableSet<String> RS =
99 ImmutableSet.of(
100 "com",
101 "co.uk",
102 "foo.bd",
103 "xxxxxx.bd",
104 "org.mK",
105 "us",
106 "co.uk.", // Trailing dot
107 "co\uFF61uk", // Alternate dot character
108 "\u7f51\u7edc.Cn", // "网络.Cn"
109 "j\u00f8rpeland.no", // "jorpeland.no" (first o slashed)
110 "xn--jrpeland-54a.no"); // IDNA (punycode) encoding of above
111
112 private static final ImmutableSet<String> PS_NOT_RS =
113 ImmutableSet.of("blogspot.com", "blogspot.co.uk", "uk.com");
114
115 private static final ImmutableSet<String> PS =
116 ImmutableSet.<String>builder().addAll(RS).addAll(PS_NOT_RS).build();
110117
111118 private static final ImmutableSet<String> NO_PS = ImmutableSet.of(
112119 "www", "foo.ihopethiswillneverbeapublicsuffix", "x.y.z");
113120
114 private static final ImmutableSet<String> NON_PS = ImmutableSet.of(
115 "foo.bar.com", "foo.ca", "foo.bar.ca",
116 "foo.bar.co.il", "state.CA.us", "www.state.pa.us", "pvt.k12.ca.us",
117 "www.google.com", "www4.yahoo.co.uk", "home.netscape.com",
118 "web.MIT.edu", "foo.eDu.au", "utenti.blah.IT", "dominio.com.co");
121 /**
122 * Having a public suffix is equivalent to having a registry suffix, because all registry suffixes
123 * are public suffixes, and all public suffixes have registry suffixes.
124 */
125 private static final ImmutableSet<String> NO_RS = NO_PS;
126
127 private static final ImmutableSet<String> NON_PS =
128 ImmutableSet.of(
129 "foo.bar.com",
130 "foo.ca",
131 "foo.bar.ca",
132 "foo.blogspot.com",
133 "foo.blogspot.co.uk",
134 "foo.uk.com",
135 "foo.bar.co.il",
136 "state.CA.us",
137 "www.state.pa.us",
138 "pvt.k12.ca.us",
139 "www.google.com",
140 "www4.yahoo.co.uk",
141 "home.netscape.com",
142 "web.MIT.edu",
143 "foo.eDu.au",
144 "utenti.blah.IT",
145 "dominio.com.co");
146
147 private static final ImmutableSet<String> NON_RS =
148 ImmutableSet.<String>builder().addAll(NON_PS).addAll(PS_NOT_RS).build();
149
150 private static final ImmutableSet<String> TOP_UNDER_REGISTRY_SUFFIX =
151 ImmutableSet.of("google.com", "foo.Co.uk", "foo.ca.us.");
119152
120153 private static final ImmutableSet<String> TOP_PRIVATE_DOMAIN =
121 ImmutableSet.of("google.com", "foo.Co.uk", "foo.ca.us.");
154 ImmutableSet.of("google.com", "foo.Co.uk", "foo.ca.us.", "foo.blogspot.com");
155
156 private static final ImmutableSet<String> UNDER_TOP_UNDER_REGISTRY_SUFFIX =
157 ImmutableSet.of("foo.bar.google.com", "a.b.co.uk", "x.y.ca.us");
122158
123159 private static final ImmutableSet<String> UNDER_PRIVATE_DOMAIN =
124 ImmutableSet.of("foo.bar.google.com", "a.b.co.uk", "x.y.ca.us");
160 ImmutableSet.of("foo.bar.google.com", "a.b.co.uk", "x.y.ca.us", "a.b.blogspot.com");
125161
126162 private static final ImmutableSet<String> VALID_IP_ADDRS = ImmutableSet.of(
127163 "1.2.3.4", "127.0.0.1", "::1", "2001:db8::1");
191227 "a\u7f51\u7edcA.\u7f51\u7edc.Cn" // "a网络A.网络.Cn"
192228 );
193229
230 private static final ImmutableSet<String> SOMEWHERE_UNDER_RS =
231 ImmutableSet.<String>builder().addAll(SOMEWHERE_UNDER_PS).addAll(PS_NOT_RS).build();
232
194233 public void testValid() {
195234 for (String name : VALID_NAME) {
196235 InternetDomainName.from(name);
261300 assertTrue(name, domain.hasPublicSuffix());
262301 assertTrue(name, domain.isUnderPublicSuffix());
263302 assertFalse(name, domain.isTopPrivateDomain());
303 }
304 }
305
306 public void testRegistrySuffix() {
307 for (String name : RS) {
308 final InternetDomainName domain = InternetDomainName.from(name);
309 assertTrue(name, domain.isRegistrySuffix());
310 assertTrue(name, domain.hasRegistrySuffix());
311 assertFalse(name, domain.isUnderRegistrySuffix());
312 assertFalse(name, domain.isTopDomainUnderRegistrySuffix());
313 assertEquals(domain, domain.registrySuffix());
314 }
315
316 for (String name : NO_RS) {
317 final InternetDomainName domain = InternetDomainName.from(name);
318 assertFalse(name, domain.isRegistrySuffix());
319 assertFalse(name, domain.hasRegistrySuffix());
320 assertFalse(name, domain.isUnderRegistrySuffix());
321 assertFalse(name, domain.isTopDomainUnderRegistrySuffix());
322 assertNull(domain.registrySuffix());
323 }
324
325 for (String name : NON_RS) {
326 final InternetDomainName domain = InternetDomainName.from(name);
327 assertFalse(name, domain.isRegistrySuffix());
328 assertTrue(name, domain.hasRegistrySuffix());
329 assertTrue(name, domain.isUnderRegistrySuffix());
330 }
331 }
332
333 public void testUnderRegistrySuffix() {
334 for (String name : SOMEWHERE_UNDER_RS) {
335 final InternetDomainName domain = InternetDomainName.from(name);
336 assertFalse(name, domain.isRegistrySuffix());
337 assertTrue(name, domain.hasRegistrySuffix());
338 assertTrue(name, domain.isUnderRegistrySuffix());
339 }
340 }
341
342 public void testTopDomainUnderRegistrySuffix() {
343 for (String name : TOP_UNDER_REGISTRY_SUFFIX) {
344 final InternetDomainName domain = InternetDomainName.from(name);
345 assertFalse(name, domain.isRegistrySuffix());
346 assertTrue(name, domain.hasRegistrySuffix());
347 assertTrue(name, domain.isUnderRegistrySuffix());
348 assertTrue(name, domain.isTopDomainUnderRegistrySuffix());
349 assertEquals(domain.parent(), domain.registrySuffix());
350 }
351 }
352
353 public void testUnderTopDomainUnderRegistrySuffix() {
354 for (String name : UNDER_TOP_UNDER_REGISTRY_SUFFIX) {
355 final InternetDomainName domain = InternetDomainName.from(name);
356 assertFalse(name, domain.isRegistrySuffix());
357 assertTrue(name, domain.hasRegistrySuffix());
358 assertTrue(name, domain.isUnderRegistrySuffix());
359 assertFalse(name, domain.isTopDomainUnderRegistrySuffix());
264360 }
265361 }
266362
362458 }
363459 }
364460
365 public void testExclusion() {
461 public void testPublicSuffixExclusion() {
366462 InternetDomainName domain = InternetDomainName.from("foo.city.yokohama.jp");
367463 assertTrue(domain.hasPublicSuffix());
368464 assertEquals("yokohama.jp", domain.publicSuffix().toString());
371467 assertFalse(domain.publicSuffix().isPublicSuffix());
372468 }
373469
374 public void testMultipleUnders() {
470 public void testPublicSuffixMultipleUnders() {
375471 // PSL has both *.uk and *.sch.uk; the latter should win.
376472 // See http://code.google.com/p/guava-libraries/issues/detail?id=1176
377473
379475 assertTrue(domain.hasPublicSuffix());
380476 assertEquals("essex.sch.uk", domain.publicSuffix().toString());
381477 assertEquals("www.essex.sch.uk", domain.topPrivateDomain().toString());
478 }
479
480 public void testRegistrySuffixExclusion() {
481 InternetDomainName domain = InternetDomainName.from("foo.city.yokohama.jp");
482 assertTrue(domain.hasRegistrySuffix());
483 assertEquals("yokohama.jp", domain.registrySuffix().toString());
484
485 // Behold the weirdness!
486 assertFalse(domain.registrySuffix().isRegistrySuffix());
487 }
488
489 public void testRegistrySuffixMultipleUnders() {
490 // PSL has both *.uk and *.sch.uk; the latter should win.
491 // See http://code.google.com/p/guava-libraries/issues/detail?id=1176
492
493 InternetDomainName domain = InternetDomainName.from("www.essex.sch.uk");
494 assertTrue(domain.hasRegistrySuffix());
495 assertEquals("essex.sch.uk", domain.registrySuffix().toString());
496 assertEquals("www.essex.sch.uk", domain.topDomainUnderRegistrySuffix().toString());
382497 }
383498
384499 public void testEquality() {
345345 testSortDescending(new double[] {1, 2}, new double[] {2, 1});
346346 testSortDescending(new double[] {1, 3, 1}, new double[] {3, 1, 1});
347347 testSortDescending(new double[] {-1, 1, -2, 2}, new double[] {2, 1, -1, -2});
348 testSortDescending(
349 new double[] {-1, 1, Double.NaN, -2, -0, 0, 2},
350 new double[] {Double.NaN, 2, 1, 0, -0, -1, -2});
348 if (sortWorksWithNaN()) {
349 testSortDescending(
350 new double[] {-1, 1, Double.NaN, -2, -0, 0, 2},
351 new double[] {Double.NaN, 2, 1, 0, -0, -1, -2});
352 }
351353 }
352354
353355 public void testSortDescendingIndexed() {
357359 testSortDescending(new double[] {1, 3, 1}, 0, 2, new double[] {3, 1, 1});
358360 testSortDescending(new double[] {1, 3, 1}, 0, 1, new double[] {1, 3, 1});
359361 testSortDescending(new double[] {-1, -2, 1, 2}, 1, 3, new double[] {-1, 1, -2, 2});
360 testSortDescending(
361 new double[] {-1, 1, Double.NaN, -2, 2}, 1, 4, new double[] {-1, Double.NaN, 1, -2, 2});
362 if (sortWorksWithNaN()) {
363 testSortDescending(
364 new double[] {-1, 1, Double.NaN, -2, 2}, 1, 4, new double[] {-1, Double.NaN, 1, -2, 2});
365 }
362366 }
363367
364368 private static void testSortDescending(double[] input, double[] expectedOutput) {
378382 for (int i = 0; i < input.length; i++) {
379383 assertEquals(0, Double.compare(expectedOutput[i], input[i]));
380384 }
385 }
386
387 @GwtIncompatible // works with real browsers but fails with HtmlUnit
388 public void testSortWorksWithNaNNonGwt() {
389 assertTrue(sortWorksWithNaN());
381390 }
382391
383392 @GwtIncompatible // SerializableTester
652661 } catch (NullPointerException expected) {
653662 }
654663 }
664
665 // HtmlUnit looks to be sorting double[] wrongly. We detect that and skip our tests there.
666 private static boolean sortWorksWithNaN() {
667 double[] array = new double[] {NaN, 0};
668 Arrays.sort(array);
669 return array[0] == 0;
670 }
655671 }
342342 testSortDescending(new float[] {1, 2}, new float[] {2, 1});
343343 testSortDescending(new float[] {1, 3, 1}, new float[] {3, 1, 1});
344344 testSortDescending(new float[] {-1, 1, -2, 2}, new float[] {2, 1, -1, -2});
345 testSortDescending(
346 new float[] {-1, 1, Float.NaN, -2, -0, 0, 2}, new float[] {Float.NaN, 2, 1, 0, -0, -1, -2});
345 if (sortWorksWithNaN()) {
346 testSortDescending(
347 new float[] {-1, 1, Float.NaN, -2, -0, 0, 2}, new float[] {Float.NaN, 2, 1, 0, -0, -1, -2});
348 }
347349 }
348350
349351 public void testSortDescendingIndexed() {
353355 testSortDescending(new float[] {1, 3, 1}, 0, 2, new float[] {3, 1, 1});
354356 testSortDescending(new float[] {1, 3, 1}, 0, 1, new float[] {1, 3, 1});
355357 testSortDescending(new float[] {-1, -2, 1, 2}, 1, 3, new float[] {-1, 1, -2, 2});
356 testSortDescending(
357 new float[] {-1, 1, Float.NaN, -2, 2}, 1, 4, new float[] {-1, Float.NaN, 1, -2, 2});
358 if (sortWorksWithNaN()) {
359 testSortDescending(
360 new float[] {-1, 1, Float.NaN, -2, 2}, 1, 4, new float[] {-1, Float.NaN, 1, -2, 2});
361 }
358362 }
359363
360364 private static void testSortDescending(float[] input, float[] expectedOutput) {
374378 for (int i = 0; i < input.length; i++) {
375379 assertEquals(0, Float.compare(expectedOutput[i], input[i]));
376380 }
381 }
382
383 @GwtIncompatible // works with real browsers but fails with HtmlUnit
384 public void testSortWorksWithNaNNonGwt() {
385 assertTrue(sortWorksWithNaN());
377386 }
378387
379388 @GwtIncompatible // SerializableTester
628637 } catch (NullPointerException expected) {
629638 }
630639 }
640
641 // HtmlUnit looks to be sorting float[] wrongly. We detect that and skip our tests there.
642 private static boolean sortWorksWithNaN() {
643 float[] array = new float[] {NaN, 0};
644 Arrays.sort(array);
645 return array[0] == 0;
646 }
631647 }
5252 Exception exception = new SampleCheckedException();
5353 try {
5454 timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
55 fail("Excpected ExecutionException");
55 fail("Expected ExecutionException");
5656 } catch (ExecutionException e) {
5757 assertThat(e.getCause()).isEqualTo(exception);
5858 }
6262 Exception exception = new RuntimeException("test");
6363 try {
6464 timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
65 fail("Excpected UncheckedExecutionException");
65 fail("Expected UncheckedExecutionException");
6666 } catch (UncheckedExecutionException e) {
6767 assertThat(e.getCause()).isEqualTo(exception);
6868 }
8484 RuntimeException exception = new RuntimeException("test");
8585 try {
8686 timeLimiter.runWithTimeout(runnableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
87 fail("Excpected UncheckedExecutionException");
87 fail("Expected UncheckedExecutionException");
8888 } catch (UncheckedExecutionException e) {
8989 assertThat(e.getCause()).isEqualTo(exception);
9090 }
9595 try {
9696 timeLimiter.runUninterruptiblyWithTimeout(
9797 runnableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
98 fail("Excpected UncheckedExecutionException");
98 fail("Expected UncheckedExecutionException");
9999 } catch (UncheckedExecutionException e) {
100100 assertThat(e.getCause()).isEqualTo(exception);
101101 }
571571 verify(service).shutdown();
572572 }
573573
574 public void testGetExitingExcutorService_executorSetToUseDaemonThreads() {
574 public void testGetExitingExecutorService_executorSetToUseDaemonThreads() {
575575 TestApplication application = new TestApplication();
576576 ThreadPoolExecutor executor = new ThreadPoolExecutor(
577577 1, 2, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
579579 assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon());
580580 }
581581
582 public void testGetExitingExcutorService_executorDelegatesToOriginal() {
582 public void testGetExitingExecutorService_executorDelegatesToOriginal() {
583583 TestApplication application = new TestApplication();
584584 ThreadPoolExecutor executor = mock(ThreadPoolExecutor.class);
585585 ThreadFactory threadFactory = mock(ThreadFactory.class);
588588 verify(executor).execute(EMPTY_RUNNABLE);
589589 }
590590
591 public void testGetExitingExcutorService_shutdownHookRegistered() throws InterruptedException {
591 public void testGetExitingExecutorService_shutdownHookRegistered() throws InterruptedException {
592592 TestApplication application = new TestApplication();
593593 ThreadPoolExecutor executor = mock(ThreadPoolExecutor.class);
594594 ThreadFactory threadFactory = mock(ThreadFactory.class);
598598 verify(executor).shutdown();
599599 }
600600
601 public void testGetExitingScheduledExcutorService_executorSetToUseDaemonThreads() {
601 public void testGetExitingScheduledExecutorService_executorSetToUseDaemonThreads() {
602602 TestApplication application = new TestApplication();
603603 ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
604604 assertNotNull(application.getExitingScheduledExecutorService(executor));
605605 assertTrue(executor.getThreadFactory().newThread(EMPTY_RUNNABLE).isDaemon());
606606 }
607607
608 public void testGetExitingScheduledExcutorService_executorDelegatesToOriginal() {
608 public void testGetExitingScheduledExecutorService_executorDelegatesToOriginal() {
609609 TestApplication application = new TestApplication();
610610 ScheduledThreadPoolExecutor executor = mock(ScheduledThreadPoolExecutor.class);
611611 ThreadFactory threadFactory = mock(ThreadFactory.class);
614614 verify(executor).execute(EMPTY_RUNNABLE);
615615 }
616616
617 public void testGetScheduledExitingExcutorService_shutdownHookRegistered()
617 public void testGetScheduledExitingExecutorService_shutdownHookRegistered()
618618 throws InterruptedException {
619619 TestApplication application = new TestApplication();
620620 ScheduledThreadPoolExecutor executor = mock(ScheduledThreadPoolExecutor.class);
114114 }
115115 }
116116
117 public void testWeakReadWrite() {
118 Striped<ReadWriteLock> striped = Striped.lazyWeakReadWriteLock(1000);
119 Object key = new Object();
120 Lock readLock = striped.get(key).readLock();
121 WeakReference<Object> garbage = new WeakReference<>(new Object());
122 GcFinalization.awaitClear(garbage);
123 Lock writeLock = striped.get(key).writeLock();
124 readLock.lock();
125 assertFalse(writeLock.tryLock());
126 readLock.unlock();
127 }
128
117129 public void testStrongImplementations() {
118130 for (Striped<?> striped : strongImplementations()) {
119131 WeakReference<Object> weakRef = new WeakReference<>(striped.get(new Object()));
1010 </parent>
1111 <groupId>com.google.guava</groupId>
1212 <artifactId>guava-parent</artifactId>
13 <version>23.2-jre</version>
13 <version>23.3-jre</version>
1414 <packaging>pom</packaging>
1515 <name>Guava Maven Parent</name>
1616 <url>https://github.com/google/guava</url>