diff options
author | James Moger <james.moger@gitblit.com> | 2014-10-06 10:33:56 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-10-06 10:33:56 -0400 |
commit | 8eed45f5117065995cbceb7ae2a96c9057a806bd (patch) | |
tree | e906aaadf3547197c65ec64937a921b094186d50 /src | |
parent | 154684d64a61bc848a1ae57acc758470fa6303c5 (diff) | |
download | iciql-8eed45f5117065995cbceb7ae2a96c9057a806bd.tar.gz iciql-8eed45f5117065995cbceb7ae2a96c9057a806bd.zip |
Be more careful with primitive type rollover in alias instantiation
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/iciql/util/Utils.java | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/main/java/com/iciql/util/Utils.java b/src/main/java/com/iciql/util/Utils.java index bc636d1..92ca825 100644 --- a/src/main/java/com/iciql/util/Utils.java +++ b/src/main/java/com/iciql/util/Utils.java @@ -49,19 +49,19 @@ import com.iciql.IciqlException; public class Utils {
public static final AtomicLong COUNTER = new AtomicLong(0);
-
+
public static final AtomicInteger AS_COUNTER = new AtomicInteger(0);
private static final boolean MAKE_ACCESSIBLE = true;
private static final int BUFFER_BLOCK_SIZE = 4 * 1024;
-
+
public static synchronized int nextAsCount() {
// prevent negative values and use a threadsafe counter
int count = AS_COUNTER.incrementAndGet();
if (count == Integer.MAX_VALUE) {
count = 0;
- AS_COUNTER.set(count);
+ AS_COUNTER.set(count);
}
return count;
}
@@ -146,15 +146,15 @@ public class Utils { public static <T> T newObject(Class<T> clazz) {
// must create new instances
if (clazz == int.class || clazz == Integer.class) {
- return (T) new Integer((int) COUNTER.getAndIncrement());
+ return (T) new Integer((int) (COUNTER.getAndIncrement() % Integer.MAX_VALUE));
} else if (clazz == String.class) {
return (T) ("" + COUNTER.getAndIncrement());
} else if (clazz == long.class || clazz == Long.class) {
return (T) new Long(COUNTER.getAndIncrement());
} else if (clazz == short.class || clazz == Short.class) {
- return (T) new Short((short) COUNTER.getAndIncrement());
+ return (T) new Short((short) (COUNTER.getAndIncrement() % Short.MAX_VALUE));
} else if (clazz == byte.class || clazz == Byte.class) {
- return (T) new Byte((byte) COUNTER.getAndIncrement());
+ return (T) new Byte((byte) (COUNTER.getAndIncrement() % Byte.MAX_VALUE));
} else if (clazz == float.class || clazz == Float.class) {
return (T) new Float(COUNTER.getAndIncrement());
} else if (clazz == double.class || clazz == Double.class) {
@@ -397,7 +397,7 @@ public class Utils { /**
* Read a number of characters from a reader and close it.
- *
+ *
* @param in
* the reader
* @param length
@@ -430,7 +430,7 @@ public class Utils { /**
* Read a number of bytes from a stream and close it.
- *
+ *
* @param in
* the stream
* @param length
|