Skip to content

Commit a91eb22

Browse files
committed
update Java version
1 parent 3718dda commit a91eb22

File tree

4 files changed

+129
-32
lines changed

4 files changed

+129
-32
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ licensed under Creative Commons Zero (CC0):
1818
// TODO: In next major version, make .Keys and .Values read-only
1919
// TODO: Add ReadObject that combines Read and ToObject; similarly
2020
// for ReadJSON, FromJSONString, FromJSONBytes
21+
// TODO: In Java version add overloads for Class<T> in overloads
22+
// that take java.lang.reflect.getType()
23+
// TODO: Add TryGetValue method
2124

2225
/**
2326
* <p>Represents an object in Concise Binary Object Representation (CBOR) and

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

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,10 +611,20 @@ private static EInteger FloorDiv(EInteger a, EInteger n) {
611611
EInteger.FromInt32(-1).Subtract(a).Divide(n));
612612
}
613613

614+
private static long FloorDiv(long longA, int longN) {
615+
return longA >= 0 ? longA / longN : (-1-longA) /longN;
616+
}
617+
614618
private static EInteger FloorMod(EInteger a, EInteger n) {
615619
return a.Subtract(FloorDiv(a, n).Multiply(n));
616620
}
617621

622+
private static long FloorModLong(long longA, int longN) {
623+
{
624+
return longA - FloorDiv(longA, longN) * longN;
625+
}
626+
}
627+
618628
private static final int[] ValueNormalDays = {
619629
0, 31, 28, 31, 30, 31, 30,
620630
31, 31, 30,
@@ -663,6 +673,59 @@ public static void GetNormalizedPartProlepticGregorian(
663673
day = day.Add(count.Multiply(146097));
664674
year = year.Subtract(count.Multiply(400));
665675
}
676+
if (year.CanFitInInt32() && day.CanFitInInt32()) {
677+
long longYear = year.ToInt32Checked();
678+
int intDay = day.ToInt32Checked();
679+
if (longYear == 1970 && month == 1 && intDay > 0 && intDay >= 10957) {
680+
// Add enough days to move from 1/1970 to 1/2000
681+
longYear = 2000;
682+
intDay -= 10957;
683+
dayArray = ((longYear & 0x03) != 0 || (
684+
longYear % 100 == 0 &&
685+
longYear % 400 != 0)) ? ValueNormalDays : ValueLeapDays;
686+
}
687+
if (longYear == 2000 && month == 1 && intDay > 0 && intDay < 35064) {
688+
// Add enough days to move from 1/2000 to closest 4-year block
689+
// in the century.
690+
int intCount = intDay / 1461;
691+
intDay += intCount * 1461;
692+
longYear -= intCount * 4;
693+
dayArray = ((longYear & 0x03) != 0 || (
694+
longYear % 100 == 0 && longYear % 400 != 0)) ? ValueNormalDays :
695+
ValueLeapDays;
696+
}
697+
while (true) {
698+
int intDays = dayArray[month];
699+
if (intDay > 0 && intDay <= intDays) {
700+
break;
701+
}
702+
if (intDay > intDays) {
703+
intDay -= intDays;
704+
if (month == 12) {
705+
month = 1;
706+
++longYear;
707+
dayArray = ((longYear & 0x03) != 0 || (
708+
longYear % 100 == 0 &&
709+
longYear % 400 != 0)) ? ValueNormalDays : ValueLeapDays;
710+
} else {
711+
++month;
712+
}
713+
}
714+
if (intDay <= 0) {
715+
--month;
716+
if (month <= 0) {
717+
--longYear;
718+
month = 12;
719+
dayArray = ((longYear & 0x03) != 0 || (
720+
longYear % 100 == 0 && longYear % 400 != 0)) ? ValueNormalDays :
721+
ValueLeapDays;
722+
}
723+
intDay += dayArray[month];
724+
}
725+
}
726+
year = EInteger.FromInt64(longYear);
727+
day = EInteger.FromInt32(intDay);
728+
} else {
666729
while (true) {
667730
EInteger days = EInteger.FromInt32(dayArray[month]);
668731
if (day.signum() > 0 && day.compareTo(days) <= 0) {
@@ -686,14 +749,15 @@ public static void GetNormalizedPartProlepticGregorian(
686749
if (month <= 0) {
687750
year = year.Add(-1);
688751
month = 12;
689-
}
690-
dayArray = (year.Remainder(4).signum() != 0 || (
752+
dayArray = (year.Remainder(4).signum() != 0 || (
691753
year.Remainder(100).signum() == 0 &&
692754
year.Remainder(400).signum() != 0)) ? ValueNormalDays :
693-
ValueLeapDays;
755+
ValueLeapDays;
756+
}
694757
day = day.Add(dayArray[month]);
695758
}
696759
}
760+
}
697761
dest[0] = year;
698762
dest[1] = EInteger.FromInt32(month);
699763
dest[2] = day;
@@ -796,6 +860,29 @@ public static EInteger GetNumberOfDaysProlepticGregorian(
796860
return numDays;
797861
}
798862

863+
public static void BreakDownSecondsSinceEpoch(
864+
long seconds,
865+
EInteger[] year,
866+
int[] lesserFields) {
867+
EInteger[] normPart = new EInteger[3];
868+
long longDays = FloorDiv(seconds, 86400) + 1;
869+
long longSecondsInDay = FloorModLong(seconds, 86400);
870+
int secondsInDay = ((int)longSecondsInDay);
871+
GetNormalizedPartProlepticGregorian(
872+
EInteger.FromInt32(1970),
873+
1,
874+
EInteger.FromInt64(longDays),
875+
normPart);
876+
lesserFields[0] = normPart[1].ToInt32Checked();
877+
lesserFields[1] = normPart[2].ToInt32Checked();
878+
lesserFields[2] = secondsInDay / 3600;
879+
lesserFields[3] = (secondsInDay % 3600) / 60;
880+
lesserFields[4] = secondsInDay % 60;
881+
lesserFields[5] = 0;
882+
lesserFields[6] = 0;
883+
year[0] = normPart[0];
884+
}
885+
799886
public static void BreakDownSecondsSinceEpoch(
800887
EDecimal edec,
801888
EInteger[] year,

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -732,16 +732,18 @@ public static Object TypeToObject(CBORObject objThis, Type t,
732732
rawType.equals(HashMap.class)){
733733
if(typeArguments==null || typeArguments.length<2){
734734
HashMap alist=new HashMap();
735-
for(CBORObject cbor : objThis.getKeys()){
736-
CBORObject cborValue=objThis.get(cbor);
735+
for(Map.Entry<CBORObject, CBORObject> entry : objThis.getEntries()){
736+
CBORObject cbor=entry.getKey();
737+
CBORObject cborValue=entry.getValue();
737738
alist.put(cbor.ToObject(Object.class,mapper,options,depth+1),
738739
cborValue.ToObject(Object.class,mapper,options,depth+1));
739740
}
740741
return alist;
741742
} else {
742743
HashMap alist=new HashMap();
743-
for(CBORObject cbor : objThis.getKeys()){
744-
CBORObject cborValue=objThis.get(cbor);
744+
for(Map.Entry<CBORObject, CBORObject> entry : objThis.getEntries()){
745+
CBORObject cbor=entry.getKey();
746+
CBORObject cborValue=entry.getValue();
745747
alist.put(cbor.ToObject(typeArguments[0],mapper,options,depth+1),
746748
cborValue.ToObject(typeArguments[1],mapper,options,depth+1));
747749
}
@@ -752,16 +754,18 @@ public static Object TypeToObject(CBORObject objThis, Type t,
752754
rawType.equals(TreeMap.class) || rawType.equals(Map.class)){
753755
if(typeArguments==null || typeArguments.length<2){
754756
TreeMap alist=new TreeMap();
755-
for(CBORObject cbor : objThis.getKeys()){
756-
CBORObject cborValue=objThis.get(cbor);
757+
for(Map.Entry<CBORObject, CBORObject> entry : objThis.getEntries()){
758+
CBORObject cbor=entry.getKey();
759+
CBORObject cborValue=entry.getValue();
757760
alist.put(cbor.ToObject(Object.class,mapper,options,depth+1),
758761
cborValue.ToObject(Object.class,mapper,options,depth+1));
759762
}
760763
return alist;
761764
} else {
762765
TreeMap alist=new TreeMap();
763-
for(CBORObject cbor : objThis.getKeys()){
764-
CBORObject cborValue=objThis.get(cbor);
766+
for(Map.Entry<CBORObject, CBORObject> entry : objThis.getEntries()){
767+
CBORObject cbor=entry.getKey();
768+
CBORObject cborValue=entry.getValue();
765769
alist.put(cbor.ToObject(typeArguments[0],mapper,options,depth+1),
766770
cborValue.ToObject(typeArguments[1],mapper,options,depth+1));
767771
}
@@ -833,9 +837,8 @@ public static void BreakDownDateTime(java.util.Date bi,
833837
int nanoseconds=((int)(time%1000L));
834838
if(nanoseconds<0)nanoseconds=1000+nanoseconds;
835839
nanoseconds*=TicksDivFracSeconds;
836-
EDecimal edec=EDecimal.FromInt64(time).Divide(
837-
EDecimal.FromInt32(1000));
838-
CBORUtilities.BreakDownSecondsSinceEpoch(edec,year,lf);
840+
long seconds=time/1000;
841+
CBORUtilities.BreakDownSecondsSinceEpoch(seconds,year,lf);
839842
lf[5]=nanoseconds;
840843
}
841844

src/test/java/com/upokecenter/test/CBORTest.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public static void TestJsonUtf8One(byte[] bytes) {
121121
throw new NullPointerException("bytes");
122122
}
123123
String str = DataUtilities.GetUtf8String(bytes, false);
124-
byte[] bytes2 = new byte[bytes.length() + 2];
124+
byte[] bytes2 = new byte[bytes.length + 2];
125125
bytes2[0] = 0x22;
126-
System.arraycopy(bytes, 0, bytes2, 1, bytes.length());
126+
System.arraycopy(bytes, 0, bytes2, 1, bytes.length);
127127
bytes2[bytes2.length - 1] = 0x22;
128128
String str2 = CBORObject.FromJSONBytes(bytes2)
129129
.AsString();
@@ -442,9 +442,9 @@ public void TestByteArray() {
442442
EInteger[] tags = co.GetAllTags();
443443
Assert.assertEquals(0, tags.length);
444444
byte[] bytes = co.GetByteString();
445-
Assert.assertEquals(2, bytes.length());
446-
Assert.assertEquals(0x20, bytes.charAt(0));
447-
Assert.assertEquals(0x78, bytes.charAt(1));
445+
Assert.assertEquals(2, bytes.length);
446+
Assert.assertEquals(0x20, bytes[0]);
447+
Assert.assertEquals(0x78, bytes[1]);
448448
}
449449

450450
@Test
@@ -1022,7 +1022,7 @@ public static boolean TestEquivJSONOne(byte[] bytes) {
10221022
if (bytes == null) {
10231023
throw new NullPointerException("bytes");
10241024
}
1025-
if (!(bytes.length() > 0)) {
1025+
if (!(bytes.length > 0)) {
10261026
return false;
10271027
}
10281028
CBORObject cbo = CBORObject.FromJSONBytes(bytes);
@@ -1067,13 +1067,13 @@ public static boolean TestEquivJSONNumberOne(byte[] bytes) {
10671067
if (bytes == null) {
10681068
throw new NullPointerException("bytes");
10691069
}
1070-
if (!(bytes.length() > 0)) {
1070+
if (!(bytes.length > 0)) {
10711071
return false;
10721072
}
1073-
if (!((bytes.charAt(0) >= 0x30 && bytes.charAt(0) <= 0x39) || bytes.charAt(0) == (byte)'-')) {
1073+
if (!((bytes[0] >= 0x30 && bytes[0] <= 0x39) || bytes[0] == (byte)'-')) {
10741074
return false;
10751075
}
1076-
if (!(bytes.charAt(bytes.length() - 1) >= 0x30 && bytes.charAt(bytes.length() - 1) <=
1076+
if (!(bytes[bytes.length - 1] >= 0x30 && bytes[bytes.length - 1] <=
10771077
0x39)) {
10781078
return false;
10791079
}
@@ -1099,13 +1099,13 @@ public static boolean TestEquivJSONNumberDecimal128One(byte[] bytes) {
10991099
if (bytes == null) {
11001100
throw new NullPointerException("bytes");
11011101
}
1102-
if (!(bytes.length() > 0)) {
1102+
if (!(bytes.length > 0)) {
11031103
return false;
11041104
}
1105-
if (!((bytes.charAt(0) >= 0x30 && bytes.charAt(0) <= 0x39) || bytes.charAt(0) == (byte)'-')) {
1105+
if (!((bytes[0] >= 0x30 && bytes[0] <= 0x39) || bytes[0] == (byte)'-')) {
11061106
return false;
11071107
}
1108-
if (!(bytes.charAt(bytes.length() - 1) >= 0x30 && bytes.charAt(bytes.length() - 1) <=
1108+
if (!(bytes[bytes.length - 1] >= 0x30 && bytes[bytes.length - 1] <=
11091109
0x39)) {
11101110
return false;
11111111
}
@@ -2960,8 +2960,9 @@ public static boolean TestAsNumberMultiplyDivideOne(
29602960
if (!on2.IsZero() && !on1a.IsFinite()) {
29612961
Assert.fail("on1a is not finite\n" +
29622962
"o1=" + on1 + "\n" + "o2=" + on2 + "\n" +
2963-
"{\nbyte[] bytes1 = " + TestCommon.ToByteArrayString(eb1) + ";\n" +
2964-
"byte[] bytes2 =" + TestCommon.ToByteArrayString(eb2) + ";\n" +
2963+
"{\nbyte[] by" +
2964+
"tes1 = " + TestCommon.ToByteArrayString(eb1) + ";\n" +
2965+
"byte[] by" + "tes2 =" + TestCommon.ToByteArrayString(eb2) + ";\n" +
29652966
"TestAsNumberMultiplyDivideOne(\nCBORObject.D" +
29662967
"ecodeFromBytes(bytes1),\n" +
29672968
"CBORObject.DecodeFromBytes(bytes2));\n}\n");
@@ -3776,10 +3777,10 @@ public void TestIndefLengthMore() {
37763777
TestSucceedingDecode(bytes);
37773778
bytes = new byte[] { 0x7f, 0x63, (byte)0xe2, (byte)0x80, (byte)0x80, (byte)0xff };
37783779
TestSucceedingDecode(bytes);
3779-
bytes = new byte[] { 0x7f, 0x64, (byte)0xe2, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0xff };
3780+
bytes = new byte[] { 0x7f, 0x64, (byte)0xf2, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0xff };
37803781
TestSucceedingDecode(bytes);
3781-
// Disallow splitting code points in an indefinite-length
3782-
// text String
3782+
// Disallow splitting code points in indefinite-length
3783+
// text strings
37833784
bytes = new byte[] { 0x7f, 0x61, (byte)0xc2, 0x61, (byte)0x80, (byte)0xff };
37843785
TestFailingDecode(bytes);
37853786
bytes = new byte[] { 0x7f, 0x61, (byte)0xe2, 0x62, (byte)0x80, (byte)0x80, (byte)0xff };
@@ -3802,7 +3803,10 @@ public void TestIndefLengthMore() {
38023803
TestFailingDecode(bytes);
38033804
bytes = new byte[] { 0x7f, 0x62, (byte)0xe2, (byte)0x80, 0x62, (byte)0x80, 0x20, (byte)0xff };
38043805
TestFailingDecode(bytes);
3805-
bytes = new byte[] { 0x7f, 0x63, (byte)0xf2, (byte)0x80, (byte)0x80, 0x62, (byte)0x80, 0x20, (byte)0xff };
3806+
bytes = new byte[] {
3807+
0x7f, 0x63, (byte)0xf2, (byte)0x80, (byte)0x80, 0x62, (byte)0x80, 0x20,
3808+
(byte)0xff,
3809+
};
38063810
TestFailingDecode(bytes);
38073811
}
38083812

0 commit comments

Comments
 (0)