Skip to content

Commit

Permalink
Moved View classes to their own declarations from inline
Browse files Browse the repository at this point in the history
  • Loading branch information
li-ukumar committed Feb 13, 2024
1 parent a1b4fec commit e9f3fea
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,7 @@ public void testNewBuilder() throws Exception {
compareIndexedRecords(instance, builder.build());
}

@Test
public void modifiablePrimitiveCollectionTest() {
String tba = "NewElement";
RandomRecordGenerator generator = new RandomRecordGenerator();
Expand All @@ -1840,22 +1841,27 @@ public void modifiablePrimitiveCollectionTest() {
// array of string
instance.getStrAr().add(tba);
Assert.assertTrue(instance.getStrAr().contains(tba));
Assert.assertTrue(instance.strAr.contains(new Utf8(tba)));

// union[null, List<String>]
instance.getUnionOfArray().add(tba);
Assert.assertTrue(instance.getUnionOfArray().contains(tba));
Assert.assertTrue(instance.unionOfArray.contains(new Utf8(tba)));

// array (union[null, string])
instance.getArOfUnionOfStr().add(tba);
Assert.assertTrue(instance.getArOfUnionOfStr().contains(tba));
Assert.assertTrue(instance.arOfUnionOfStr.contains(new Utf8(tba)));


// Union (null, Map<String, String>)
instance.getUnionOfMap().put("key1", tba);
Assert.assertEquals(tba, instance.getUnionOfMap().get("key1"));
Assert.assertEquals(new Utf8(tba), instance.unionOfMap.get(new Utf8("key1")));

instance.getIntAr().add(Integer.MAX_VALUE);
Assert.assertEquals((int) instance.getIntAr().get(instance.getIntAr().size() - 1), Integer.MAX_VALUE);
Assert.assertEquals((int) instance.intAr.get(instance.getIntAr().size() - 1), Integer.MAX_VALUE);
}

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.AbstractList;
import org.apache.avro.util.Utf8;


/**
* View of List<Utf8> to allow get as CharSequence while still allowing put to reflect on the original object.
*/
public class CharSequenceListView extends AbstractList<CharSequence> {
private java.util.List<Utf8> utf8List;

public CharSequenceListView(java.util.List<Utf8> utf8List) {
this.utf8List = utf8List;
}

@Override
public CharSequence get(int index) {
return String.valueOf(utf8List.get(index));
}

@Override
public int size() {
return utf8List.size();
}

@Override
public CharSequence set(int index, CharSequence element) {
CharSequence previousValue = String.valueOf(utf8List.get(index));
utf8List.set(index, new Utf8(element.toString()));
return previousValue;
}

@Override
public void add(int index, CharSequence element) {
utf8List.add(index, new Utf8(element.toString()));
}

@Override
public boolean add(CharSequence element) {
return utf8List.add(new Utf8(element.toString()));
}

@Override
public boolean addAll(int index, java.util.Collection<? extends CharSequence> c) {
boolean modified = false;
for (CharSequence element : c) {
utf8List.add(index++, new Utf8(element.toString()));
modified = true;
}
return modified;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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 Map<Utf8, Utf8> to allow get as String while still allowing put to reflect on the original object.
*/
public class CharSequenceMapView extends AbstractMap<CharSequence, CharSequence> {

private Map<Utf8, Utf8> utf8Map;

public CharSequenceMapView(Map<Utf8, Utf8> utf8Map) {
this.utf8Map = utf8Map;
}

@Override
public Set<Entry<CharSequence, CharSequence>> 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<CharSequence> keySet() {
return Collections.unmodifiableSet(utf8Map.keySet().stream()
.map(CharSequence::toString)
.collect(Collectors.toSet()));
}

@Override
public Collection<CharSequence> 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<? extends CharSequence, ? extends CharSequence> m) {
m.forEach((key, value) -> {
Utf8 utf8Key = new Utf8(String.valueOf(key));
Utf8 utf8Value = new Utf8(String.valueOf(value));
utf8Map.put(utf8Key, utf8Value);
});
}
}
Loading

0 comments on commit e9f3fea

Please sign in to comment.