Skip to content

Commit 16faf26

Browse files
committed
update Java version
1 parent 8d80942 commit 16faf26

23 files changed

+1164
-1034
lines changed

src/main/java/com/upokecenter/cbor/CBORBigInteger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,8 @@ public Object Abs(Object obj) {
124124
public ERational AsExtendedRational(Object obj) {
125125
return ERational.FromEInteger((EInteger)obj);
126126
}
127+
128+
public boolean IsNegative(Object obj) {
129+
return ((EInteger)obj).signum() < 0;
130+
}
127131
}

src/main/java/com/upokecenter/cbor/CBORDouble.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,10 @@ public Object Abs(Object obj) {
148148
public ERational AsExtendedRational(Object obj) {
149149
return ERational.FromDouble(((Double)obj).doubleValue());
150150
}
151+
152+
public boolean IsNegative(Object obj) {
153+
double dbl = ((Double)obj).doubleValue();
154+
long lvalue = Double.doubleToRawLongBits(dbl);
155+
return (lvalue >> 63) != 0;
156+
}
151157
}

src/main/java/com/upokecenter/cbor/CBORExtendedDecimal.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,8 @@ public Object Abs(Object obj) {
157157
public ERational AsExtendedRational(Object obj) {
158158
return ERational.FromExtendedDecimal((EDecimal)obj);
159159
}
160+
161+
public boolean IsNegative(Object obj) {
162+
return ((EDecimal)obj).isNegative();
163+
}
160164
}

src/main/java/com/upokecenter/cbor/CBORExtendedFloat.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,8 @@ public Object Abs(Object obj) {
162162
public ERational AsExtendedRational(Object obj) {
163163
return ERational.FromExtendedFloat((EFloat)obj);
164164
}
165+
166+
public boolean IsNegative(Object obj) {
167+
return ((EFloat)obj).isNegative();
168+
}
165169
}

src/main/java/com/upokecenter/cbor/CBORExtendedRational.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,8 @@ public Object Abs(Object obj) {
157157
public ERational AsExtendedRational(Object obj) {
158158
return (ERational)obj;
159159
}
160+
161+
public boolean IsNegative(Object obj) {
162+
return ((ERational)obj).isNegative();
163+
}
160164
}

src/main/java/com/upokecenter/cbor/CBORInteger.java

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,18 @@
77
at: http://upokecenter.dreamhosters.com/articles/donate-now-2/
88
*/
99

10-
import com.upokecenter.util.*; import com.upokecenter.numbers.*;
10+
import com.upokecenter.util.*;
11+
import com.upokecenter.numbers.*;
1112

