KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > ordereddata > util > OrderedDataComparator


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: OrderedDataComparator.java,v 1.3 2007/01/07 06:14:32 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.patterns.ordereddata.util;
23
24 import java.util.Comparator JavaDoc;
25
26 import org.opensubsystems.core.util.GlobalConstants;
27 import org.opensubsystems.patterns.ordereddata.data.OrderedData;
28
29 /**
30  * Class for comparing of OrderedData.
31  *
32  * @version $Id: OrderedDataComparator.java,v 1.3 2007/01/07 06:14:32 bastafidli Exp $
33  * @author Peter Satury
34  * @code.reviewer Miro Halas
35  * @code.reviewed 1.1 2006/04/29 00:25:11 jlegeny
36  */

37 public class OrderedDataComparator implements Comparator JavaDoc
38 {
39    /**
40     * Lock used in synchronized sections.
41     */

42    private static final String JavaDoc IMPL_LOCK = "IMPL_LOCK";
43
44    // Cached values ////////////////////////////////////////////////////////////
45

46    /**
47     * Reference to the default instance actually in use.
48     */

49    private static OrderedDataComparator s_defaultInstance = null;
50
51    // Public methods ///////////////////////////////////////////////////////////
52

53    /**
54     * Get the default OrderedDataComparator instance.
55     *
56     * @return default OrderedDataComparator
57     */

58    public static OrderedDataComparator getInstance(
59    )
60    {
61       if (s_defaultInstance == null)
62       {
63          // Only if the default factory wasn't set by other means create a new one
64
// Synchronize just for the creation
65
synchronized (IMPL_LOCK)
66          {
67             if (s_defaultInstance == null)
68             {
69                setInstance(new OrderedDataComparator());
70             }
71          }
72       }
73       
74       return s_defaultInstance;
75    }
76    
77    /**
78     * Set default OrderedDataComparatory instance. This instance will be returned by getInstance
79     * method until it is changed.
80     *
81     * @param defaultInstance - new default OrderedDataComparatory instance
82     */

83    public static void setInstance(
84       OrderedDataComparator defaultInstance
85    )
86    {
87       if (GlobalConstants.ERROR_CHECKING)
88       {
89          assert defaultInstance != null
90                 : "Default factory instance cannot be null";
91       }
92       
93       synchronized (IMPL_LOCK)
94       {
95          s_defaultInstance = defaultInstance;
96       }
97    }
98    
99    /**
100     * {@inheritDoc}
101     */

102    public int compare(
103       Object JavaDoc o1,
104       Object JavaDoc o2
105    )
106    {
107       int iReturn = 0;
108       if (o1 instanceof OrderedData && o2 instanceof OrderedData)
109       {
110          if (((OrderedData) o1).getOrderNumber()
111                   != ((OrderedData) o2).getOrderNumber())
112          {
113             iReturn = (((OrderedData) o1).getOrderNumber()
114                            < ((OrderedData) o2).getOrderNumber()) ? -1 : 1;
115          }
116       }
117       return iReturn;
118    }
119 }
120
Popular Tags