Source Code Cross Referenced for Comparator.java in  » 6.0-JDK-Modules » j2me » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)Comparator.java	1.24 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package java.util;
029:
030:        /**
031:         * A comparison function, which imposes a <i>total ordering</i> on some
032:         * collection of objects.  Comparators can be passed to a sort method (such as
033:         * <tt>Collections.sort</tt>) to allow precise control over the sort order.
034:         * Comparators can also be used to control the order of certain data
035:         * structures (such as <tt>TreeSet</tt> or <tt>TreeMap</tt>).<p>
036:         *
037:         * The ordering imposed by a Comparator <tt>c</tt> on a set of elements
038:         * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
039:         * <tt>(compare((Object)e1, (Object)e2)==0)</tt> has the same boolean value as
040:         * <tt>e1.equals((Object)e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
041:         * <tt>S</tt>.<p>
042:         *
043:         * Caution should be exercised when using a comparator capable of imposing an
044:         * ordering inconsistent with equals to order a sorted set (or sorted map).
045:         * Suppose a sorted set (or sorted map) with an explicit Comparator <tt>c</tt>
046:         * is used with elements (or keys) drawn from a set <tt>S</tt>.  If the
047:         * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
048:         * the sorted set (or sorted map) will behave "strangely."  In particular the
049:         * sorted set (or sorted map) will violate the general contract for set (or
050:         * map), which is defined in terms of <tt>equals</tt>.<p>
051:         * 
052:         * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
053:         * <tt>(a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0)</tt> to a
054:         * sorted set with comparator <tt>c</tt>, the second <tt>add</tt> operation
055:         * will return false (and the size of the sorted set will not increase)
056:         * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
057:         * perspective.<p>
058:         *
059:         * Note: It is generally a good idea for comparators to implement
060:         * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
061:         * serializable data structures (like <tt>TreeSet</tt>, <tt>TreeMap</tt>).  In
062:         * order for the data structure to serialize successfully, the comparator (if
063:         * provided) must implement <tt>Serializable</tt>.<p>
064:         *
065:         * For the mathematically inclined, the <i>relation</i> that defines
066:         * the <i>total order</i> that a given comparator <tt>c</tt> imposes on a
067:         * given set of objects <tt>S</tt> is:<pre>
068:         *       {(x, y) such that c.compare((Object)x, (Object)y) &lt;= 0}.
069:         * </pre> The <i>quotient</i> for this total order is:<pre>
070:         *       {(x, y) such that c.compare((Object)x, (Object)y) == 0}.
071:         * </pre>
072:         *
073:         * It follows immediately from the contract for <tt>compare</tt> that the
074:         * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
075:         * natural ordering is a <i>total order</i> on <tt>S</tt>.  When we say that
076:         * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
077:         * equals</i>, we mean that the quotient for the natural ordering is the
078:         * equivalence relation defined by the objects' <tt>equals(Object)</tt>
079:         * method(s):<pre>
080:         *       {(x, y) such that x.equals((Object)y)}.
081:         * </pre><p>
082:         *
083:         * This interface is a member of the 
084:         * <a href="{@docRoot}/../guide/collections/index.html">
085:         * Java Collections Framework</a>.
086:         *
087:         * @author  Josh Bloch
088:         * @version 1.15, 02/02/00
089:         * @see Comparable
090:         * @see Arrays#sort(Object[], Comparator)
091:         * @see TreeMap
092:         * @see TreeSet
093:         * @see SortedMap
094:         * @see SortedSet
095:         * @see java.io.Serializable
096:         * @since 1.2
097:         */
098:
099:        public interface Comparator {
100:            /**
101:             * Compares its two arguments for order.  Returns a negative integer,
102:             * zero, or a positive integer as the first argument is less than, equal
103:             * to, or greater than the second.<p>
104:             *
105:             * The implementor must ensure that <tt>sgn(compare(x, y)) ==
106:             * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
107:             * implies that <tt>compare(x, y)</tt> must throw an exception if and only
108:             * if <tt>compare(y, x)</tt> throws an exception.)<p>
109:             *
110:             * The implementor must also ensure that the relation is transitive:
111:             * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
112:             * <tt>compare(x, z)&gt;0</tt>.<p>
113:             *
114:             * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
115:             * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
116:             * <tt>z</tt>.<p>
117:             *
118:             * It is generally the case, but <i>not</i> strictly required that 
119:             * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
120:             * any comparator that violates this condition should clearly indicate
121:             * this fact.  The recommended language is "Note: this comparator
122:             * imposes orderings that are inconsistent with equals."
123:             * 
124:             * @param o1 the first object to be compared.
125:             * @param o2 the second object to be compared.
126:             * @return a negative integer, zero, or a positive integer as the
127:             * 	       first argument is less than, equal to, or greater than the
128:             *	       second. 
129:             * @throws ClassCastException if the arguments' types prevent them from
130:             * 	       being compared by this Comparator.
131:             */
132:            int compare(Object o1, Object o2);
133:
134:            /**
135:             * 
136:             * Indicates whether some other object is &quot;equal to&quot; this
137:             * Comparator.  This method must obey the general contract of
138:             * <tt>Object.equals(Object)</tt>.  Additionally, this method can return
139:             * <tt>true</tt> <i>only</i> if the specified Object is also a comparator
140:             * and it imposes the same ordering as this comparator.  Thus,
141:             * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
142:             * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
143:             * <tt>o1</tt> and <tt>o2</tt>.<p>
144:             *
145:             * Note that it is <i>always</i> safe <i>not</i> to override
146:             * <tt>Object.equals(Object)</tt>.  However, overriding this method may,
147:             * in some cases, improve performance by allowing programs to determine
148:             * that two distinct Comparators impose the same order.
149:             *
150:             * @param   obj   the reference object with which to compare.
151:             * @return  <code>true</code> only if the specified object is also
152:             *		a comparator and it imposes the same ordering as this
153:             *		comparator.
154:             * @see     java.lang.Object#equals(java.lang.Object)
155:             * @see java.lang.Object#hashCode()
156:             */
157:            boolean equals(Object obj);
158:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.