12-
class CBORInteger implements ICBORNumber
13-
{
14-
public boolean IsPositiveInfinity(Object obj) {
15-
return false;
16-
}
17-
18-
public boolean IsInfinity(Object obj) {
19-
return false;
20-
}
21-
22-
public boolean IsNegativeInfinity(Object obj) {
23-
return false;
13+
class CBORInteger implements ICBORNumber {
14+
public Object Abs(Object obj) {
15+
long val = (((Long)obj).longValue());
16+
return (val == Integer.MIN_VALUE) ? (EInteger.FromInt32(1).ShiftLeft(63)) : ((val < 0) ?
17+
-val : obj);
2418
}
2519

26-
public boolean IsNaN(Object obj) {
27-
return false;
20+
public EInteger AsBigInteger(Object obj) {
21+
return EInteger.FromInt64((((Long)obj).longValue()));
2822
}
2923

3024
public double AsDouble(Object obj) {
@@ -39,28 +33,24 @@ public EFloat AsExtendedFloat(Object obj) {
3933
return EFloat.FromInt64((((Long)obj).longValue()));
4034
}
4135

42-
public float AsSingle(Object obj) {
43-
return ((Long)obj).floatValue();
36+
public ERational AsExtendedRational(Object obj) {
37+
return ERational.FromInt64((((Long)obj).longValue()));
4438
}
4539

46-
public EInteger AsBigInteger(Object obj) {
47-
return EInteger.FromInt64((((Long)obj).longValue()));
40+
public int AsInt32(Object obj, int minValue, int maxValue) {
41+
long val = (((Long)obj).longValue());
42+
if (val >= minValue && val <= maxValue) {
43+
return (int)val;
44+
}
45+
throw new ArithmeticException("This Object's value is out of range");
4846
}
4947

5048
public long AsInt64(Object obj) {
5149
return (((Long)obj).longValue());
5250
}
5351

54-
public boolean CanFitInSingle(Object obj) {
55-
long intItem = (((Long)obj).longValue());
56-
if (intItem == Long.MIN_VALUE) {
57-
return true;
58-
}
59-
intItem = (intItem < 0) ? -intItem : intItem;
60-
while (intItem >= (1L << 24) && (intItem & 1) == 0) {
61-
intItem >>= 1;
62-
}
63-
return intItem < (1L << 24);
52+
public float AsSingle(Object obj) {
53+
return ((Long)obj).floatValue();
6454
}
6555

6656
public boolean CanFitInDouble(Object obj) {
@@ -84,48 +74,61 @@ public boolean CanFitInInt64(Object obj) {
8474
return true;
8575
}
8676

87-
public Object Negate(Object obj) {
88-
return (((((Long)obj).longValue())) == Long.MIN_VALUE) ? (EInteger.FromInt32(1).ShiftLeft(63)) :
89-
(-((((Long)obj).longValue())));
90-
}
91-
92-
public boolean CanTruncatedIntFitInInt64(Object obj) {
93-
return true;
77+
public boolean CanFitInSingle(Object obj) {
78+
long intItem = (((Long)obj).longValue());
79+
if (intItem == Long.MIN_VALUE) {
80+
return true;
81+
}
82+
intItem = (intItem < 0) ? -intItem : intItem;
83+
while (intItem >= (1L << 24) && (intItem & 1) == 0) {
84+
intItem >>= 1;
85+
}
86+
return intItem < (1L << 24);
9487
}
9588

9689
public boolean CanTruncatedIntFitInInt32(Object obj) {
9790
long val = (((Long)obj).longValue());
9891
return val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE;
9992
}
10093

101-
public boolean IsZero(Object obj) {
102-
return ((((Long)obj).longValue())) == 0;
94+
public boolean CanTruncatedIntFitInInt64(Object obj) {
95+
return true;
10396
}
10497

105-
public int Sign(Object obj) {
106-
long val = (((Long)obj).longValue());
107-
return (val == 0) ? 0 : ((val < 0) ? -1 : 1);
98+
public boolean IsInfinity(Object obj) {
99+
return false;
108100
}
109101

110102
public boolean IsIntegral(Object obj) {
111103
return true;
112104
}
113105

114-
public int AsInt32(Object obj, int minValue, int maxValue) {
115-
long val = (((Long)obj).longValue());
116-
if (val >= minValue && val <= maxValue) {
117-
return (int)val;
118-
}
119-
throw new ArithmeticException("This Object's value is out of range");
106+
public boolean IsNaN(Object obj) {
107+
return false;
120108
}
121109

122-
public Object Abs(Object obj) {
123-
long val = (((Long)obj).longValue());
124-
return (val == Integer.MIN_VALUE) ? (EInteger.FromInt32(1).ShiftLeft(63)) : ((val < 0) ?
125-
-val : obj);
110+
public boolean IsNegative(Object obj) {
111+
return ((((Long)obj).longValue())) < 0;
126112
}
127113

128-
public ERational AsExtendedRational(Object obj) {
129-
return ERational.FromInt64((((Long)obj).longValue()));
114+
public boolean IsNegativeInfinity(Object obj) {
115+
return false;
116+
}
117+
public boolean IsPositiveInfinity(Object obj) {
118+
return false;
119+
}
120+
121+
public boolean IsZero(Object obj) {
122+
return ((((Long)obj).longValue())) == 0;
123+
}
124+
125+
public Object Negate(Object obj) {
126+
return (((((Long)obj).longValue())) == Long.MIN_VALUE) ? (EInteger.FromInt32(1).ShiftLeft(63)) :
127+
(-((((Long)obj).longValue())));
128+
}
129+
130+
public int Sign(Object obj) {
131+
long val = (((Long)obj).longValue());
132+
return (val == 0) ? 0 : ((val < 0) ? -1 : 1);
130133
}
131134
}

src/main/java/com/upokecenter/cbor/CBORObject.java

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public final class CBORObject implements Comparable<CBORObject> {
9292
private static CBORObject ConstructSimpleValue(int v) {
9393
return new CBORObject(CBORObjectTypeSimpleValue, v);
9494
}
95+
private static CBORObject ConstructIntegerValue(int v) {
96+
return new CBORObject(CBORObjectTypeInteger, (long)v);
97+
}
9598

9699
/**
97100
* Represents the value false.
@@ -138,6 +141,12 @@ private static CBORObject ConstructSimpleValue(int v) {
138141
public static final CBORObject Undefined =
139142
CBORObject.ConstructSimpleValue(23);
140143

144+
/**
145+
* Gets a CBOR object for the number zero.
146+
*/
147+
public static final CBORObject Zero =
148+
CBORObject.ConstructIntegerValue(0);
149+
141150
static final int CBORObjectTypeArray = 4;
142151
static final int CBORObjectTypeBigInteger = 1; // all other integers
143152
static final int CBORObjectTypeByteString = 2;
@@ -350,6 +359,15 @@ public final Collection<CBORObject> getKeys() {
350359
throw new IllegalStateException("Not a map");
351360
}
352361

362+
/**
363+
* Gets a value indicating whether this object is a negative number.
364+
* @return True if this object is a negative number; otherwise, false.
365+
*/
366+
public final boolean isNegative() {
367+
ICBORNumber cn = NumberInterfaces[this.getItemType()];
368+
return (cn != null) && cn.IsNegative(this.getThisItem());
369+
}
370+
353371
/**
354372
* Gets the outermost tag for this CBOR data item, or -1 if the item is
355373
* untagged.
@@ -1172,31 +1190,33 @@ public static <TKey, TValue> CBORObject FromObject(Map<TKey,
11721190
* Generates a CBORObject from an arbitrary object. The following types are
11731191
* specially handled by this method: null; primitive types; string;
11741192
* CBORObject; the <code>EDecimal</code>, <code>EFloat</code>, <code>EInteger</code>, and
1175-
* <code>ERational</code> classes in the new <code>PeterO.Numbers</code> library (in
1176-
* .NET) or the <code>com.github.peteroupc/numbers</code> artifact (in Java);
1177-
* the legacy <code>ExtendedDecimal</code>, <code>ExtendedFloat</code>,
1178-
* <code>ExtendedInteger</code>, and <code>ExtendedRational</code> classes in this
1179-
* library; arrays; enumerations (<code>Enum</code> objects); and maps. <p>In
1180-
* the .NET version, if the object is a type not specially handled by
1181-
* this method, returns a CBOR map with the values of each of its
1182-
* read/write properties (or all properties in the case of an anonymous
1183-
* type). Properties are converted to their camel-case names (meaning if
1184-
* a name starts with A to Z, that letter is lower-cased). If the
1185-
* property name begins with the word "Is", that word is deleted from
1186-
* the name. Also, .NET <code>Enum</code> objects will be converted to their
1187-
* integer values, and a multidimensional array is converted to an array
1188-
* of arrays.</p> <p>In the Java version, if the object is a type not
1189-
* specially handled by this method, this method checks the CBOR object
1190-
* for methods starting with the word "get" or "is" that take no
1191-
* parameters, and returns a CBOR map with one entry for each such
1192-
* method found. For each method found, the starting word "get" or "is"
1193-
* is deleted from its name, and the name is converted to camel case
1194-
* (meaning if a name starts with A to Z, that letter is lower-cased).
1195-
* Also, Java <code>Enum</code> objects will be converted to the result of
1196-
* their name method.</p> <p>If the input is a byte array, the byte
1197-
* array is copied to a new byte array. (This method can't be used to
1198-
* decode CBOR data from a byte array; for that, use the DecodeFromBytes
1199-
* method instead.).</p>
1193+
* <code>ERational</code> classes in the new <a
1194+
* href='https://www.nuget.org/packages/PeterO.Numbers'><code>PeterO.Numbers</code></a>
1195+
* library (in .NET) or the <a
1196+
* href='https://github.com/peteroupc/numbers-java'><code>com.github.peteroupc/numbers</code></a>
1197+
* artifact (in Java); the legacy <code>ExtendedDecimal</code>,
1198+
* <code>ExtendedFloat</code>, <code>ExtendedInteger</code>, and
1199+
* <code>ExtendedRational</code> classes in this library; arrays; enumerations
1200+
* (<code>Enum</code> objects); and maps. <p>In the .NET version, if the
1201+
* object is a type not specially handled by this method, returns a CBOR
1202+
* map with the values of each of its read/write properties (or all
1203+
* properties in the case of an anonymous type). Properties are
1204+
* converted to their camel-case names (meaning if a name starts with A
1205+
* to Z, that letter is lower-cased). If the property name begins with
1206+
* the word "Is", that word is deleted from the name. Also, .NET
1207+
* <code>Enum</code> objects will be converted to their integer values, and a
1208+
* multidimensional array is converted to an array of arrays.</p> <p>In
1209+
* the Java version, if the object is a type not specially handled by
1210+
* this method, this method checks the CBOR object for methods starting
1211+
* with the word "get" or "is" that take no parameters, and returns a
1212+
* CBOR map with one entry for each such method found. For each method
1213+
* found, the starting word "get" or "is" is deleted from its name, and
1214+
* the name is converted to camel case (meaning if a name starts with A
1215+
* to Z, that letter is lower-cased). Also, Java <code>Enum</code> objects
1216+
* will be converted to the result of their name method.</p> <p>If the
1217+
* input is a byte array, the byte array is copied to a new byte array.
1218+
* (This method can't be used to decode CBOR data from a byte array; for
1219+
* that, use the DecodeFromBytes method instead.).</p>
12001220
* @param obj An arbitrary object.
12011221
* @return A CBOR object corresponding to the given object. Returns
12021222
* CBORObject.Null if the object is null.

src/main/java/com/upokecenter/cbor/CBORSingle.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,10 @@ public Object Abs(Object obj) {
151151
public ERational AsExtendedRational(Object obj) {
152152
return ERational.FromSingle(((Float)obj).floatValue());
153153
}
154+
155+
public boolean IsNegative(Object obj) {
156+
float val = ((Float)obj).floatValue();
157+
int ivalue = Float.floatToRawIntBits(val);
158+
return (ivalue >> 31) != 0;
159+
}
154160
}

src/main/java/com/upokecenter/cbor/CBORUtilities.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ public static String DoubleToString(double dbl) {
8080
return Double.toString((double)dbl);
8181
// TODO: Use this version in version 3, and preserve
8282
// TODO: negative zeros in that version
83-
// return EFloat.FromDouble(dbl).toString();
83+
// return EFloat.FromDouble(dbl).ToShortestString(EContext.Binary32);
8484
}
8585

8686
public static String SingleToString(float sing) {
8787
return Float.toString((float)sing);
8888
// TODO: Use this version in version 3, and preserve
8989
// TODO: negative zeros in that version
90-
// return EFloat.FromSingle(dbl).toString();
90+
// return EFloat.FromSingle(sing).ToShortestString(EContext.Binary64);
9191
}
9292

9393
public static EInteger BigIntegerFromSingle(float flt) {

src/main/java/com/upokecenter/cbor/ICBORNumber.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface ICBORNumber
1919

2020
boolean IsNaN(Object obj);
2121

22+
boolean IsNegative(Object obj);
23+
2224
double AsDouble(Object obj);
2325

2426
Object Negate(Object obj);

0 commit comments

Comments
 (0)