KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > ltl > graph > ColorPair


1
2 //
3
// Copyright (C) 2005 United States Government as represented by the
4
// Administrator of the National Aeronautics and Space Administration
5
// (NASA). All Rights Reserved.
6
//
7
// This software is distributed under the NASA Open Source Agreement
8
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
9
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
10
// directory tree for the complete NOSA document.
11
//
12
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
13
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
14
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
15
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
16
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
17
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
18
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
19
//
20
package gov.nasa.ltl.graph;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.TreeSet JavaDoc;
24
25
26 /**
27  * DOCUMENT ME!
28  */

29 public class ColorPair extends Pair implements Comparable JavaDoc {
30   public ColorPair (int colorIn, TreeSet JavaDoc iMaxSetIn) {
31     super(colorIn, iMaxSetIn);
32   }
33
34   public void setColor (int colorIn) {
35     super.setValue(colorIn);
36   }
37
38   public int getColor () {
39     return super.getValue();
40   }
41
42   public void setIMaxSet (TreeSet JavaDoc iMaxSetIn) {
43     super.setElement(iMaxSetIn);
44   }
45
46   public TreeSet JavaDoc getIMaxSet () {
47     return (TreeSet JavaDoc) super.getElement();
48   }
49
50   public int compareTo (Object JavaDoc o) {
51     ColorPair other = (ColorPair) o;
52     TreeSet JavaDoc otherSet = other.getIMaxSet();
53
54     if (getIMaxSet().size() < otherSet.size()) {
55       return -1;
56     }
57
58     if (getIMaxSet().size() > otherSet.size()) {
59       return 1;
60     }
61
62     // TreeSets are ordered !!
63
int index = 0;
64
65     for (Iterator JavaDoc i = getIMaxSet().iterator(); i.hasNext();) {
66       ITypeNeighbor currNeigh = (ITypeNeighbor) i.next();
67       Object JavaDoc[] otherArray = otherSet.toArray();
68       int comparison = currNeigh.compareTo(
69                                        (ITypeNeighbor) otherArray[index]);
70
71       if ((comparison < 0) || (comparison > 0)) {
72         return comparison;
73       }
74
75       index++;
76     }
77
78     if (getColor() < other.getColor()) {
79       return -1;
80     }
81
82     if (getColor() > other.getColor()) {
83       return 1;
84     }
85
86     return 0;
87   }
88
89   public boolean equals (Object JavaDoc o) {
90     ColorPair other = (ColorPair) o;
91     TreeSet JavaDoc otherSet = other.getIMaxSet();
92
93     if (getIMaxSet().size() < otherSet.size()) {
94       return false;
95     }
96
97     if (getIMaxSet().size() > otherSet.size()) {
98       return false;
99     }
100
101     if (getColor() != other.getColor()) {
102       return false;
103     }
104
105     // TreeSets are ordered
106
int index = 0;
107
108     for (Iterator JavaDoc i = getIMaxSet().iterator(); i.hasNext();) {
109       ITypeNeighbor currNeigh = (ITypeNeighbor) i.next();
110       Object JavaDoc[] otherArray = otherSet.toArray();
111       int comparison = currNeigh.compareTo(
112                                        (ITypeNeighbor) otherArray[index]);
113
114       if ((comparison < 0) || (comparison > 0)) {
115         return false;
116       }
117
118       index++;
119     }
120
121     return true;
122   }
123 }
Popular Tags