Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/ubermq/chord/IntegerChordIdentifier.java


1   package com.ubermq.chord;
2   
3   import java.security.*;
4   
5   /**
6    * A chord identifier that uses a 2^32 size identifier space. This
7    * is intended for use with the Java <code>hashCode</code> operation
8    * to perform correct object hashing. <P>
9    *
10   * If stricter hashing is required, a SHA implementation of this
11   * identifier interface could be created fairly simply.<P>
12   */
13  public final class IntegerChordIdentifier
14      implements ChordIdentifier
15  {
16      private int id;
17      private static SecureRandom sr = null;
18  
19      static {
20          try
21          {
22              sr = SecureRandom.getInstance("SHA1PRNG");
23          }
24          catch (NoSuchAlgorithmException e) {
25              e.printStackTrace();
26          }
27      }
28  
29      public static final long serialVersionUID = 1L;
30  
31      /**
32       * The default finger table size for this identifier type.
33       */
34      public static final int INTEGER_FINGER_TABLE_SIZE = 16;
35  
36      /**
37       * Constructs a chord identifier from an integer.
38       * @param hashCode the integer hash code.
39       */
40      IntegerChordIdentifier(int hashCode)
41      {
42          this.id = hashCode;
43      }
44  
45      /**
46       * Constructs a chord identifier from the result
47       * of the <code>hashCode</code> method on the
48       * given object.
49       * @param o an object to produce an identifier for
50       */
51      IntegerChordIdentifier(Object o)
52      {
53          this.id = o.hashCode();
54      }
55  
56      /**
57       * Generates a pseudo-random identifier.
58       */
59      IntegerChordIdentifier()
60      {
61          this.id = sr.nextInt();
62      }
63  
64      private static final ChordIdentifierFactory theFactory = new ChordIdentifierFactory() {
65          public ChordIdentifier createRandomIdentifier()
66          {
67              return new IntegerChordIdentifier();
68          }
69  
70          public ChordIdentifier createIdentifier(Object o)
71          {
72              return new IntegerChordIdentifier(o);
73          }
74  
75          /**
76           * Returns the identifier specified by a given URI.  This
77           * can be implementation dependent.
78           */
79          public ChordIdentifier getIdentifier(java.net.URI uri)
80          {
81              if (uri.getPath() == null ||
82                  uri.getPath().length() == 0)
83                  return null;
84              else
85                  return new IntegerChordIdentifier(Integer.valueOf(uri.getPath().substring(1)).intValue());
86          }
87  
88          public int getFingerTableSize()
89          {
90              return INTEGER_FINGER_TABLE_SIZE;
91          }
92      };
93  
94      /**
95       * Returns the chord identifier factory for integer
96       * chord identifiers.
97       */
98      public static ChordIdentifierFactory getFactory()
99      {
100         return theFactory;
101     }
102 
103     public double normal()
104     {
105         return ((1 + (((double)id) / Integer.MAX_VALUE)) / 2);
106     }
107 
108     /**
109      * Returns the next chord identifier in the
110      * identifier sequence.
111      *
112      * @return a chord identifier that is the next in the
113      * identifier sequence.
114      */
115     public ChordIdentifier next()
116     {
117         return new IntegerChordIdentifier(id + 1);
118     }
119 
120     /**
121      * Returns the chord identifier that is <code>l</code>
122      * after this one.
123      *
124      * @return a chord identifier that is the specified
125      * interval away.
126      */
127     public ChordIdentifier next(long l)
128     {
129         return new IntegerChordIdentifier(id + (int)(l & 0xffffffff));
130     }
131 
132     /**
133      * Returns the identifier for the <code>i</code>th finger
134      * for this identifier.  A finger is defined by the Chord
135      * set of algorithms as the identifier that succeeds this
136      * identifier by 2<super>i</super>. However, individual
137      * identifiers can use other finger computations, as appropriate.
138      *
139      * @param i the finger index, from 1 to <code>factory().getFingerTableSize()-1</code>.
140      * The finger at index 0 is defined as the immediate successor to this node.
141      * @return the identifier corresponding to the ith finger.
142      * @throws IllegalArgumentException if i is out of the specified range
143      */
144     public ChordIdentifier nextFinger(int i)
145     {
146         return new IntegerChordIdentifier(id + (1 << i));
147     }
148 
149     /**
150      * Returns the previous chord identifier in the
151      * identifier sequence.
152      *
153      * @return a chord identifier that is immediately before this
154      * one in the identifier sequence.
155      */
156     public ChordIdentifier previous()
157     {
158         return new IntegerChordIdentifier(id - 1);
159     }
160 
161     /**
162      * Returns the chord identifier that is <code>l</code>
163      * before this one.
164      *
165      * @return a chord identifier that is the specified
166      * interval before this identifier.
167      */
168     public ChordIdentifier previous(long l)
169     {
170         return new IntegerChordIdentifier(id - (int)(l & 0xffffffff));
171     }
172 
173     /**
174      * Provides the factory instance used to create this
175      * identifier, so that new identifiers can be easily
176      * created from existing ones.
177      * @return an identifier factory
178      */
179     public ChordIdentifierFactory factory()
180     {
181         return IntegerChordIdentifier.theFactory;
182     }
183 
184     /**
185      * Returns the value of this chord identifier as an <code>int</code>.
186      * @return an int representing this identifier
187      */
188     public int intValue()
189     {
190         return id;
191     }
192 
193     public int compareTo(Object o)
194     {
195         IntegerChordIdentifier i = (IntegerChordIdentifier)o;
196         return (id == i.id) ? 0 : ((id > i.id) ? 1 : -1);
197     }
198 
199     public boolean equals(Object o)
200     {
201         if (o instanceof IntegerChordIdentifier)
202             return id == ((IntegerChordIdentifier)o).id;
203         else return false;
204     }
205 
206     public int hashCode()
207     {
208         return id;
209     }
210 
211     public String toString()
212     {
213         return String.valueOf(id);
214     }
215 }