Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referencing Binary Format: Remove references to collections #230

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/main/java/org/joda/beans/ser/bin/BeanReferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ private void findReferencesBean(
return;
}

// has this object been seen before, if so no need to check it again
int result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}

if (base instanceof Bean) {
// has this object been seen before, if so no need to check it again
int result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}

addClassInfo(base, declaredClass);
Bean bean = (Bean) base;
if (settings.getConverter().isConvertible(bean.getClass())) {
Expand Down Expand Up @@ -166,11 +166,23 @@ private void findReferencesBean(
} else if (parentIterator != null) {
SerIterator childIterator = settings.getIteratorFactory().createChild(base, parentIterator);
if (childIterator != null) {
if (childIterator.metaTypeRequired()) {
objects.compute(childIterator.metaTypeName(), BeanReferences::incrementOrOne);
}
// shouldn't try and reuse references to collections
findReferencesIterable(childIterator, objects);
} else {
int result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}
addClassInfo(base, declaredClass);
}
} else {
int result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}
addClassInfo(base, declaredClass);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/joda/beans/ser/SerTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.util.Currency;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeSet;

import org.joda.beans.sample.Address;
import org.joda.beans.sample.Company;
Expand Down Expand Up @@ -228,6 +230,17 @@ public static ImmGenericCollections<JodaConvertInterface> testGenericInterfaces(
.build();
}

public static ImmGenericCollections<Object> testGenericInterfacesCollections() {
return ImmGenericCollections.builder()
.map(ImmutableMap.of(
"First", Arrays.asList("A", "B"),
"First1", ImmutableList.of("A", "B"),
"Third1", new TreeSet<>(ImmutableList.of("A", "B")),
"Third", new HashSet<>(Arrays.asList("A", "B")),
"Second", testCollections()))
.build();
}

public static ImmKeyList testIntermediateInterfaces() {
// second serialized as JodaConvertInterface, non-bean
// third and fourth are serialized as an intermediate Joda-Convert interface INamedKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ public void test_writeJodaConvertInterface() {
BeanAssert.assertBeanEquals(bean, array);
}

@Test
public void test_writeJodaConvertInterfaceCollections() {
ImmGenericCollections<?> array = SerTestHelper.testGenericInterfacesCollections();

byte[] bytes = JodaBeanSer.COMPACT.binWriterReferencing().write(array);
//System.out.println(JodaBeanBinReader.visualize(bytes));

ImmGenericCollections<?> bean =
(ImmGenericCollections<?>) JodaBeanSer.COMPACT.binReader().read(bytes);
//System.out.println(bean);
BeanAssert.assertBeanEquals(bean, array);
}

@Test
public void test_writeIntermediateInterface() {
ImmKeyList array = SerTestHelper.testIntermediateInterfaces();
Expand Down