01: package xdoclet.modules.ojb.model;
02:
03: /* Copyright 2004-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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;
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 {
28: /**
29: * The table owning the columns
30: */
31: private TableDef _table = null;
32:
33: /**
34: * Creates a new comparator object.
35: *
36: * @param table The table owning the columns
37: */
38: public ColumnWithIdComparator(TableDef table) {
39: _table = table;
40: }
41:
42: /**
43: * Compares two columns given by their names.
44: *
45: * @param objA The name of the first column
46: * @param objB The name of the second column
47: * @return
48: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
49: */
50: public int compare(Object objA, Object objB) {
51: String idAStr = _table.getColumn((String) objA).getProperty(
52: "id");
53: String idBStr = _table.getColumn((String) objB).getProperty(
54: "id");
55: int idA;
56: int idB;
57:
58: try {
59: idA = Integer.parseInt(idAStr);
60: } catch (Exception ex) {
61: return 1;
62: }
63: try {
64: idB = Integer.parseInt(idBStr);
65: } catch (Exception ex) {
66: return -1;
67: }
68: return idA < idB ? -1 : (idA > idB ? 1 : 0);
69: }
70:
71: }
|