1 9 package javolution.util; 10 11 import j2me.io.ObjectStreamException; 12 import j2me.io.Serializable; 13 import j2mex.realtime.MemoryArea; 14 import javolution.lang.Immutable; 15 import javolution.util.FastCollection.Record; 16 import javolution.xml.XMLFormat; 17 import javolution.xml.stream.XMLStreamException; 18 19 39 public final class Index implements Record, Immutable, Serializable { 40 41 44 public static final Index ZERO = new Index(0); 45 46 49 private static final FastTable POSITIVE = new FastTable(); 50 static { 51 POSITIVE.add(ZERO); 52 } 53 54 57 private static final FastTable NEGATIVE = new FastTable(); 58 static { 59 NEGATIVE.add(ZERO); 60 } 61 62 67 protected static final XMLFormatXML = new XMLFormat(new Index(0) 68 .getClass()) { 69 70 public boolean isReferenceable() { 71 return false; } 73 74 public Object newInstance(Class cls, InputElement xml) 75 throws XMLStreamException { 76 return Index.valueOf(xml.getAttribute("value", 0)); 77 } 78 79 public void read(InputElement xml, Object obj) 80 throws XMLStreamException { 81 } 83 84 public void write(Object obj, OutputElement xml) 85 throws XMLStreamException { 86 xml.setAttribute("value", ((Index) obj).intValue()); 87 } 88 }; 89 90 93 private final int _value; 94 95 98 private transient Index _next; 99 100 103 private transient Index _previous; 104 105 110 private Index(int i) { 111 _value = i; 112 } 113 114 122 public static Index valueOf(int i) { 123 return ((i >= 0) && (i < POSITIVE.size())) ? (Index) POSITIVE.get(i) 124 : ((i < 0) && (-i < NEGATIVE.size())) ? (Index) NEGATIVE 125 .get(-i) : createInstance(i); 126 } 127 128 private static synchronized Index createInstance(int value) { 129 if (value >= 0) { 130 while (value >= POSITIVE.size()) { 131 MemoryArea.getMemoryArea(POSITIVE).executeInArea( 132 AUGMENT_POSITIVE); 133 } 134 return (Index) POSITIVE.get(value); 135 } else { 136 while (-value >= NEGATIVE.size()) { 137 MemoryArea.getMemoryArea(NEGATIVE).executeInArea( 138 AUGMENT_NEGATIVE); 139 } 140 return (Index) NEGATIVE.get(-value); 141 } 142 } 143 144 private static Runnable AUGMENT_POSITIVE = new Runnable () { 145 public void run() { 146 Index prev = (Index) POSITIVE.getLast(); 147 for (int i = 0; i < 16; i++) { Index index = new Index(prev._value + 1); 149 index._previous = prev; 150 prev._next = index; 151 POSITIVE.add(index); 152 prev = index; 153 } 154 } 155 }; 156 157 private static Runnable AUGMENT_NEGATIVE = new Runnable () { 158 public void run() { 159 Index next = (Index) NEGATIVE.getLast(); 160 for (int i = 0; i < 16; i++) { Index index = new Index(next._value - 1); 162 index._next = next; 163 next._previous = index; 164 NEGATIVE.add(index); 165 next = index; 166 } 167 } 168 }; 169 170 175 public final int intValue() { 176 return _value; 177 } 178 179 184 public final String toString() { 185 return String.valueOf(_value); 186 } 187 188 194 public final boolean equals(Object obj) { 195 return this == obj; 196 } 197 198 203 public final int hashCode() { 204 return _value; 205 } 206 207 212 protected final Object readResolve() throws ObjectStreamException { 213 return valueOf(_value); 214 } 215 216 public final Record getNext() { 218 return _next; 219 } 220 221 public final Record getPrevious() { 223 return _previous; 224 } 225 226 private static final long serialVersionUID = 1L; 227 } | Popular Tags |