-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed BagTypeAdapter in favor of Container1TypeAdapter and Containe…
…r2TypeAdapter
- Loading branch information
1 parent
a523460
commit 54a6400
Showing
6 changed files
with
126 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
src/main/java/lsh/ext/gson/ext/org/apache/commons/collections4/BagTypeAdapter.java
This file was deleted.
Oops, something went wrong.
33 changes: 10 additions & 23 deletions
33
src/main/java/lsh/ext/gson/ext/org/apache/commons/collections4/Builder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,26 @@ | ||
package lsh.ext.gson.ext.org.apache.commons.collections4; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import lsh.ext.gson.IBuilder1; | ||
import lsh.ext.gson.IBuilder2; | ||
import org.apache.commons.collections4.Bag; | ||
import org.apache.commons.collections4.MultiValuedMap; | ||
|
||
@UtilityClass | ||
public final class Builder { | ||
|
||
public static <E, B extends Bag<E>> IBuilder2<E, Integer, B> forBag(final B bag) { | ||
return new IBuilder2<>() { | ||
@Override | ||
public void accept(final E e, final Integer n) { | ||
bag.add(e, n); | ||
} | ||
|
||
@Override | ||
public B build() { | ||
return bag; | ||
} | ||
}; | ||
public static <E, B extends Bag<E>> IBuilder1<E, B> forBag(final Supplier<? extends B> create) { | ||
return IBuilder1.of(create, (e, bag) -> bag.add(e)); | ||
} | ||
|
||
public static <K, V, M extends MultiValuedMap<K, V>> IBuilder2<K, V, M> forMultiValuedMap(final M multiValuedMap) { | ||
return new IBuilder2<>() { | ||
@Override | ||
public void accept(final K k, final V v) { | ||
multiValuedMap.put(k, v); | ||
} | ||
public static <E, B extends Bag<E>> IBuilder2<E, Integer, B> forBagNCopies(final Supplier<? extends B> create) { | ||
return IBuilder2.of(create, (e, nCopies, bag) -> bag.add(e, nCopies)); | ||
} | ||
|
||
@Override | ||
public M build() { | ||
return multiValuedMap; | ||
} | ||
}; | ||
public static <K, V, M extends MultiValuedMap<K, V>> IBuilder2<K, V, M> forMultiValuedMap(final Supplier<? extends M> create) { | ||
return IBuilder2.of(create, (k, v, map) -> map.put(k, v)); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...xt/gson/ext/org/apache/commons/collections4/ApacheCommonsCollections4TypeAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lsh.ext.gson.ext.org.apache.commons.collections4; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Function; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import lsh.ext.gson.IBuilder2; | ||
import lsh.ext.gson.test.TestTypeAdapters; | ||
import org.apache.commons.collections4.Bag; | ||
import org.apache.commons.collections4.bag.TreeBag; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public final class ApacheCommonsCollections4TypeAdapterTest { | ||
|
||
@Test | ||
public void testForBagRoundTrip() | ||
throws IOException { | ||
final TypeAdapter<Bag<String>> unit = ApacheCommonsCollections4TypeAdapter.forBag( | ||
TestTypeAdapters.stringTypeAdapter, | ||
() -> Builder.forBag(TreeBag::new) | ||
); | ||
final Bag<String> before = new TreeBag<>(); | ||
before.add("foo"); | ||
before.add("bar"); | ||
before.add("bar"); | ||
before.add("baz"); | ||
before.add("baz"); | ||
before.add("baz"); | ||
final String json = unit.toJson(before); | ||
Assertions.assertEquals("[\"bar\",\"bar\",\"baz\",\"baz\",\"baz\",\"foo\"]", json); | ||
final Bag<String> actual = unit.fromJson(json); | ||
Assertions.assertIterableEquals(before, actual); | ||
} | ||
|
||
@Test | ||
public void testForBagNCopiesForRoundTrip() | ||
throws IOException { | ||
@SuppressWarnings("RedundantTypeArguments") | ||
final TypeAdapter<Bag<String>> unit = ApacheCommonsCollections4TypeAdapter.<String>forBagNCopies( | ||
TestTypeAdapters.integerTypeAdapter, | ||
() -> IBuilder2.of(TreeBag::new, (e, nCopies, strings) -> strings.add(e, nCopies), Function.identity()) | ||
); | ||
final Bag<String> before = new TreeBag<>(); | ||
before.add("foo"); | ||
before.add("bar"); | ||
before.add("bar"); | ||
before.add("baz"); | ||
before.add("baz"); | ||
before.add("baz"); | ||
final String json = unit.toJson(before); | ||
Assertions.assertEquals("{\"bar\":2,\"baz\":3,\"foo\":1}", json); | ||
final Bag<String> actual = unit.fromJson(json); | ||
Assertions.assertIterableEquals(before, actual); | ||
} | ||
|
||
} |