KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > MSort


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.io.Serializable JavaDoc;
17 import java.math.BigDecimal JavaDoc;
18 import java.sql.Timestamp JavaDoc;
19 import java.util.Comparator JavaDoc;
20
21 /**
22  * Class to Sort Data
23  *
24  * @author Jorg Janke
25  * @version $Id: MSort.java,v 1.2 2003/09/27 11:08:55 jjanke Exp $
26  */

27 public final class MSort implements Comparator JavaDoc, Serializable JavaDoc
28 {
29     /**
30      * Constructor - Sort Entity
31      * @param new_index index
32      * @param new_data data
33      */

34     public MSort (int new_index, Object JavaDoc new_data)
35     {
36         index = new_index;
37         data = new_data;
38     } // MSort
39

40     /** Direct access index */
41     public int index;
42     /** The data */
43     public Object JavaDoc data;
44
45     /** Multiplier */
46     private int m_multiplier = 1; // Asc by default
47

48     /**
49      * Sort Ascending
50      * @param ascending if true sort ascending
51      */

52     public void setSortAsc (boolean ascending)
53     {
54         if (ascending)
55             m_multiplier = 1;
56         else
57             m_multiplier = -1;
58     } // setSortAsc
59

60     /*************************************************************************/
61
62     /**
63      * Compare Data of two entities
64      * @param o1 object
65      * @param o2 object
66      * @return comparator
67      */

68     public int compare(Object JavaDoc o1, Object JavaDoc o2)
69     {
70         // Get Objects to compare
71
Object JavaDoc cmp1 = null;
72         if (o1 instanceof MSort)
73             cmp1 = ((MSort)o1).data;
74         if (cmp1 instanceof NamePair)
75             cmp1 = ((NamePair)cmp1).getName();
76
77         Object JavaDoc cmp2 = o2;
78         if (o2 instanceof MSort)
79             cmp2 = ((MSort)o2).data;
80         if (cmp2 instanceof NamePair)
81             cmp2 = ((NamePair)cmp2).getName();
82
83         // Comparing Null values
84
if (cmp1 == null)
85             cmp1 = new String JavaDoc("");
86         if (cmp2 == null)
87             cmp2 = new String JavaDoc("");
88
89         /**
90          * compare different data types
91          */

92
93         // Date
94
if (cmp1 instanceof Timestamp JavaDoc)
95         {
96             Timestamp JavaDoc t = (Timestamp JavaDoc)cmp1;
97             return t.compareTo(cmp2) * m_multiplier;
98         }
99         // BigDecimal
100
else if (cmp1 instanceof BigDecimal JavaDoc)
101         {
102             BigDecimal JavaDoc d = (BigDecimal JavaDoc)cmp1;
103             return d.compareTo(cmp2) * m_multiplier;
104         }
105         // Integer
106
else if (cmp1 instanceof Integer JavaDoc)
107         {
108             Integer JavaDoc d = (Integer JavaDoc)cmp1;
109             return d.compareTo(cmp2) * m_multiplier;
110         }
111         // String
112
else if (cmp1 instanceof String JavaDoc)
113         {
114             String JavaDoc s = (String JavaDoc)cmp1;
115             return s.compareTo(cmp2.toString()) * m_multiplier;
116         }
117
118         // String value
119
String JavaDoc s = cmp1.toString();
120         return s.compareTo(cmp2.toString()) * m_multiplier;
121     } // compare
122

123     /**
124      * Equal (based on data, ignores index)
125      * @param obj object
126      * @return true if equal
127      */

128     public boolean equals (Object JavaDoc obj)
129     {
130         if (obj instanceof MSort)
131         {
132             MSort ms = (MSort)obj;
133             if (data == ms.data)
134                 return true;
135         }
136         return false;
137     } // equals
138

139     /**
140      * String Representation
141      * @return info
142      */

143     public String JavaDoc toString()
144     {
145         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("MSort[");
146         sb.append("Index=").append(index).append(",Data=").append(data);
147         sb.append("]");
148         return sb.toString();
149     } // toString
150

151
152 } // MSort
153
Popular Tags