KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > model > ColumnWithIdComparator


1 package xdoclet.modules.ojb.model;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.Comparator JavaDoc;
19
20 /**
21  * Comparator used for sorting collections that compares column names according to the id properties of the corresponding
22  * columns.
23  *
24  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
25  * @created May 1, 2003
26  */

27 public class ColumnWithIdComparator implements Comparator JavaDoc
28 {
29     /**
30      * The table owning the columns
31      */

32     private TableDef _table = null;
33
34     /**
35      * Creates a new comparator object.
36      *
37      * @param table The table owning the columns
38      */

39     public ColumnWithIdComparator(TableDef table)
40     {
41         _table = table;
42     }
43
44     /**
45      * Compares two columns given by their names.
46      *
47      * @param objA The name of the first column
48      * @param objB The name of the second column
49      * @return
50      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
51      */

52     public int compare(Object JavaDoc objA, Object JavaDoc objB)
53     {
54         String JavaDoc idAStr = _table.getColumn((String JavaDoc)objA).getProperty("id");
55         String JavaDoc idBStr = _table.getColumn((String JavaDoc)objB).getProperty("id");
56         int idA;
57         int idB;
58
59         try {
60             idA = Integer.parseInt(idAStr);
61         }
62         catch (Exception JavaDoc ex) {
63             return 1;
64         }
65         try {
66             idB = Integer.parseInt(idBStr);
67         }
68         catch (Exception JavaDoc ex) {
69             return -1;
70         }
71         return idA < idB ? -1 : (idA > idB ? 1 : 0);
72     }
73
74 }
75
Popular Tags