01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.util;
17:
18: class Comparators {
19: /*
20: * This is a utility class that provides a default Comparator. This class
21: * exists so Arrays and Collections can share the natural comparator without
22: * having to know internals of each other.
23: *
24: * This class is package protected since it is not in the JRE.
25: */
26:
27: /**
28: * Compares two Objects according to their <i>natural ordering</i>.
29: *
30: * @see java.lang.Comparable
31: */
32: private static final Comparator<Object> NATURAL = new Comparator<Object>() {
33: public int compare(Object o1, Object o2) {
34: return ((Comparable<Object>) o1).compareTo(o2);
35: }
36: };
37:
38: /**
39: * Returns the natural Comparator.
40: * <p>
41: * Example:
42: *
43: * <pre>Comparator<String> compareString = Comparators.natural()</pre>
44: *
45: * @return the natural Comparator
46: */
47: public static <T> Comparator<T> natural() {
48: return (Comparator<T>) NATURAL;
49: }
50: }
|