KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > Resource


1 package org.sapia.magnet.domain;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.net.URL JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.util.Comparator JavaDoc;
8
9 /**
10  *
11  * @author Jean-Cedric Desrochers
12  *
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class Resource implements Comparable JavaDoc {
20
21   /////////////////////////////////////////////////////////////////////////////////////////
22
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
23
/////////////////////////////////////////////////////////////////////////////////////////
24

25   /** The URL of this resource. */
26   private String JavaDoc _theURL;
27
28   /** The index of this resource. */
29   private int _theIndex;
30
31   /////////////////////////////////////////////////////////////////////////////////////////
32
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
33
/////////////////////////////////////////////////////////////////////////////////////////
34

35   /**
36    * Creates a new Resource instance with the passed in arguments.
37    *
38    * @param aURL The URL of the new resource.
39    * @param anIndex The index of this new resource.
40    */

41   public Resource(String JavaDoc aURL, int anIndex) {
42     _theURL = aURL;
43     _theIndex = anIndex;
44   }
45
46   /////////////////////////////////////////////////////////////////////////////////////////
47
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
48
/////////////////////////////////////////////////////////////////////////////////////////
49

50   /**
51    * Returns the URL of this resource.
52    *
53    * @return The URL of this resource.
54    */

55   public String JavaDoc getURL() {
56     return _theURL;
57   }
58
59   /**
60    * Returns the index of this resource.
61    *
62    * @return The index of this resource.
63    */

64   public int getIndex() {
65     return _theIndex;
66   }
67
68   /////////////////////////////////////////////////////////////////////////////////////////
69
/////////////////////////////////// HELPER METHODS ////////////////////////////////////
70
/////////////////////////////////////////////////////////////////////////////////////////
71

72   /**
73    * Returns this resource as a URL object.
74    *
75    * @return This resource as a URL object.
76    * @exception MalformedURLException If an error occurs while converting
77    * this resource as an URL object.
78    */

79   public URL JavaDoc toURL() throws MalformedURLException JavaDoc {
80     return new URL JavaDoc(_theURL);
81   }
82
83   /////////////////////////////////////////////////////////////////////////////////////////
84
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
85
/////////////////////////////////////////////////////////////////////////////////////////
86

87   /**
88    * Compares the object passed in for equality.
89    *
90    * @param anObject The object to compare.
91    * @return True if the object is a Resource and it has the same URL.
92    */

93   public boolean equals(Object JavaDoc anObject) {
94     if (anObject instanceof Resource) {
95       return _theURL.equals(((Resource) anObject)._theURL);
96     } else {
97       return false;
98     }
99   }
100
101   /**
102    * Returns the hash code value for this resource.
103    *
104    * @return The hash code value for this resource.
105    */

106   public int hashCode() {
107     return _theURL.hashCode();
108   }
109
110   /**
111    * Returns a string representation of this resource.
112    *
113    * @return A string representation of this resource.
114    */

115   public String JavaDoc toString() {
116     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
117     aBuffer.append("[url=").append(_theURL).
118             append(" index=").append(_theIndex).
119             append("]\n");
120
121     return aBuffer.toString();
122   }
123
124   /////////////////////////////////////////////////////////////////////////////////////////
125
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
126
/////////////////////////////////////////////////////////////////////////////////////////
127

128   /**
129    * Compares this object with the specified object for order. Returns a
130    * negative integer, zero, or a positive integer as this object is less
131    * than, equal to, or greater than the specified object.<p>
132    *
133    * @param anObject The object to be compared.
134    * @return A negative integer, zero, or a positive integer as this object
135    * is less than, equal to, or greater than the specified object.
136    * @exception ClassCastException if the specified object's type prevents it
137    * from being compared to this Object.
138    */

139   public int compareTo(Object JavaDoc anObject) {
140     return _theIndex - ((Resource) anObject)._theIndex;
141   }
142
143
144   /////////////////////////////////////////////////////////////////////////////////////////
145
/////////////////////////////////// INNER CLASSES /////////////////////////////////////
146
/////////////////////////////////////////////////////////////////////////////////////////
147

148   public static class AscendingComparator implements Comparator JavaDoc {
149     public int compare(Object JavaDoc anObject, Object JavaDoc anotherObject) {
150       return ((Resource) anObject)._theURL.compareTo(((Resource) anotherObject)._theURL);
151     }
152   }
153
154   public static class DescendingComparator implements Comparator JavaDoc {
155     public int compare(Object JavaDoc anObject, Object JavaDoc anotherObject) {
156       return ((Resource) anObject)._theURL.compareTo(((Resource) anotherObject)._theURL) * -1;
157     }
158   }
159 }
160
Popular Tags