Javolution 6.0.0 java
Index.java
Go to the documentation of this file.
1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2012 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */
9 package javolution.util;
10 
11 import java.io.IOException;
12 import java.io.ObjectStreamException;
13 
15 import javolution.lang.MathLib;
16 import javolution.lang.Realtime;
18 import javolution.text.Cursor;
23 
41 @Realtime
42 @DefaultTextFormat(Index.Decimal.class)
43 public final class Index extends Number implements Comparable<Index>,
44  ValueType<Index> {
45 
49  public static class Decimal extends TextFormat<Index> {
50 
51  @Override
52  public Appendable format(Index obj, Appendable dest) throws IOException {
53  return TypeFormat.format(obj.intValue(), dest);
54  }
55 
56  @Override
57  public Index parse(CharSequence csq, Cursor cursor)
58  throws IllegalArgumentException {
59  return Index.valueOf(TypeFormat.parseInt(csq, cursor));
60  }
61 
62  }
63 
69  public static final Configurable<Integer> UNIQUE = new Configurable<Integer>() {
70 
71  @Override
72  protected Integer getDefault() {
73  return 1024;
74  }
75 
76  @Override
77  protected Integer initialized(Integer value) {
78  return MathLib.min(value, 65536); // Hard-limiting
79  }
80 
81  @Override
82  protected Integer reconfigured(Integer oldCount, Integer newCount) {
83  throw new UnsupportedOperationException(
84  "Unicity reconfiguration not supported.");
85  }
86  };
87 
91  public static final Index ZERO = new Index(0);
92 
93  private static final long serialVersionUID = 0x600L; // Version.
94  private static final Index[] INSTANCES = new Index[UNIQUE.get()];
95  static {
96  INSTANCES[0] = ZERO;
97  for (int i = 1; i < INSTANCES.length; i++) {
98  INSTANCES[i] = new Index(i);
99  }
100  }
101 
111  public static Index valueOf(int value) {
112  return (value < INSTANCES.length) ? INSTANCES[value] : new Index(value);
113  }
114 
118  private final int value;
119 
123  private Index(int value) {
124  this.value = value;
125  }
126 
136  public int compareTo(Index that) {
137  return this.value - that.value;
138  }
139 
149  public int compareTo(int value) {
150  return this.value - value;
151  }
152 
157  public Index copy() {
158  return value < INSTANCES.length ? this : new Index(value);
159  }
160 
166  public double doubleValue() {
167  return (double) value;
168  }
169 
174  @Override
175  public boolean equals(Object obj) {
176  return (this.value < INSTANCES.length) ? (this == obj)
177  : ((obj instanceof Index) ? (((Index) obj).value == value)
178  : false);
179  }
180 
186  public float floatValue() {
187  return (float) value;
188  }
189 
193  @Override
194  public int hashCode() {
195  return value;
196  }
197 
203  public int intValue() {
204  return value;
205  }
206 
212  public boolean isZero() {
213  return this == ZERO;
214  }
215 
221  public long longValue() {
222  return value;
223  }
224 
228  public Index next() {
229  return Index.valueOf(value + 1);
230  }
231 
237  public Index previous() {
238  return Index.valueOf(value - 1);
239  }
240 
244  protected final Object readResolve() throws ObjectStreamException {
245  return Index.valueOf(value);
246  }
247 
253  @Override
254  public String toString() {
255  return TextContext.getFormat(Index.class).format(this);
256  }
257 
258  @Override
259  public Index value() {
260  return this;
261  }
262 }
javolution.text.TextContext.getFormat
static< T > TextFormat< T > getFormat(Class<? extends T > type)
Definition: TextContext.java:69
javolution.util.Index.hashCode
int hashCode()
Definition: Index.java:194
javolution
javolution.lang.Configurable
Definition: Configurable.java:78
javolution.lang.MathLib
Definition: MathLib.java:20
javolution.util.Index.previous
Index previous()
Definition: Index.java:237
javolution.util.Index.longValue
long longValue()
Definition: Index.java:221
javolution.util.Index.floatValue
float floatValue()
Definition: Index.java:186
javolution.util.Index.valueOf
static Index valueOf(int value)
Definition: Index.java:111
javolution.util.Index.isZero
boolean isZero()
Definition: Index.java:212
javolution.util.Index.equals
boolean equals(Object obj)
Definition: Index.java:175
Comparable
javolution.util.Index.Index
Index(int value)
Definition: Index.java:123
javolution.text.Cursor
Definition: Cursor.java:44
javolution.util.Index.compareTo
int compareTo(int value)
Definition: Index.java:149
javolution.lang
Definition: Configurable.java:9
Number
javolution.text
Definition: CharArray.java:9
javolution.util.Index.copy
Index copy()
Definition: Index.java:157
javolution.text.TypeFormat
Definition: TypeFormat.java:43
javolution.util.Index
Definition: Index.java:44
javolution.util.Index.doubleValue
double doubleValue()
Definition: Index.java:166
javolution.util.Index.Decimal.parse
Index parse(CharSequence csq, Cursor cursor)
Definition: Index.java:57
javolution.lang.Realtime
Definition: Realtime.java:59
javolution.text.TypeFormat.format
static Appendable format(boolean b, Appendable a)
Definition: TypeFormat.java:597
javolution.util.Index.intValue
int intValue()
Definition: Index.java:203
javolution.text.DefaultTextFormat
Definition: DefaultTextFormat.java:53
javolution.util.Index.compareTo
int compareTo(Index that)
Definition: Index.java:136
javolution.util.Index.next
Index next()
Definition: Index.java:228
javolution.text.TextFormat
Definition: TextFormat.java:52
javolution.util.Index.readResolve
final Object readResolve()
Definition: Index.java:244
javolution.util.Index.value
Index value()
Definition: Index.java:259
javolution.util.Index.toString
String toString()
Definition: Index.java:254
javolution.lang.ValueType
Definition: ValueType.java:63
javolution.util.Index.Decimal
Definition: Index.java:49
javolution.lang.Configurable.get
T get()
Definition: Configurable.java:170
javolution.util.Index.value
final int value
Definition: Index.java:118
javolution.lang.MathLib.min
static int min(int x, int y)
Definition: MathLib.java:1010
javolution.text.TextContext
Definition: TextContext.java:49
javolution.util.Index.Decimal.format
Appendable format(Index obj, Appendable dest)
Definition: Index.java:52
javolution.text.TypeFormat.parseInt
static int parseInt(CharSequence csq, int radix, Cursor cursor)
Definition: TypeFormat.java:248