KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: CreationTimestampIdComparator.java,v 1.2 2007/03/05 07:29:25 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.BasicDataObject;
27
28 /**
29  * Comparator to compare basic data objects based on their creation timestamp
30  * and id.
31  *
32  * @version $Id: CreationTimestampIdComparator.java,v 1.2 2007/03/05 07:29:25 bastafidli Exp $
33  * @author Miro Halas
34  * @code.reviewer Miro Halas
35  * @code.reviewed Initial revision
36  */

37 public class CreationTimestampIdComparator implements Comparator JavaDoc
38 {
39    // Cached values ////////////////////////////////////////////////////////////
40

41    /**
42     * Shared comparator instance. Must be named this way to avoid Checkstyle
43     * warning.
44     */

45    private static Comparator JavaDoc s_instance = new CreationTimestampIdComparator();
46
47    // Public methods ///////////////////////////////////////////////////////////
48

49    /**
50     * Get shared comparator instance.
51     *
52     * @return Comparator - shared comparator instance
53     */

54    public static Comparator JavaDoc getInstance(
55    )
56    {
57       return s_instance;
58    }
59
60    /**
61     * Compare creation timestamp and id of two basic data objects.
62     *
63     * @param o1 - BasicDataObject #1
64     * @param o2 - BasicDataObject #2
65     * @return int - -1 if o1 < o2,
66     * 0 if o1 == o2
67     * 1 if o1 > o2
68     */

69    public int compare(
70       Object JavaDoc o1,
71       Object JavaDoc o2
72    )
73    {
74       long lTimestamp1 = ((BasicDataObject)o1).getCreationTimestamp().getTime();
75       long lTimestamp2 = ((BasicDataObject)o2).getCreationTimestamp().getTime();
76
77       if (lTimestamp1 < lTimestamp2)
78       {
79          return -1;
80       }
81       else
82       {
83          if (lTimestamp1 == lTimestamp2)
84          {
85             int iId1 = ((BasicDataObject)o1).getId();
86             int iId2 = ((BasicDataObject)o2).getId();
87             
88             if (iId1 < iId2)
89             {
90                return -1;
91             }
92             else if (iId1 == iId2)
93             {
94                return 0;
95             }
96             else
97             {
98                return 1;
99             }
100          }
101          else
102          {
103             return 1;
104          }
105       }
106    }
107 }
108
Popular Tags