From 65adb5928a3d1c5ee85898ea3b3edb9bad2cb2b1 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Fri, 15 Dec 2017 12:17:26 +0100 Subject: [PATCH] [#6922] Upgrade the jOOU dependency to version 0.9.3 --- jOOQ/src/main/java/org/jooq/types/UByte.java | 45 ++++- .../main/java/org/jooq/types/UInteger.java | 61 +++++-- jOOQ/src/main/java/org/jooq/types/ULong.java | 171 ++++++++++++++---- jOOQ/src/main/java/org/jooq/types/UShort.java | 37 +++- 4 files changed, 249 insertions(+), 65 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/types/UByte.java b/jOOQ/src/main/java/org/jooq/types/UByte.java index cdd210d690..fed7999475 100644 --- a/jOOQ/src/main/java/org/jooq/types/UByte.java +++ b/jOOQ/src/main/java/org/jooq/types/UByte.java @@ -23,6 +23,7 @@ import java.math.BigInteger; * * @author Lukas Eder * @author Ed Schaller + * @author Jens Nerche */ public final class UByte extends UNumber implements Comparable { @@ -48,6 +49,18 @@ public final class UByte extends UNumber implements Comparable { */ public static final short MAX_VALUE = 0xff; + /** + * A constant holding the minimum value an unsigned byte can + * have as UByte, 0. + */ + public static final UByte MIN = valueOf(0x00); + + /** + * A constant holding the maximum value an unsigned byte can + * have as UByte, 28-1. + */ + public static final UByte MAX = valueOf(0xff); + /** * The value modelling the content of this unsigned byte */ @@ -63,6 +76,7 @@ public final class UByte extends UNumber implements Comparable { for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) ret[i & MAX_VALUE] = new UByte((byte) i); + return ret; } @@ -178,9 +192,9 @@ public final class UByte extends UNumber implements Comparable { * @throws NumberFormatException if value is out of range */ private static short rangeCheck(short value) throws NumberFormatException { - if (value < MIN_VALUE || value > MAX_VALUE) { + if (value < MIN_VALUE || value > MAX_VALUE) throw new NumberFormatException("Value is out of range : " + value); - } + return value; } @@ -192,9 +206,9 @@ public final class UByte extends UNumber implements Comparable { * @throws NumberFormatException if value is out of range */ private static short rangeCheck(int value) throws NumberFormatException { - if (value < MIN_VALUE || value > MAX_VALUE) { + if (value < MIN_VALUE || value > MAX_VALUE) throw new NumberFormatException("Value is out of range : " + value); - } + return (short) value; } @@ -206,9 +220,9 @@ public final class UByte extends UNumber implements Comparable { * @throws NumberFormatException if value is out of range */ private static short rangeCheck(long value) throws NumberFormatException { - if (value < MIN_VALUE || value > MAX_VALUE) { + if (value < MIN_VALUE || value > MAX_VALUE) throw new NumberFormatException("Value is out of range : " + value); - } + return (short) value; } @@ -253,9 +267,8 @@ public final class UByte extends UNumber implements Comparable { public boolean equals(Object obj) { if (this == obj) return true; - if (obj instanceof UByte) { + if (obj instanceof UByte) return value == ((UByte) obj).value; - } return false; } @@ -274,4 +287,20 @@ public final class UByte extends UNumber implements Comparable { public BigInteger toBigInteger() { return BigInteger.valueOf(value); } + + public UByte add(UByte val) throws NumberFormatException { + return valueOf(value + val.value); + } + + public UByte add(int val) throws NumberFormatException { + return valueOf(value + val); + } + + public UByte subtract(final UByte val) { + return valueOf(value - val.value); + } + + public UByte subtract(final int val) { + return valueOf(value - val); + } } diff --git a/jOOQ/src/main/java/org/jooq/types/UInteger.java b/jOOQ/src/main/java/org/jooq/types/UInteger.java index 5b9afa0bbd..d96a3c4d94 100644 --- a/jOOQ/src/main/java/org/jooq/types/UInteger.java +++ b/jOOQ/src/main/java/org/jooq/types/UInteger.java @@ -23,8 +23,10 @@ import java.math.BigInteger; * * @author Lukas Eder * @author Ed Schaller + * @author Jens Nerche */ public final class UInteger extends UNumber implements Comparable { + private static final Class CLASS = UInteger.class; private static final String CLASS_NAME = CLASS.getName(); @@ -60,6 +62,18 @@ public final class UInteger extends UNumber implements Comparable { */ public static final long MAX_VALUE = 0xffffffffL; + /** + * A constant holding the minimum value an unsigned int can + * have as UInteger, 0. + */ + public static final UInteger MIN = valueOf(MIN_VALUE); + + /** + * A constant holding the maximum value an unsigned int can + * have as UInteger, 232-1. + */ + public static final UInteger MAX = valueOf(MAX_VALUE); + /** * The value modelling the content of this unsigned int */ @@ -89,11 +103,12 @@ public final class UInteger extends UNumber implements Comparable { } if (prop == null) return DEFAULT_PRECACHE_SIZE; - if (prop.length() <= 0) { - // empty value - // FIXME: should we log this somewhere? + + // empty value + // FIXME: should we log this somewhere? + if (prop.length() <= 0) return DEFAULT_PRECACHE_SIZE; - } + try { propParsed = Long.parseLong(prop); } @@ -102,13 +117,15 @@ public final class UInteger extends UNumber implements Comparable { // FIXME: should we log this somewhere? return DEFAULT_PRECACHE_SIZE; } + // treat negative value as no cache... if (propParsed < 0) return 0; - if (propParsed > Integer.MAX_VALUE) { - // FIXME: should we log this somewhere + + // FIXME: should we log this somewhere? + if (propParsed > Integer.MAX_VALUE) return Integer.MAX_VALUE; - } + return (int) propParsed; } @@ -123,9 +140,11 @@ public final class UInteger extends UNumber implements Comparable { if (precacheSize <= 0) return null; + ret = new UInteger[precacheSize]; for (int i = 0; i < precacheSize; i++) ret[i] = new UInteger(i); + return ret; } @@ -135,7 +154,7 @@ public final class UInteger extends UNumber implements Comparable { * constructor without unnecessary value checks. * * @param value The value to wrap - * @param unused Unused paramater to distinguish between this and the + * @param unused Unused parameter to distinguish between this and the * deprecated public constructor. */ private UInteger(long value, boolean unused) { @@ -151,6 +170,7 @@ public final class UInteger extends UNumber implements Comparable { private static UInteger getCached(long value) { if (VALUES != null && value < VALUES.length) return VALUES[(int) value]; + return null; } @@ -162,6 +182,7 @@ public final class UInteger extends UNumber implements Comparable { if ((cached = getCached(value)) != null) return cached; + return new UInteger(value, true); } @@ -231,9 +252,9 @@ public final class UInteger extends UNumber implements Comparable { * @throws NumberFormatException if value is out of range */ private static long rangeCheck(long value) throws NumberFormatException { - if (value < MIN_VALUE || value > MAX_VALUE) { + if (value < MIN_VALUE || value > MAX_VALUE) throw new NumberFormatException("Value is out of range : " + value); - } + return value; } @@ -251,6 +272,7 @@ public final class UInteger extends UNumber implements Comparable { rangeCheck(value); if ((cached = getCached(value)) != null) return cached; + return this; } @@ -288,9 +310,8 @@ public final class UInteger extends UNumber implements Comparable { public boolean equals(Object obj) { if (this == obj) return true; - if (obj instanceof UInteger) { + if (obj instanceof UInteger) return value == ((UInteger) obj).value; - } return false; } @@ -304,4 +325,20 @@ public final class UInteger extends UNumber implements Comparable { public int compareTo(UInteger o) { return (value < o.value ? -1 : (value == o.value ? 0 : 1)); } + + public UInteger add(final UInteger val) { + return valueOf(value + val.value); + } + + public UInteger add(final int val) { + return valueOf(value + val); + } + + public UInteger subtract(final UInteger val) { + return valueOf(value - val.value); + } + + public UInteger subtract(final int val) { + return valueOf(value - val); + } } diff --git a/jOOQ/src/main/java/org/jooq/types/ULong.java b/jOOQ/src/main/java/org/jooq/types/ULong.java index a80aebbc6e..900ca3c286 100644 --- a/jOOQ/src/main/java/org/jooq/types/ULong.java +++ b/jOOQ/src/main/java/org/jooq/types/ULong.java @@ -21,6 +21,8 @@ import java.math.BigInteger; * The unsigned long type * * @author Lukas Eder + * @author Jens Nerche + * @author Ivan Sokolov */ public final class ULong extends UNumber implements Comparable { @@ -47,10 +49,22 @@ public final class ULong extends UNumber implements Comparable { */ public static final BigInteger MAX_VALUE_LONG = new BigInteger("9223372036854775808"); + /** + * A constant holding the minimum value an unsigned long can + * have as ULong, 0. + */ + public static final ULong MIN = valueOf(MIN_VALUE.longValue()); + + /** + * A constant holding the maximum value + 1 an signed long can + * have as ULong, 263. + */ + public static final ULong MAX = valueOf(MAX_VALUE); + /** * The value modelling the content of this unsigned long */ - private final BigInteger value; + private final long value; /** * Create an unsigned long @@ -81,6 +95,12 @@ public final class ULong extends UNumber implements Comparable { return new ULong(value); } + public static int compare(long x, long y) { + x += Long.MIN_VALUE; + y += Long.MIN_VALUE; + return (x < y) ? -1 : ((x == y) ? 0 : 1); + } + /** * Create an unsigned long * @@ -88,8 +108,10 @@ public final class ULong extends UNumber implements Comparable { * of an unsigned long */ private ULong(BigInteger value) throws NumberFormatException { - this.value = value; - rangeCheck(); + if (value.compareTo(MIN_VALUE) < 0 || value.compareTo(MAX_VALUE) > 0) + throw new NumberFormatException(); + else + this.value = value.longValue(); } /** @@ -98,12 +120,7 @@ public final class ULong extends UNumber implements Comparable { * (uint) 18446744073709551615 */ private ULong(long value) { - if (value >= 0) { - this.value = BigInteger.valueOf(value); - } - else { - this.value = BigInteger.valueOf(value & Long.MAX_VALUE).add(MAX_VALUE_LONG); - } + this.value = value; } /** @@ -113,64 +130,140 @@ public final class ULong extends UNumber implements Comparable { * parsable unsigned long. */ private ULong(String value) throws NumberFormatException { - this.value = new BigInteger(value); - rangeCheck(); - } + if (value == null) + throw new NumberFormatException("null"); - private void rangeCheck() throws NumberFormatException { - if (value.compareTo(MIN_VALUE) < 0 || value.compareTo(MAX_VALUE) > 0) { - throw new NumberFormatException("Value is out of range : " + value); + int length = value.length(); + + if (length == 0) + throw new NumberFormatException("Empty input string"); + + if (value.charAt(0) == '-') + throw new NumberFormatException( + String.format("Illegal leading minus sign on unsigned string %s", value)); + + if (length <= 18) { + this.value = Long.parseLong(value, 10); + return; } + + final long first = Long.parseLong(value.substring(0, length - 1), 10); + final int second = Character.digit(value.charAt(length - 1), 10); + if (second < 0) + throw new NumberFormatException("Bad digit at end of " + value); + + long result = first * 10 + second; + if (compare(result, first) < 0) + throw new NumberFormatException( + String.format("String value %s exceeds range of unsigned long", value)); + + this.value = result; } @Override public int intValue() { - return value.intValue(); + return (int) value; } @Override public long longValue() { - return value.longValue(); - } - - @Override - public float floatValue() { - return value.floatValue(); - } - - @Override - public double doubleValue() { - return value.doubleValue(); - } - - @Override - public BigInteger toBigInteger() { return value; } + @Override + public float floatValue() { + if (value < 0) + return ((float) (value & Long.MAX_VALUE)) + Long.MAX_VALUE; + else + return value; + } + + @Override + public double doubleValue() { + if (value < 0) + return ((double) (value & Long.MAX_VALUE)) + Long.MAX_VALUE; + else + return value; + } + @Override public int hashCode() { - return value.hashCode(); + return Long.valueOf(value).hashCode(); } @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj instanceof ULong) { - return value.equals(((ULong) obj).value); - } + if (obj instanceof ULong) + return value == ((ULong) obj).value; return false; } @Override public String toString() { - return value.toString(); + if (value >= 0) + return Long.toString(value); + else + return BigInteger.valueOf(value & Long.MAX_VALUE).add(MAX_VALUE_LONG).toString(); } @Override public int compareTo(ULong o) { - return value.compareTo(o.value); + return compare(value, o.value); + } + + public ULong add(ULong val) throws NumberFormatException { + if (value < 0 && val.value < 0) + throw new NumberFormatException(); + + final long result = value + val.value; + if ((value < 0 || val.value < 0) && result >= 0) + throw new NumberFormatException(); + + return valueOf(result); + } + + public ULong add(int val) throws NumberFormatException { + return add((long) val); + } + + public ULong add(long val) throws NumberFormatException { + if (val < 0) + return subtract(Math.abs(val)); + + final long result = value + val; + if (value < 0 && result >= 0) + throw new NumberFormatException(); + + return valueOf(result); + } + + public ULong subtract(final ULong val) { + if (this.compareTo(val) < 0) + throw new NumberFormatException(); + + final long result = value - val.value; + if (value < 0 && result >= 0) + throw new NumberFormatException(); + + return valueOf(result); + } + + public ULong subtract(final int val) { + return subtract((long) val); + } + + public ULong subtract(final long val) { + if (val < 0) + return add(-val); + + if (compare(value, val) < 0) + throw new NumberFormatException(); + + final long result = value - val; + if (value < 0 && result >= 0) + throw new NumberFormatException(); + + return valueOf(result); } } diff --git a/jOOQ/src/main/java/org/jooq/types/UShort.java b/jOOQ/src/main/java/org/jooq/types/UShort.java index 11fd15c1a7..514cb81aeb 100644 --- a/jOOQ/src/main/java/org/jooq/types/UShort.java +++ b/jOOQ/src/main/java/org/jooq/types/UShort.java @@ -21,6 +21,7 @@ import java.math.BigInteger; * The unsigned short type * * @author Lukas Eder + * @author Jens Nerche */ public final class UShort extends UNumber implements Comparable { @@ -41,6 +42,18 @@ public final class UShort extends UNumber implements Comparable { */ public static final int MAX_VALUE = 0xffff; + /** + * A constant holding the minimum value an unsigned short can + * have as UShort, 0. + */ + public static final UShort MIN = valueOf(MIN_VALUE); + + /** + * A constant holding the maximum value an unsigned short can + * have as UShort, 216-1. + */ + public static final UShort MAX = valueOf(MAX_VALUE); + /** * The value modelling the content of this unsigned short */ @@ -107,9 +120,8 @@ public final class UShort extends UNumber implements Comparable { } private void rangeCheck() throws NumberFormatException { - if (value < MIN_VALUE || value > MAX_VALUE) { + if (value < MIN_VALUE || value > MAX_VALUE) throw new NumberFormatException("Value is out of range : " + value); - } } @Override @@ -144,11 +156,8 @@ public final class UShort extends UNumber implements Comparable { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj instanceof UShort) { + if (obj instanceof UShort) return value == ((UShort) obj).value; - } return false; } @@ -162,4 +171,20 @@ public final class UShort extends UNumber implements Comparable { public int compareTo(UShort o) { return (value < o.value ? -1 : (value == o.value ? 0 : 1)); } + + public UShort add(UShort val) throws NumberFormatException { + return valueOf(value + val.value); + } + + public UShort add(int val) throws NumberFormatException { + return valueOf(value + val); + } + + public UShort subtract(final UShort val) { + return valueOf(value - val.value); + } + + public UShort subtract(final int val) { + return valueOf(value - val); + } }