Skip to content

Commit 23fbc2d

Browse files
authored
Add ConcurrentHashMap.KeySetView serializer (#1183)
* Add ConcurrentHashMap.KeySetView serializer
1 parent 5894b12 commit 23fbc2d

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/com/esotericsoftware/kryo/serializers/DefaultSerializers.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.TreeMap;
6363
import java.util.TreeSet;
6464
import java.util.UUID;
65+
import java.util.concurrent.ConcurrentHashMap;
6566
import java.util.concurrent.ConcurrentSkipListMap;
6667
import java.util.concurrent.atomic.AtomicBoolean;
6768
import java.util.concurrent.atomic.AtomicInteger;
@@ -852,6 +853,27 @@ private PriorityQueue createPriorityQueue (Class<? extends Collection> type, int
852853
}
853854
}
854855

856+
/** Serializer for {@link ConcurrentHashMap.KeySetView}.
857+
* @author Andreas Bergander */
858+
public static class KeySetViewSerializer extends Serializer<ConcurrentHashMap.KeySetView> {
859+
public void write (Kryo kryo, Output output, ConcurrentHashMap.KeySetView set) {
860+
kryo.writeClassAndObject(output, set.getMap());
861+
kryo.writeClassAndObject(output, set.getMappedValue());
862+
}
863+
864+
public ConcurrentHashMap.KeySetView read (Kryo kryo, Input input, Class<? extends ConcurrentHashMap.KeySetView> type) {
865+
return createKeySetView((ConcurrentHashMap)kryo.readClassAndObject(input), kryo.readClassAndObject(input));
866+
}
867+
868+
public ConcurrentHashMap.KeySetView copy (Kryo kryo, ConcurrentHashMap.KeySetView original) {
869+
return createKeySetView(kryo.copy(original.getMap()), kryo.copy(original.getMappedValue()));
870+
}
871+
872+
private ConcurrentHashMap.KeySetView createKeySetView (ConcurrentHashMap map, Object mappedValue) {
873+
return map.keySet(mappedValue);
874+
}
875+
}
876+
855877
/** Serializer for {@link Locale} (immutables).
856878
* @author Tumi <[email protected]> */
857879
public static class LocaleSerializer extends ImmutableSerializer<Locale> {

test/com/esotericsoftware/kryo/serializers/DefaultSerializersTest.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.esotericsoftware.kryo.io.Input;
2727
import com.esotericsoftware.kryo.io.Output;
2828
import com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy;
29+
import com.esotericsoftware.kryo.serializers.DefaultSerializers.KeySetViewSerializer;
2930

3031
import java.math.BigDecimal;
3132
import java.math.BigInteger;
@@ -45,6 +46,7 @@
4546
import java.util.PriorityQueue;
4647
import java.util.TimeZone;
4748
import java.util.UUID;
49+
import java.util.concurrent.ConcurrentHashMap;
4850
import java.util.concurrent.atomic.AtomicBoolean;
4951
import java.util.concurrent.atomic.AtomicInteger;
5052
import java.util.concurrent.atomic.AtomicLong;
@@ -246,7 +248,7 @@ void testTimestampSerializer () {
246248
roundTrip(11, newTimestamp(-1234567, 1));
247249
roundTrip(14, newTimestamp(-1234567, 123_456_789));
248250
}
249-
251+
250252
private java.sql.Timestamp newTimestamp(long time, int nanos) {
251253
java.sql.Timestamp t = new java.sql.Timestamp(time);
252254
t.setNanos(nanos);
@@ -452,6 +454,53 @@ void testEmptyPriorityQueueSubclass () {
452454
roundTrip(3, queue);
453455
}
454456

457+
@Test
458+
void testConcurrentHashMapKeySetView () {
459+
ConcurrentHashMap.KeySetView<Integer, Boolean> set = ConcurrentHashMap.newKeySet();
460+
set.add(12);
461+
kryo.register(ConcurrentHashMap.KeySetView.class, new KeySetViewSerializer());
462+
kryo.register(ConcurrentHashMap.class);
463+
roundTrip(9, set);
464+
}
465+
466+
@Test
467+
void testConcurrentHashMapKeySetViewFromExistingMap () {
468+
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
469+
470+
map.put("1", 1);
471+
map.put("2", 2);
472+
473+
ConcurrentHashMap.KeySetView<String, Integer> set = map.keySet(4);
474+
475+
kryo.register(ConcurrentHashMap.KeySetView.class, new KeySetViewSerializer());
476+
kryo.register(ConcurrentHashMap.class);
477+
roundTrip(15, set);
478+
}
479+
480+
@Test
481+
void testEmptyConcurrentHashMapKeySetView () {
482+
ConcurrentHashMap.KeySetView set = ConcurrentHashMap.newKeySet();
483+
kryo.register(ConcurrentHashMap.KeySetView.class, new KeySetViewSerializer());
484+
kryo.register(ConcurrentHashMap.class);
485+
roundTrip(5, set);
486+
}
487+
488+
@Test
489+
void testConcurrentHashMapKeySetViewCopy () {
490+
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
491+
492+
map.put("1", 1);
493+
map.put("2", 2);
494+
495+
ConcurrentHashMap.KeySetView<String, Integer> set = map.keySet(4);
496+
497+
kryo.register(ConcurrentHashMap.KeySetView.class, new KeySetViewSerializer());
498+
kryo.register(ConcurrentHashMap.class);
499+
ConcurrentHashMap.KeySetView<String, Integer> copy = kryo.copy(set);
500+
assertEquals(set.iterator().next(), copy.iterator().next());
501+
assertEquals(set.getMappedValue(), copy.getMappedValue());
502+
}
503+
455504
@Test
456505
void testCalendar () {
457506
kryo.setRegistrationRequired(false);
@@ -589,7 +638,7 @@ void testURISerializer () throws Exception {
589638
@Test
590639
void testUUIDSerializer () {
591640
kryo.register(UUID.class, new DefaultSerializers.UUIDSerializer());
592-
641+
593642
roundTrip(17, UUID.fromString("e58ed763-928c-4155-bee9-fdbaaadc15f3"));
594643
}
595644

0 commit comments

Comments
 (0)