KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > DataObjectOrderingComparator


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DataObjectOrderingComparator.java,v 1.4 2007/01/07 06:14:01 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.util.Comparator JavaDoc;
25
26 import org.opensubsystems.core.data.DataObject;
27
28 /**
29  * Class for comparing order of DataObjects based on some external order.
30  *
31  * @version $Id: DataObjectOrderingComparator.java,v 1.4 2007/01/07 06:14:01 bastafidli Exp $
32  * @author Julo Legeny
33  * @code.reviewer Miro Halas
34  * @code.reviewed 1.2 2004/12/18 06:18:25 bastafidli
35  */

36 public class DataObjectOrderingComparator implements Comparator JavaDoc
37 {
38    // Attributes ///////////////////////////////////////////////////////////////
39

40    /**
41     * Array of ordered IDs
42     */

43    protected int[] m_arrOrderedIDs = null;
44    
45    // Constructors /////////////////////////////////////////////////////////////
46

47    /**
48     * Constructor.
49     *
50     * @param arrOrderedIDs - ordered IDs which will determine the order of data
51     * objects
52     */

53    public DataObjectOrderingComparator(
54       int[] arrOrderedIDs
55    )
56    {
57       super();
58       
59       m_arrOrderedIDs = arrOrderedIDs;
60    }
61
62    // Public methods ///////////////////////////////////////////////////////////
63

64    /**
65     * {@inheritDoc}
66     */

67    public int compare(
68       Object JavaDoc o1,
69       Object JavaDoc o2
70    )
71    {
72       int iReturn = 0;
73       
74       if ((o1 instanceof DataObject) && (o2 instanceof DataObject))
75       {
76          if (m_arrOrderedIDs != null)
77          {
78             int i1 = ((DataObject) o1).getId();
79             int i2 = ((DataObject) o2).getId();
80             
81             if (i1 != i2)
82             {
83                int iCounter = 0;
84                
85                while ((iReturn == 0) && (iCounter < m_arrOrderedIDs.length))
86                {
87                   if (m_arrOrderedIDs[iCounter] == i1)
88                   {
89                      iReturn = -1;
90                   }
91                   else if (m_arrOrderedIDs[iCounter] == i2)
92                   {
93                      iReturn = 1;
94                   }
95                   iCounter++;
96                }
97             }
98          }
99          else
100          {
101             int i1 = ((DataObject) o1).getId();
102             int i2 = ((DataObject) o2).getId();
103             
104             if (i1 < i2)
105             {
106                iReturn = -1;
107             }
108             else if (i1 > i2)
109             {
110                iReturn = 1;
111             }
112             else
113             {
114                iReturn = 0;
115             }
116          }
117       }
118
119       return iReturn;
120    }
121 }
122
Popular Tags