KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > Comparators


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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. Instead of
21    * having a directly accessible field, a function is used in anticipation of
22    * generics support. This class exists so Arrays and Collections can share
23    * the natural comparator without having to know internals of each other.
24    */

25   
26   /**
27    * Compares two Objects according to their <i>natural ordering</i>.
28    * @see java.lang.Comparable
29    */

30   private static final Comparator JavaDoc NATURAL = new Comparator JavaDoc() {
31     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
32       return ((Comparable JavaDoc) o1).compareTo(o2);
33     }
34   };
35
36   /**
37    * Returns the natural Comparator.
38    * TODO: generics support
39    *
40    * @return the natural Comparator
41    */

42   public static /*<T>*/ Comparator JavaDoc/*<T>*/ natural() {
43     /*
44      * Code for generics support is commented. Example calling code, which
45      * should be moved into the Javadoc comment when generics are added:
46      * <code>Comparator&lt;String&gt; = Comparators.natural();</code>
47      */

48     return /*(Comparator<T>)*/ NATURAL;
49   }
50 }
51
Popular Tags