Skip to content

Commit 1ce3318

Browse files
committed
update Java version
1 parent c32a258 commit 1ce3318

File tree

6 files changed

+283
-11
lines changed

6 files changed

+283
-11
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7309,12 +7309,8 @@ private static int MapCompare(
73097309
if (listACount != listBCount) {
73107310
return listACount < listBCount ? -1 : 1;
73117311
}
7312-
ArrayList<CBORObject> sortedASet = new ArrayList<CBORObject>(mapA.keySet());
7313-
ArrayList<CBORObject> sortedBSet = new ArrayList<CBORObject>(mapB.keySet());
7314-
// System.out.println("---sorting mapA's keys");
7315-
// java.util.Collections.sort(sortedASet);
7316-
// System.out.println("---sorting mapB's keys");
7317-
// java.util.Collections.sort(sortedBSet);
7312+
ArrayList<CBORObject> sortedASet = new ArrayList<CBORObject>(PropertyMap.GetSortedKeys(mapA));
7313+
ArrayList<CBORObject> sortedBSet = new ArrayList<CBORObject>(PropertyMap.GetSortedKeys(mapB));
73187314
// System.out.println("---done sorting");
73197315
listACount = sortedASet.size();
73207316
listBCount = sortedBSet.size();

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

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ licensed under Creative Commons Zero (CC0):
1313
import java.lang.reflect.*;
1414
import java.math.BigInteger;
1515
import java.math.BigDecimal;
16+
import java.util.*;
1617
import java.util.AbstractList;
1718
import java.util.AbstractMap;
1819
import java.util.ArrayList;
@@ -316,10 +317,145 @@ private static List<MethodData> GetPropertyList(final Class<?> t, boolean setter
316317
return Collections.unmodifiableMap(dict).entrySet();
317318
}
318319

320+
private static final class LinkedListKeySet<TKey>
321+
extends AbstractSet<TKey> {
322+
private final LinkedList list;
323+
public LinkedListKeySet(LinkedList<TKey> list){
324+
this.list=list;
325+
}
326+
public int size() {
327+
return this.list.size();
328+
}
329+
public Iterator<TKey> iterator() {
330+
return this.list.iterator();
331+
}
332+
}
333+
334+
public static final class OrderedMapSet<TKey, TValue>
335+
extends AbstractSet<Map.Entry<TKey, TValue>> {
336+
private final Map<TKey, TValue> dict;
337+
private final LinkedList<TKey> list;
338+
public OrderedMapSet(LinkedList<TKey> list, Map<TKey, TValue> dict){
339+
this.list=list;
340+
this.dict=dict;
341+
}
342+
public Iterator<Map.Entry<TKey, TValue>> iterator() {
343+
return new OrderedMapIterator<TKey, TValue>(list.iterator(), dict);
344+
}
345+
public int size() {
346+
return list.size();
347+
}
348+
}
349+
350+
public static final class OrderedMapIterator<TKey, TValue>
351+
implements Iterator<Map.Entry<TKey, TValue>> {
352+
private final Map<TKey, TValue> dict;
353+
private final Iterator<TKey> iter;
354+
public OrderedMapIterator(Iterator<TKey> iter, Map<TKey, TValue> dict){
355+
this.iter=iter;
356+
this.dict=dict;
357+
}
358+
public boolean hasNext() {
359+
return this.iter.hasNext();
360+
}
361+
public Map.Entry<TKey, TValue> next() {
362+
TKey k=iter.next();
363+
return new AbstractMap.SimpleImmutableEntry<TKey, TValue>(k,dict.get(k));
364+
}
365+
public void remove() {
366+
this.iter.remove();
367+
}
368+
}
369+
370+
public static final class OrderedMap<TKey, TValue>
371+
implements Map<TKey, TValue> {
372+
private final SortedMap<TKey, TValue> dict;
373+
private final LinkedList<TKey> list;
374+
public OrderedMap() {
375+
this.dict = new TreeMap<TKey, TValue>();
376+
this.list = new LinkedList<TKey>();
377+
}
378+
public Set<Map.Entry<TKey, TValue>> entrySet() {
379+
return new OrderedMapSet(list,dict);
380+
}
381+
public Set<TKey> keySet() {
382+
return new LinkedListKeySet(list);
383+
}
384+
public Set<TKey> sortedKeys() {
385+
return dict.keySet();
386+
}
387+
public void clear() {
388+
this.list.clear();
389+
this.dict.clear();
390+
}
391+
public boolean containsKey(Object k) {
392+
return this.dict.containsKey(k);
393+
}
394+
public boolean containsValue(Object v) {
395+
return this.dict.containsValue(v);
396+
}
397+
public boolean equals(Object v) {
398+
return this.dict.equals(v);
399+
}
400+
public int hashCode() {
401+
return this.dict.hashCode();
402+
}
403+
public boolean isEmpty() {
404+
return this.dict.isEmpty();
405+
}
406+
public TValue remove(Object k) {
407+
TValue ret=this.dict.remove(k);
408+
this.list.remove(k);
409+
return ret;
410+
}
411+
public int size() {
412+
return this.dict.size();
413+
}
414+
public String toString() {
415+
return this.dict.toString();
416+
}
417+
public Collection<TValue> values() {
418+
List<TValue> ret=new ArrayList<TValue>();
419+
for(Map.Entry<TKey,TValue> entry : this.entrySet()){
420+
ret.add(entry.getValue());
421+
}
422+
return ret;
423+
}
424+
public TValue get(Object k) {
425+
return this.dict.get(k);
426+
}
427+
public TValue put(TKey k, TValue v) {
428+
if (this.containsKey(k)){
429+
return this.dict.put(k,v);
430+
} else {
431+
TValue ret=this.dict.put(k,v);
432+
this.list.add(k);
433+
return ret;
434+
}
435+
}
436+
public void putAll(Map<? extends TKey,? extends TValue> m) {
437+
for(TKey k : m.keySet()){
438+
this.put(k,m.get(k));
439+
}
440+
}
441+
}
442+
319443
public static Map<CBORObject,CBORObject> NewOrderedDict() {
320-
return new LinkedHashMap<CBORObject,CBORObject>();
444+
return new OrderedMap<CBORObject,CBORObject>();
321445
}
322446

447+
public static <TKey, TValue> Collection<TKey> GetSortedKeys(
448+
Map<TKey, TValue> dict) {
449+
if (dict instanceof OrderedMap<?, ?>) {
450+
return ((OrderedMap<TKey, TValue>)dict).sortedKeys();
451+
}
452+
if (dict instanceof SortedMap<?, ?>) {
453+
return ((SortedMap<TKey, TValue>)dict).keySet();
454+
}
455+
throw new IllegalStateException("Internal error: Map doesn't" +
456+
"\u0020support sorted keys");
457+
}
458+
323459
/**
324460
* <p>FromArray.</p>
325461
*

src/test/java/com/upokecenter/test/CBORGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private void Generate(IRandomGenExtended r, int depth, ByteWriter bs) {
177177
} else if (depth > 6) {
178178
len = r.GetInt32(100) == 0 ? 1 : 0;
179179
} else if (depth > 2) {
180-
len = r.GetInt32(16);
180+
len = r.GetInt32(16) + 1;
181181
}
182182
// TODO: Ensure key uniqueness
183183
if (r.GetInt32(2) == 0) {
@@ -216,7 +216,7 @@ private void Generate(IRandomGenExtended r, int depth, ByteWriter bs) {
216216
} else if (depth > 6) {
217217
len = r.GetInt32(100) == 0 ? 1 : 0;
218218
} else if (depth > 2) {
219-
len = r.GetInt32(3);
219+
len = r.GetInt32(3) + 1;
220220
}
221221
if (depth > 6) {
222222
len = r.GetInt32(100) < 50 ? 1 : (r.GetInt32(100) < 10 ? 2 : 0);

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

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11035,6 +11035,145 @@ public void TestFromJsonStringLongKindIntOrFloat2() {
1103511035
}
1103611036
}
1103711037

11038+
@Test
11039+
public void TestRoundTripRegressions() {
11040+
{
11041+
CBOREncodeOptions options = new CBOREncodeOptions("allowduplicatekeys=1;keepkeyorder=1");
11042+
byte[] bytes = new byte[] {
11043+
(byte)0xba, 0x00, 0x00, 0x00, 0x03,
11044+
(byte)0xf9,
11045+
(byte)0x83, 0x1d,
11046+
(byte)0xda,
11047+
(byte)0xb6,
11048+
(byte)0xda, 0x50, 0x56, 0x1a, 0x50,
11049+
(byte)0xe3, 0x2c, 0x7a, 0x16,
11050+
(byte)0xfa, 0x50, 0x32, 0x73, 0x07,
11051+
(byte)0xfa, (byte)0xb9, 0x2d, 0x73, (byte)0xce, 0x38, (byte)0xd0,
11052+
};
11053+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
11054+
}
11055+
{
11056+
CBOREncodeOptions options = new CBOREncodeOptions("allowduplicatekeys=1;keepkeyorder=1");
11057+
byte[] bytes = new byte[] {
11058+
(byte)0xbf,
11059+
(byte)0x9f,
11060+
(byte)0xbf, 0x39, 0x20,
11061+
(byte)0x8f, 0x4a, 0x1f, 0x46, 0x26, 0x0b, 0x3e, 0x72, 0x2c, 0x7f, 0x11,
11062+
0x2e, 0x39,
11063+
(byte)0x9d,
11064+
(byte)0xba, 0x1a, 0x11,
11065+
(byte)0x8d,
11066+
(byte)0xc0,
11067+
(byte)0xb4, 0x38,
11068+
(byte)0xb6,
11069+
(byte)0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
11070+
(byte)0xd8, 0x3b,
11071+
(byte)0x99, 0x00, 0x02, 0x3b, 0x05,
11072+
(byte)0xbb,
11073+
(byte)0xea,
11074+
(byte)0x8e, 0x4b,
11075+
(byte)0xd3, 0x5e, 0x22,
11076+
(byte)0x9f, 0x59, 0x00, 0x00,
11077+
(byte)0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x41, 0x20,
11078+
(byte)0xbf, 0x1a, 0x00, 0x00, 0x00, 0x61,
11079+
(byte)0xb9, 0x00, 0x01, 0x1a, 0x00, 0x00, 0x00, 0x0e,
11080+
(byte)0xba, 0x00, 0x00, 0x00, 0x00,
11081+
(byte)0xff,
11082+
(byte)0xff,
11083+
(byte)0xff,
11084+
(byte)0xd8, 0x22,
11085+
(byte)0xf8,
11086+
(byte)0x93,
11087+
(byte)0xd9,
11088+
(byte)0xaf, 0x33, 0x19,
11089+
(byte)0xf0,
11090+
(byte)0xf0,
11091+
(byte)0xf9,
11092+
(byte)0x85,
11093+
(byte)0x93,
11094+
(byte)0x99, 0x00, 0x01, 0x3a,
11095+
(byte)0xb5,
11096+
(byte)0xfb, 0x4d, 0x43,
11097+
(byte)0x98, 0x00,
11098+
(byte)0xff, (byte)0xfa, (byte)0xb0, (byte)0xb4, (byte)0xdc, 0x6d,
11099+
(byte)0xff,
11100+
};
11101+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
11102+
}
11103+
{
11104+
CBOREncodeOptions options = new CBOREncodeOptions("allowduplicatekeys=1;keepkeyorder=1");
11105+
byte[] bytes = new byte[] {
11106+
(byte)0xdb, 0x0d,
11107+
(byte)0xcb, 0x5d, 0x78,
11108+
(byte)0x92,
11109+
(byte)0xc2,
11110+
(byte)0xc7, 0x2b,
11111+
(byte)0xb9, 0x00, 0x02, 0x39,
11112+
(byte)0xee,
11113+
(byte)0xa0, (byte)0xa0, 0x1a, 0x0e, (byte)0xd9, (byte)0xec, (byte)0xca,
11114+
(byte)0xf2,
11115+
};
11116+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
11117+
}
11118+
{
11119+
CBOREncodeOptions options = new CBOREncodeOptions("allowduplicatekeys=1;keepkeyorder=1");
11120+
byte[] bytes = new byte[] {
11121+
(byte)0xbf,
11122+
(byte)0xfb,
11123+
(byte)0xb1, 0x21,
11124+
(byte)0x93,
11125+
(byte)0x8c,
11126+
(byte)0xc6,
11127+
(byte)0xf3,
11128+
(byte)0xcf,
11129+
(byte)0xb7, (byte)0xf8, 0x76, 0x18, (byte)0xda, 0x39, 0x60, (byte)0xf4,
11130+
(byte)0xff,
11131+
};
11132+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
11133+
}
11134+
{
11135+
CBOREncodeOptions options = new CBOREncodeOptions("allowduplicatekeys=1;keepkeyorder=1");
11136+
byte[] bytes = new byte[] {
11137+
(byte)0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11138+
0x00, 0x02, (byte)0xf0, 0x0d, 0x2a, 0x21,
11139+
};
11140+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
11141+
}
11142+
{
11143+
CBOREncodeOptions options = new CBOREncodeOptions("allowduplicatekeys=1;keepkeyorder=1");
11144+
byte[] bytes = new byte[] {
11145+
(byte)0xba, 0x00, 0x00, 0x00, 0x02,
11146+
(byte)0xf9, 0x48, 0x37,
11147+
(byte)0xda,
11148+
(byte)0xb5, 0x72,
11149+
(byte)0xcf,
11150+
(byte)0xf8, 0x31, 0x3b, 0x06, 0x78,
11151+
(byte)0xdb, 0x44, 0x7d, (byte)0xba, (byte)0xbd, 0x7d, 0x39, (byte)0x98,
11152+
(byte)0xb9,
11153+
};
11154+
CBORTestCommon.AssertRoundTrip(CBORObject.DecodeFromBytes(bytes, options));
11155+
}
11156+
}
11157+
@Test
11158+
public void TestMapCompareRegressions() {
11159+
CBORObject m1, m2;
11160+
m1 = CBORObject.NewMap().Add(3, 4).Add(1, 2);
11161+
m2 = CBORObject.NewOrderedMap().Add(3, 4).Add(1, 2);
11162+
Assert.assertEquals(0, m1.compareTo(m2));
11163+
TestCommon.CompareTestEqualAndConsistent(m1, m2);
11164+
m1 = CBORObject.NewMap().Add(3, 2).Add(1, 2);
11165+
m2 = CBORObject.NewOrderedMap().Add(3, 4).Add(1, 2);
11166+
TestCommon.CompareTestLess(m1, m2);
11167+
m1 = CBORObject.NewMap().Add(3, 7).Add(1, 2);
11168+
m2 = CBORObject.NewOrderedMap().Add(3, 4).Add(1, 2);
11169+
TestCommon.CompareTestGreater(m1, m2);
11170+
m1 = CBORObject.NewMap().Add(3, 4).Add(1, 0);
11171+
m2 = CBORObject.NewOrderedMap().Add(3, 4).Add(1, 2);
11172+
TestCommon.CompareTestLess(m1, m2);
11173+
m1 = CBORObject.NewMap().Add(3, 4).Add(1, 7);
11174+
m2 = CBORObject.NewOrderedMap().Add(3, 4).Add(1, 2);
11175+
TestCommon.CompareTestGreater(m1, m2);
11176+
}
1103811177
@Test
1103911178
public void TestToObject_TypeMapper() {
1104011179
CBORTypeMapper mapper = new CBORTypeMapper()

src/test/java/com/upokecenter/test/CBORTestCommon.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public static CBORObject RandomNumberOrRational(IRandomGenExtended rand) {
122122
public static CBORObject RandomCBORMap(IRandomGenExtended rand, int depth) {
123123
int x = rand.GetInt32(100);
124124
int count = (x < 80) ? 2 : ((x < 93) ? 1 : ((x < 98) ? 0 : 10));
125-
CBORObject cborRet = CBORObject.NewMap();
125+
CBORObject cborRet = rand.GetInt32(100) < 30 ?
126+
CBORObject.NewOrderedMap() : CBORObject.NewMap();
126127
for (int i = 0; i < count; ++i) {
127128
CBORObject key = RandomCBORObject(rand, depth + 1);
128129
CBORObject value = RandomCBORObject(rand, depth + 1);

src/test/java/com/upokecenter/test/TestCommon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ private static boolean ByteArraysEqual(
746746
return true;
747747
}
748748

749-
private static boolean ByteArraysEqual(byte[] arr1, byte[] arr2) {
749+
public static boolean ByteArraysEqual(byte[] arr1, byte[] arr2) {
750750
if (arr1 == null) {
751751
return arr2 == null;
752752
}

0 commit comments

Comments
 (0)