Skip to content

Commit 7a821c9

Browse files
committed
update Java version
1 parent 6b5a271 commit 7a821c9

File tree

3 files changed

+95
-32
lines changed

3 files changed

+95
-32
lines changed

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

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,30 +1158,33 @@ public static <TKey, TValue> CBORObject FromObject(Map<TKey,
11581158

11591159
/**
11601160
* Generates a CBORObject from an arbitrary object. The following types are
1161-
* specially handled by this method: null , primitive types, strings,
1162-
* CBORObject , arbitrary-precision decimal , arbitrary-precision binary
1163-
* float , arbitrary-precision rational number, the custom
1164-
* arbitrary-precision integer , lists, arrays, enumerations (
1165-
* <code>Enum</code> objects), and maps. <p>In the .NET version, if the object
1166-
* is a type not specially handled by this method, returns a CBOR map
1167-
* with the values of each of its read/write properties (or all
1168-
* properties in the case of an anonymous type). Properties are
1169-
* converted to their camel-case names (meaning if a name starts with A
1170-
* to Z, that letter is lower-cased). If the property name begins with
1171-
* the word "Is", that word is deleted from the name. Also, .NET
1172-
* <code>Enum</code> objects will be converted to their integer values, and a
1173-
* multidimensional array is converted to an array of arrays.</p> <p>In
1174-
* the Java version, if the object is a type not specially handled by
1175-
* this method, this method checks the CBOR object for methods starting
1176-
* with the word "get" or "is" that take no parameters, and returns a
1177-
* CBOR map with one entry for each such method found. For each method
1178-
* found, the starting word "get" or "is" is deleted from its name, and
1179-
* the name is converted to camel case (meaning if a name starts with A
1180-
* to Z, that letter is lower-cased). Also, Java <code>Enum</code> objects
1181-
* will be converted to the result of their name method.</p> <p>If the
1182-
* input is a byte array, the byte array is copied to a new byte array.
1183-
* (This method can't be used to decode CBOR data from a byte array; for
1184-
* that, use the DecodeFromBytes method instead.).</p>
1161+
* specially handled by this method: null; primitive types; string;
1162+
* CBORObject; the <code>EDecimal</code>, <code>EFloat</code>, <code>EInteger</code>, and
1163+
* <code>ERational</code> classes in the new <code>PeterO.Numbers</code> library (in
1164+
* .NET) or the <code>com.github.peteroupc/numbers</code> artifact (in Java);
1165+
* the legacy <code>ExtendedDecimal</code>, <code>ExtendedFloat</code>,
1166+
* <code>ExtendedInteger</code>, and <code>ExtendedRational</code> classes in this
1167+
* library; arrays; enumerations (<code>Enum</code> objects); and maps. <p>In
1168+
* the .NET version, if the object is a type not specially handled by
1169+
* this method, returns a CBOR map with the values of each of its
1170+
* read/write properties (or all properties in the case of an anonymous
1171+
* type). Properties are converted to their camel-case names (meaning if
1172+
* a name starts with A to Z, that letter is lower-cased). If the
1173+
* property name begins with the word "Is", that word is deleted from
1174+
* the name. Also, .NET <code>Enum</code> objects will be converted to their
1175+
* integer values, and a multidimensional array is converted to an array
1176+
* of arrays.</p> <p>In the Java version, if the object is a type not
1177+
* specially handled by this method, this method checks the CBOR object
1178+
* for methods starting with the word "get" or "is" that take no
1179+
* parameters, and returns a CBOR map with one entry for each such
1180+
* method found. For each method found, the starting word "get" or "is"
1181+
* is deleted from its name, and the name is converted to camel case
1182+
* (meaning if a name starts with A to Z, that letter is lower-cased).
1183+
* Also, Java <code>Enum</code> objects will be converted to the result of
1184+
* their name method.</p> <p>If the input is a byte array, the byte
1185+
* array is copied to a new byte array. (This method can't be used to
1186+
* decode CBOR data from a byte array; for that, use the DecodeFromBytes
1187+
* method instead.).</p>
11851188
* @param obj An arbitrary object.
11861189
* @return A CBOR object corresponding to the given object. Returns
11871190
* CBORObject.Null if the object is null.
@@ -1205,6 +1208,22 @@ public static CBORObject FromObject(Object obj) {
12051208
if (obj instanceof BigInteger) {
12061209
return FromObject((BigInteger)obj);
12071210
}
1211+
EInteger eif = ((obj instanceof EInteger) ? (EInteger)obj : null);
1212+
if (eif != null) {
1213+
return FromObject(eif);
1214+
}
1215+
EDecimal edf = ((obj instanceof EDecimal) ? (EDecimal)obj : null);
1216+
if (edf != null) {
1217+
return FromObject(edf);
1218+
}
1219+
EFloat eff = ((obj instanceof EFloat) ? (EFloat)obj : null);
1220+
if (eff != null) {
1221+
return FromObject(eff);
1222+
}
1223+
ERational erf = ((obj instanceof ERational) ? (ERational)obj : null);
1224+
if (erf != null) {
1225+
return FromObject(erf);
1226+
}
12081227
ExtendedDecimal df = ((obj instanceof ExtendedDecimal) ? (ExtendedDecimal)obj : null);
12091228
if (df != null) {
12101229
return FromObject(df);

src/main/java/com/upokecenter/util/BigInteger.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@
1515
import com.upokecenter.numbers.*;
1616

1717
/**
18-
* An arbitrary-precision integer. <p><b>Thread safety:</b>Instances of this
19-
* class are immutable, so they are inherently safe for use by multiple
20-
* threads. Multiple instances of this object with the same value are
21-
* interchangeable, but they should be compared using the "Equals"
22-
* method rather than the "==" operator.</p>
18+
* <p><b>This class is largely obsolete. It will be replaced by a new version
19+
* of this class in a different namespace/package and library, called
20+
* <code>PeterO.Numbers.EInteger</code> in the <code>PeterO.Numbers</code> library
21+
* (in .NET), or <code>com.upokecenter.numbers.getEInteger()</code> in the
22+
* <code>com.github.peteroupc/numbers</code> artifact (in Java). This new
23+
* class can be used in the <code>CBORObject.FromObject(Object)</code> method,
24+
* among other things, but for method, among other things (by including
25+
* the new library in your code), but for this versionof the CBOR
26+
* library doesn't include any methods that explicitly take an
27+
* <code>EInteger</code> as a parameter or return value.</b></p> An
28+
* arbitrary-precision integer. <p><b>Thread safety:</b>Instances of
29+
* this class are immutable, so they are inherently safe for use by
30+
* multiple threads. Multiple instances of this object with the same
31+
* value are interchangeable, but they should be compared using the
32+
* "Equals" method rather than the "==" operator.</p>
2333
*/
2434
public final class BigInteger implements Comparable<BigInteger> {
2535
/**
@@ -202,9 +212,7 @@ public static BigInteger fromRadixSubstring(
202212
* @throws java.lang.NullPointerException The parameter {@code str} is null.
203213
* @throws java.lang.NumberFormatException The parameter {@code str} is in an invalid
204214
* format.
205-
* @deprecated Use EInteger from PeterO.Numbers/com.upokecenter.numbers.
206-
*/
207-
@Deprecated
215+
*/
208216
public static BigInteger fromString(String str) {
209217
return new BigInteger(EInteger.FromString(str));
210218
}

src/test/java/com/upokecenter/test/CBORObjectTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.Test;
66
import com.upokecenter.util.*;
77
import com.upokecenter.cbor.*;
8+
import com.upokecenter.numbers.*;
89

910
public class CBORObjectTest {
1011
private static String[] jsonFails = { "\"\\uxxxx\"",
@@ -2037,6 +2038,41 @@ public void TestFromJSONString() {
20372038
TestFailingJSON(jsonTemp);
20382039
}
20392040
@Test
2041+
public void TestEI() {
2042+
CBORObject cbor = CBORObject.FromObject(EInteger.FromString("100"));
2043+
Assert.assertEquals(CBORType.Number, cbor.getType());
2044+
{
2045+
String stringTemp = cbor.toString();
2046+
Assert.assertEquals(
2047+
"100",
2048+
stringTemp);
2049+
}
2050+
cbor = CBORObject.FromObject(EDecimal.FromString("200"));
2051+
Assert.assertEquals(CBORType.Number, cbor.getType());
2052+
{
2053+
String stringTemp = cbor.toString();
2054+
Assert.assertEquals(
2055+
"200",
2056+
stringTemp);
2057+
}
2058+
cbor = CBORObject.FromObject(EFloat.FromString("300"));
2059+
Assert.assertEquals(CBORType.Number, cbor.getType());
2060+
{
2061+
String stringTemp = cbor.toString();
2062+
Assert.assertEquals(
2063+
"300",
2064+
stringTemp);
2065+
}
2066+
cbor = CBORObject.FromObject(ERational.Create(1, 2));
2067+
Assert.assertEquals(CBORType.Number, cbor.getType());
2068+
{
2069+
String stringTemp = cbor.toString();
2070+
Assert.assertEquals(
2071+
"1/2",
2072+
stringTemp);
2073+
}
2074+
}
2075+
@Test
20402076
public void TestFromObject() {
20412077
CBORObject[] cborarray = new CBORObject[2];
20422078
cborarray[0] = CBORObject.False;

0 commit comments

Comments
 (0)