From 46c92a9540d7824a0e004c7a48e404131ab942cd Mon Sep 17 00:00:00 2001 From: Uttam Kumar Date: Wed, 13 Mar 2024 10:39:07 -0700 Subject: [PATCH] disable modifiable collection transformer for Maps (#554) * disable modifiable collection transformer for Maps * test updates --- .../src/main/avro/vs110/TestCollections.avsc | 10 ++ .../src/main/avro/vs111/TestCollections.avsc | 10 ++ .../src/main/avro/vs14/TestCollections.avsc | 10 ++ .../src/main/avro/vs15/TestCollections.avsc | 10 ++ .../src/main/avro/vs16/TestCollections.avsc | 10 ++ .../src/main/avro/vs17/TestCollections.avsc | 10 ++ .../src/main/avro/vs18/TestCollections.avsc | 10 ++ .../src/main/avro/vs19/TestCollections.avsc | 10 ++ .../avro/charseqmethod/TestCollections.avsc | 10 ++ .../avro/charseqmethod/TestCollections.avsc | 10 ++ .../avroutil1/builder/SpecificRecordTest.java | 25 +++-- .../CollectionViewTest.java | 106 ------------------ .../codegen/SpecificRecordClassGenerator.java | 23 ++-- .../CharSequenceMapView.java | 102 ----------------- .../CollectionTransformerUtil.java | 34 ------ .../collectiontransformer/MapTransformer.java | 10 -- .../collectiontransformer/StringMapView.java | 102 ----------------- 17 files changed, 124 insertions(+), 378 deletions(-) delete mode 100644 helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CharSequenceMapView.java delete mode 100644 helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringMapView.java diff --git a/avro-builder/tests/codegen-110/src/main/avro/vs110/TestCollections.avsc b/avro-builder/tests/codegen-110/src/main/avro/vs110/TestCollections.avsc index 6e3b6f58..a47652f9 100644 --- a/avro-builder/tests/codegen-110/src/main/avro/vs110/TestCollections.avsc +++ b/avro-builder/tests/codegen-110/src/main/avro/vs110/TestCollections.avsc @@ -78,6 +78,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-111/src/main/avro/vs111/TestCollections.avsc b/avro-builder/tests/codegen-111/src/main/avro/vs111/TestCollections.avsc index ebbb6de3..31d8bf28 100644 --- a/avro-builder/tests/codegen-111/src/main/avro/vs111/TestCollections.avsc +++ b/avro-builder/tests/codegen-111/src/main/avro/vs111/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-14/src/main/avro/vs14/TestCollections.avsc b/avro-builder/tests/codegen-14/src/main/avro/vs14/TestCollections.avsc index c8cb8cbe..bb529c94 100644 --- a/avro-builder/tests/codegen-14/src/main/avro/vs14/TestCollections.avsc +++ b/avro-builder/tests/codegen-14/src/main/avro/vs14/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-15/src/main/avro/vs15/TestCollections.avsc b/avro-builder/tests/codegen-15/src/main/avro/vs15/TestCollections.avsc index e4d4fe3a..5e79b217 100644 --- a/avro-builder/tests/codegen-15/src/main/avro/vs15/TestCollections.avsc +++ b/avro-builder/tests/codegen-15/src/main/avro/vs15/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-16/src/main/avro/vs16/TestCollections.avsc b/avro-builder/tests/codegen-16/src/main/avro/vs16/TestCollections.avsc index 3e5832ad..a79e1144 100644 --- a/avro-builder/tests/codegen-16/src/main/avro/vs16/TestCollections.avsc +++ b/avro-builder/tests/codegen-16/src/main/avro/vs16/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-17/src/main/avro/vs17/TestCollections.avsc b/avro-builder/tests/codegen-17/src/main/avro/vs17/TestCollections.avsc index ac9430f1..717c97dd 100644 --- a/avro-builder/tests/codegen-17/src/main/avro/vs17/TestCollections.avsc +++ b/avro-builder/tests/codegen-17/src/main/avro/vs17/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc b/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc index 42fa056f..3fbbd583 100644 --- a/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc +++ b/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-19/src/main/avro/vs19/TestCollections.avsc b/avro-builder/tests/codegen-19/src/main/avro/vs19/TestCollections.avsc index 2651f92e..7c9f39d0 100644 --- a/avro-builder/tests/codegen-19/src/main/avro/vs19/TestCollections.avsc +++ b/avro-builder/tests/codegen-19/src/main/avro/vs19/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-charseq-method/src/main/avro/charseqmethod/TestCollections.avsc b/avro-builder/tests/codegen-charseq-method/src/main/avro/charseqmethod/TestCollections.avsc index 9dcaac88..81933b9e 100644 --- a/avro-builder/tests/codegen-charseq-method/src/main/avro/charseqmethod/TestCollections.avsc +++ b/avro-builder/tests/codegen-charseq-method/src/main/avro/charseqmethod/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/codegen-no-utf8-in-putbyindex/src/main/avro/charseqmethod/TestCollections.avsc b/avro-builder/tests/codegen-no-utf8-in-putbyindex/src/main/avro/charseqmethod/TestCollections.avsc index e8b8e730..9e9a56ba 100644 --- a/avro-builder/tests/codegen-no-utf8-in-putbyindex/src/main/avro/charseqmethod/TestCollections.avsc +++ b/avro-builder/tests/codegen-no-utf8-in-putbyindex/src/main/avro/charseqmethod/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java index 302c7b40..9a53a58a 100644 --- a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java +++ b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java @@ -1374,6 +1374,7 @@ private Object[][] testStringTypeParamsProvider() { put("arOfUnionOfStr", "java.util.List"); put("arOfMapOfUnionOfArray", "java.util.List>>"); put("intAr", "java.util.List"); + put("unionOfIntMap", "java.util.Map"); }}; Map vs14TestCollectionsCharSeqFieldToType = new LinkedHashMap() {{ @@ -1386,6 +1387,7 @@ private Object[][] testStringTypeParamsProvider() { put("arOfUnionOfStr", "java.util.List"); put("arOfMapOfUnionOfArray", "java.util.List>>"); put("intAr", "java.util.List"); + put("unionOfIntMap", "java.util.Map"); }}; return new Object[][]{ @@ -1493,6 +1495,11 @@ public void testRecordWithCharSeqStringTypeForMethods() throws Exception { put("key1", Arrays.asList("val1", "val2")); put("key2", Arrays.asList("val10", "val20")); }}; + + Map mapOfInt = new HashMap() {{ + put("key1", 1); + put("key2", 2); + }}; charseqmethod.TestCollections.Builder testCollectionsBuilder = charseqmethod.TestCollections.newBuilder() .setStr(str) .setStrAr(Arrays.asList(str)) @@ -1501,7 +1508,8 @@ public void testRecordWithCharSeqStringTypeForMethods() throws Exception { .setArOfMap(Arrays.asList(mapCharSeq)) .setUnionOfMap(mapCharSeq) .setArOfUnionOfStr(Arrays.asList(str)) - .setArOfMapOfUnionOfArray(Arrays.asList(mapOfList)).setIntAr(Arrays.asList(1, 2, 3)); + .setArOfMapOfUnionOfArray(Arrays.asList(mapOfList)).setIntAr(Arrays.asList(1, 2, 3)) + .setUnionOfIntMap(mapOfInt); charseqmethod.TestCollections testCollections = testCollectionsBuilder.build(); @@ -1840,7 +1848,8 @@ public void testNewBuilder() throws Exception { .setArOfMap(instance.getArOfMap()) .setUnionOfMap(instance.getUnionOfMap()) .setArOfUnionOfStr(instance.getArOfUnionOfStr()) - .setArOfMapOfUnionOfArray(instance.getArOfMapOfUnionOfArray()); + .setArOfMapOfUnionOfArray(instance.getArOfMapOfUnionOfArray()) + .setUnionOfIntMap(instance.getUnionOfIntMap()); TestCollections.newBuilder(builder); @@ -1870,9 +1879,7 @@ public void modifiablePrimitiveCollectionTest() { // Union (null, Map) - instance.getUnionOfMap().put("key1", tba); - Assert.assertEquals(tba, instance.getUnionOfMap().get("key1")); - Assert.assertEquals(new Utf8(tba), instance.unionOfMap.get(new Utf8("key1"))); + Assert.assertThrows(UnsupportedOperationException.class, () -> instance.getUnionOfMap().put("key1", tba)); instance.getIntAr().add(Integer.MAX_VALUE); Assert.assertEquals((int) instance.getIntAr().get(instance.getIntAr().size() - 1), Integer.MAX_VALUE); @@ -1902,9 +1909,7 @@ public void modifiablePrimitiveCollectionTestForCharSeq() { // Union (null, Map) - instance.getUnionOfMap().put("key1", tba); - Assert.assertEquals(tba, instance.getUnionOfMap().get("key1")); - Assert.assertEquals(new Utf8(tba), instance.unionOfMap.get(new Utf8("key1"))); + Assert.assertThrows(UnsupportedOperationException.class, () -> instance.getUnionOfMap().put("key1", tba)); instance.getIntAr().add(Integer.MAX_VALUE); Assert.assertEquals((int) instance.getIntAr().get(instance.getIntAr().size() - 1), Integer.MAX_VALUE); @@ -1934,9 +1939,7 @@ public void testCharSeqAccessorForNoUtf8() { // Union (null, Map) - instance.getUnionOfMap().put("key1", tba); - Assert.assertEquals(tba, instance.getUnionOfMap().get("key1")); - Assert.assertEquals(new Utf8(tba), instance.unionOfMap.get(new Utf8("key1"))); + Assert.assertThrows(UnsupportedOperationException.class, () -> instance.getUnionOfMap().put("key1", tba)); instance.getIntAr().add(Integer.MAX_VALUE); Assert.assertEquals((int) instance.getIntAr().get(instance.getIntAr().size() - 1), Integer.MAX_VALUE); diff --git a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionViewTest.java b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionViewTest.java index fde87bd2..e2b5f757 100644 --- a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionViewTest.java +++ b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionViewTest.java @@ -7,9 +7,7 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.avro.util.Utf8; import org.testng.Assert; import org.testng.annotations.Test; @@ -126,108 +124,4 @@ public void testCharSequenceListView() { Assert.assertTrue(utf8List.contains(u)); } } - - @Test - public void testStringMapView() { - Map utf8Map = new HashMap<>(); - List keys = Arrays.asList("key1", "key2", "key3"); - String val = "value"; - - Map map = CollectionTransformerUtil.createStringMapView(utf8Map); - - // utf8 map is empty - Assert.assertEquals(utf8Map.size(), 0); - // view should be empty - Assert.assertEquals(map.size(), 0); - - // insert in view - for (String key : keys) { - map.put(key, key + val); - } - - // view should have 3 elements - for (String key : keys) { - Assert.assertTrue(map.containsKey(key)); - } - // utf8 map should contain the same 3 elements - for (String key : keys) { - Assert.assertTrue(utf8Map.containsKey(new Utf8(key))); - } - - // remove from map - for (String key : keys) { - map.remove(key); - } - // view should be empty - Assert.assertEquals(map.size(), 0); - } - - @Test - public void testUtf8MapView() { - Map utf8Map = new HashMap<>(); - List keys = Arrays.asList(new Utf8("key1"), new Utf8("key2"), new Utf8("key3")); - Utf8 val = new Utf8("value"); - - Map map = CollectionTransformerUtil.createUtf8MapView(utf8Map); - // utf8 map is empty - Assert.assertEquals(utf8Map.size(), 0); - // view should be empty - Assert.assertEquals(map.size(), 0); - - // insert in view - for (Utf8 key : keys) { - map.put(key, val); - } - - // view should have 3 elements - for (Utf8 key : keys) { - Assert.assertTrue(map.containsKey(key)); - } - // utf8 map should contain the same 3 elements - for (Utf8 key : keys) { - Assert.assertTrue(utf8Map.containsKey(key)); - } - - // remove from view - for (Utf8 key : keys) { - map.remove(key); - } - // view should be empty - Assert.assertEquals(map.size(), 0); - } - - @Test - public void testCharSequenceMapView() { - Map utf8Map = new HashMap<>(); - List keys = Arrays.asList("key1", "key2", "key3"); - String val = "value"; - - Map map = CollectionTransformerUtil.createCharSequenceMapView(utf8Map); - - // utf8 map is empty - Assert.assertEquals(utf8Map.size(), 0); - // view should be empty - Assert.assertEquals(map.size(), 0); - - // insert in view - for (CharSequence key : keys) { - map.put(key, key + val); - } - - // view should have 3 elements - for (CharSequence key : keys) { - Assert.assertTrue(map.containsKey(key)); - } - // utf8 map should contain the same 3 elements - for (CharSequence key : keys) { - Assert.assertTrue(utf8Map.containsKey(new Utf8(String.valueOf(key)))); - } - - // remove from view - for (CharSequence key : keys) { - map.remove(key); - } - // view should be empty - Assert.assertEquals(map.size(), 0); - } } diff --git a/avro-codegen/src/main/java/com/linkedin/avroutil1/codegen/SpecificRecordClassGenerator.java b/avro-codegen/src/main/java/com/linkedin/avroutil1/codegen/SpecificRecordClassGenerator.java index f5fc5845..8bc63746 100644 --- a/avro-codegen/src/main/java/com/linkedin/avroutil1/codegen/SpecificRecordClassGenerator.java +++ b/avro-codegen/src/main/java/com/linkedin/avroutil1/codegen/SpecificRecordClassGenerator.java @@ -1116,7 +1116,7 @@ private String getSerializedCustomDecodeBlock(SpecificRecordGenerationConfig con codeBlockBuilder.addStatement("$L.add($L)", arrayVarName, arrayElementVarName) .endControlFlow() .endControlFlow() - .addStatement("$L = com.linkedin.avroutil1.compatibility.collectiontransformer.ListTransformer.getUtf8List($L)", fieldName, arrayVarName); + .addStatement("$L = com.linkedin.avroutil1.compatibility.collectiontransformer.ListTransformer.convertToUtf8($L)", fieldName, arrayVarName); serializedCodeBlock = codeBlockBuilder.build().toString(); @@ -1165,7 +1165,7 @@ private String getSerializedCustomDecodeBlock(SpecificRecordGenerationConfig con codeBlockBuilder.addStatement("$L.put($L,$L)", mapVarName, mapKeyVarName, mapValueVarName) .endControlFlow() .endControlFlow() - .addStatement("$L = com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.getUtf8Map($L)", fieldName, mapVarName);; + .addStatement("$L = com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.convertToUtf8($L)", fieldName, mapVarName);; serializedCodeBlock = codeBlockBuilder.build().toString(); @@ -1581,9 +1581,8 @@ private void addGetByIndexMethod(TypeSpec.Builder classBuilder, AvroRecordSchema SpecificRecordGeneratorUtil.isCollectionSchemaValuePrimitive(field.getSchema())); } else if (SpecificRecordGeneratorUtil.isMapTransformerApplicable(field.getSchema())) { switchBuilder.addStatement( - "case $L: return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$LMap(this.$L, $L)", - fieldIndex++, config.getDefaultMethodStringRepresentation().getJsonValue(), escapedFieldName, - SpecificRecordGeneratorUtil.isCollectionSchemaValuePrimitive(field.getSchema())); + "case $L: return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$LMap(this.$L)", + fieldIndex++, config.getDefaultMethodStringRepresentation().getJsonValue(), escapedFieldName); } else if (field.getSchema() != null && AvroType.UNION.equals(field.getSchema().type())) { switchBuilder.addStatement("case $L:", fieldIndex++); @@ -1610,9 +1609,8 @@ private void addGetByIndexMethod(TypeSpec.Builder classBuilder, AvroRecordSchema } else if (SpecificRecordGeneratorUtil.isMapTransformerApplicable(unionMemberSchema.getSchema())) { switchBuilder.beginControlFlow("else if($1L instanceof $2T)", escapedFieldName, Map.class) .addStatement( - "return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$1LMap($2L, $3L)", - config.getDefaultMethodStringRepresentation().getJsonValue(), escapedFieldName, - SpecificRecordGeneratorUtil.isCollectionSchemaValuePrimitive(field.getSchema())) + "return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$1LMap($2L)", + config.getDefaultMethodStringRepresentation().getJsonValue(), escapedFieldName) .endControlFlow(); } } @@ -1826,9 +1824,9 @@ private MethodSpec getGetterMethodSpec(AvroSchemaField field, SpecificRecordGene SpecificRecordGeneratorUtil.isCollectionSchemaValuePrimitive(field.getSchema())); } else if (SpecificRecordGeneratorUtil.isMapTransformerApplicable(field.getSchema())) { methodSpecBuilder.addStatement( - "return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$LMap(this.$L, $L)", + "return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$LMap(this.$L)", config.getDefaultMethodStringRepresentation().getJsonValue(), - escapedFieldName, SpecificRecordGeneratorUtil.isCollectionSchemaValuePrimitive(field.getSchema())); + escapedFieldName); } else if (field.getSchema() != null && AvroType.UNION.equals(field.getSchema().type())) { methodSpecBuilder.beginControlFlow("if (this.$1L == null)", escapedFieldName) @@ -1852,9 +1850,8 @@ private MethodSpec getGetterMethodSpec(AvroSchemaField field, SpecificRecordGene } else if (SpecificRecordGeneratorUtil.isMapTransformerApplicable(unionMemberSchema.getSchema())) { methodSpecBuilder.beginControlFlow("else if($1L instanceof $2T)", escapedFieldName, Map.class) .addStatement( - "return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$2LMap($1L, $3L)", - escapedFieldName, config.getDefaultMethodStringRepresentation().getJsonValue(), - SpecificRecordGeneratorUtil.isCollectionSchemaValuePrimitive(field.getSchema())) + "return com.linkedin.avroutil1.compatibility.collectiontransformer.MapTransformer.get$2LMap($1L)", + escapedFieldName, config.getDefaultMethodStringRepresentation().getJsonValue()) .endControlFlow(); } } diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CharSequenceMapView.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CharSequenceMapView.java deleted file mode 100644 index eb73cb13..00000000 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CharSequenceMapView.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2024 LinkedIn Corp. - * Licensed under the BSD 2-Clause License (the "License"). - * See License in the project root for license information. - */ -package com.linkedin.avroutil1.compatibility.collectiontransformer; - -import java.util.AbstractMap; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import org.apache.avro.util.Utf8; - - -/** - * View of Utf8 Map to allow get as String while still allowing put to reflect on the original object. - */ -public class CharSequenceMapView extends AbstractMap { - - private Map utf8Map; - - public CharSequenceMapView(Map utf8Map) { - this.utf8Map = utf8Map; - } - - @Override - public Set> entrySet() { - return Collections.unmodifiableSet(utf8Map.entrySet().stream() - .collect(Collectors.toMap( - entry -> (CharSequence) String.valueOf(entry.getKey()), - entry -> (CharSequence) String.valueOf(entry.getValue()) - )) - .entrySet()); - } - - @Override - public CharSequence put(CharSequence key, CharSequence value) { - Utf8 utf8Key = new Utf8(key.toString()); - Utf8 utf8Value = new Utf8(value.toString()); - Utf8 previousValue = utf8Map.put(utf8Key, utf8Value); - return previousValue != null ? (CharSequence) String.valueOf(previousValue) : null; - } - - @Override - public Set keySet() { - return Collections.unmodifiableSet(utf8Map.keySet().stream() - .map(CharSequence::toString) - .collect(Collectors.toSet())); - } - - @Override - public Collection values() { - return Collections.unmodifiableCollection(utf8Map.values().stream() - .map(CharSequence::toString) - .collect(Collectors.toList())); - } - - @Override - public int size() { - return utf8Map.size(); - } - - @Override - public boolean isEmpty() { - return utf8Map.isEmpty(); - } - - @Override - public boolean containsKey(Object key) { - return utf8Map.containsKey(new Utf8(String.valueOf(key))); - } - - @Override - public boolean containsValue(Object value) { - return utf8Map.containsValue(new Utf8(String.valueOf(value))); - } - - @Override - public CharSequence get(Object key) { - Utf8 utf8Key = new Utf8(String.valueOf(key)); - Utf8 utf8Value = utf8Map.get(utf8Key); - return utf8Value != null ? (CharSequence) String.valueOf(utf8Value) : null; - } - - @Override - public CharSequence remove(Object key) { - Utf8 utf8Key = new Utf8(String.valueOf(key)); - Utf8 previousValue = utf8Map.remove(utf8Key); - return previousValue != null ? (CharSequence) String.valueOf(previousValue) : null; - } - - @Override - public void putAll(Map m) { - m.forEach((key, value) -> { - Utf8 utf8Key = new Utf8(String.valueOf(key)); - Utf8 utf8Value = new Utf8(String.valueOf(value)); - utf8Map.put(utf8Key, utf8Value); - }); - } -} diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java index 39b02247..f2c1a96d 100644 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java +++ b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java @@ -8,7 +8,6 @@ import com.linkedin.avroutil1.compatibility.StringUtils; import java.util.List; -import java.util.Map; import org.apache.avro.util.Utf8; @@ -47,37 +46,4 @@ public static List createCharSequenceListView(List utf8List) public static List createUtf8ListView(List utf8List) { return new Utf8ListView(utf8List); } - - /** - * Returns a {@link StringMapView} for the given map of {@link Utf8} objects. - * @param utf8Map map of {@link Utf8} objects - * @return a {@link StringMapView} for the given map of {@link Utf8} objects - */ - public static Map createStringMapView(Map utf8Map) { - if (utf8Map == null) { - return null; - } - return new StringMapView(utf8Map); - } - - /** - * Returns a {@link CharSequenceMapView} for the given map of {@link Utf8} objects. - * @param utf8Map map of {@link Utf8} objects - * @return a {@link CharSequenceMapView} for the given map of {@link Utf8} objects - */ - public static Map createUtf8MapView(Map utf8Map) { - return utf8Map; - } - - /** - * Returns a {@link CharSequenceMapView} for the given map of {@link Utf8} objects. - * @param utf8Map map of {@link Utf8} objects - * @return a {@link CharSequenceMapView} for the given map of {@link Utf8} objects - */ - public static Map createCharSequenceMapView(Map utf8Map) { - if (utf8Map == null) { - return null; - } - return new CharSequenceMapView(utf8Map); - } } diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java index ad732d1b..68c0195d 100644 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java +++ b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java @@ -11,7 +11,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.avro.util.Utf8; public class MapTransformer { @@ -29,9 +28,6 @@ public static Map getCharSequenceMap(Object mapObj) { } public static Map getUtf8Map(Object mapObj, boolean isPrimitiveCollection) { - if(isPrimitiveCollection) { - return CollectionTransformerUtil.createUtf8MapView((Map) mapObj); - } if (mapObj == null) { return null; } @@ -61,9 +57,6 @@ public static Map getUtf8Map(Object mapObj, boolean isPrimitiveCollection) { } public static Map getStringMap(Object mapObj, boolean isPrimitiveCollection) { - if(isPrimitiveCollection) { - return CollectionTransformerUtil.createStringMapView((Map) mapObj); - } if (mapObj == null) { return null; } @@ -92,9 +85,6 @@ public static Map getStringMap(Object mapObj, boolean isPrimitiveCollection) { } public static Map getCharSequenceMap(Object mapObj, boolean isPrimitiveCollection) { - if(isPrimitiveCollection) { - return CollectionTransformerUtil.createCharSequenceMapView((Map) mapObj); - } if (mapObj == null) { return null; } diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringMapView.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringMapView.java deleted file mode 100644 index 19c0a500..00000000 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringMapView.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2024 LinkedIn Corp. - * Licensed under the BSD 2-Clause License (the "License"). - * See License in the project root for license information. - */ -package com.linkedin.avroutil1.compatibility.collectiontransformer; - -import java.util.AbstractMap; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import org.apache.avro.util.Utf8; - - -/** - * View of Utf8 Map to allow get as String while still allowing put to reflect on the original object. - */ -public class StringMapView extends AbstractMap { - - private Map utf8Map; - - public StringMapView(Map utf8Map) { - this.utf8Map = utf8Map; - } - - @Override - public Set> entrySet() { - return Collections.unmodifiableSet(utf8Map.entrySet().stream() - .collect(Collectors.toMap( - entry -> String.valueOf(entry.getKey()), - entry -> String.valueOf(entry.getValue()) - )) - .entrySet()); - } - - @Override - public String put(String key, String value) { - Utf8 utf8Key = new Utf8(key); - Utf8 utf8Value = new Utf8(value); - Utf8 previousValue = utf8Map.put(utf8Key, utf8Value); - return previousValue != null ? String.valueOf(previousValue) : null; - } - - @Override - public Set keySet() { - return Collections.unmodifiableSet(utf8Map.keySet().stream() - .map(String::valueOf) - .collect(Collectors.toSet())); - } - - @Override - public Collection values() { - return Collections.unmodifiableCollection(utf8Map.values().stream() - .map(String::valueOf) - .collect(Collectors.toList())); - } - - @Override - public int size() { - return utf8Map.size(); - } - - @Override - public boolean isEmpty() { - return utf8Map.isEmpty(); - } - - @Override - public boolean containsKey(Object key) { - return utf8Map.containsKey(new Utf8(String.valueOf(key))); - } - - @Override - public boolean containsValue(Object value) { - return utf8Map.containsValue(new Utf8(String.valueOf(value))); - } - - @Override - public String get(Object key) { - Utf8 utf8Key = new Utf8(String.valueOf(key)); - Utf8 utf8Value = utf8Map.get(utf8Key); - return utf8Value != null ? String.valueOf(utf8Value) : null; - } - - @Override - public String remove(Object key) { - Utf8 utf8Key = new Utf8(String.valueOf(key)); - Utf8 previousValue = utf8Map.remove(utf8Key); - return previousValue != null ? String.valueOf(previousValue) : null; - } - - @Override - public void putAll(Map m) { - m.forEach((key, value) -> { - Utf8 utf8Key = new Utf8(key); - Utf8 utf8Value = new Utf8(value); - utf8Map.put(utf8Key, utf8Value); - }); - } -}