Skip to content

Commit

Permalink
Introduced the Apache Commons Collections KeyValue support
Browse files Browse the repository at this point in the history
  • Loading branch information
lyubomyr-shaydariv committed Oct 15, 2024
1 parent 756dae7 commit 3eb3548
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ No `ext-gson` dependencies are bundled or planned to be bundled, so all dependen
* `org.apache.commons.collections4.BidiMap`
* `org.apache.commons.collections4.BoundedCollection`
* `org.apache.commons.collections4.IterableMap`
* `org.apache.commons.collections4.KeyValue`
* `org.apache.commons.collections4.MultiSet`
* `org.apache.commons.collections4.MultiValuedMap`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.BoundedCollection;
import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.KeyValue;
import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.MultiValuedMap;

Expand All @@ -31,6 +32,9 @@ public final class ApacheCommonsCollections4Module
@Setter
private ITypeAdapterFactory<? extends IterableMap<String, ?>> iterableMapTypeAdapterFactory = ApacheCommonsCollections4TypeAdapterFactory.defaultForIterableMap;

@Setter
private ITypeAdapterFactory<? extends KeyValue<String, ?>> keyTypeAdapterFactory = ApacheCommonsCollections4TypeAdapterFactory.defaultForKeyValue;

@Setter
private ITypeAdapterFactory<? extends MultiSet<?>> multiSetTypeAdapterFactory = ApacheCommonsCollections4TypeAdapterFactory.defaultForMultiSet;

Expand All @@ -44,6 +48,7 @@ public IModule build() {
bidiMapTypeAdapterFactory,
boundedCollectionTypeAdapterFactory,
iterableMapTypeAdapterFactory,
keyTypeAdapterFactory,
multiSetTypeAdapterFactory,
multiValuedMapTypeAdapterFactory
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.AbstractMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -11,11 +12,13 @@
import lsh.ext.gson.Container2TypeAdapter;
import lsh.ext.gson.IBuilder1;
import lsh.ext.gson.IBuilder2;
import lsh.ext.gson.SingleEntryTypeAdapter;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.BoundedCollection;
import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.KeyValue;
import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.MultiValuedMap;

Expand Down Expand Up @@ -104,6 +107,22 @@ public static <K, V> TypeAdapter<IterableMap<K, V>> forIterableMap(
);
}

public static <K, V> TypeAdapter<KeyValue<K, V>> forKeyValue(
final TypeAdapter<V> valueTypeAdapter,
final BiFunction<? super K, ? super V, ? extends KeyValue<K, V>> createEntry,
final Function<? super K, String> encodeKey,
final Function<? super String, ? extends K> decodeKey
) {
return SingleEntryTypeAdapter.getInstance(
valueTypeAdapter,
KeyValue::getKey,
KeyValue::getValue,
createEntry,
encodeKey,
decodeKey
);
}

public static <E> TypeAdapter<MultiSet<E>> forMultiSet(
final TypeAdapter<E> elementTypeAdapter,
final Supplier<? extends IBuilder1<? super E, ? extends MultiSet<E>>> builderFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lsh.ext.gson.ext.org.apache.commons.collections4;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -13,11 +14,13 @@
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.BoundedCollection;
import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.KeyValue;
import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.bag.HashBag;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.collections4.bidimap.DualLinkedHashBidiMap;
import org.apache.commons.collections4.keyvalue.UnmodifiableMapEntry;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
import org.apache.commons.collections4.multiset.HashMultiSet;
Expand Down Expand Up @@ -124,6 +127,29 @@ public static <V> Supplier<IBuilder2<String, V, IterableMap<String, V>>> default
throw new UnsupportedOperationException(String.valueOf(typeToken));
}

public static final ITypeAdapterFactory<KeyValue<String, Object>> defaultForKeyValue = forKeyValue(UnmodifiableMapEntry::new);

public static <V> ITypeAdapterFactory<KeyValue<String, V>> forKeyValue(
final BiFunction<? super String, ? super V, ? extends KeyValue<String, V>> createEntry
) {
return forKeyValue(createEntry, Function.identity(), Function.identity());
}

public static <K, V> ITypeAdapterFactory<KeyValue<K, V>> forKeyValue(
final BiFunction<? super K, ? super V, ? extends KeyValue<K, V>> createEntry,
final Function<? super K, String> toKey,
final Function<? super String, ? extends K> fromKey
) {
return ITypeAdapterFactory.forClassHierarchy(
KeyValue.class,
provider -> ApacheCommonsCollections4TypeAdapter.forKeyValue(provider.getTypeAdapter(1), createEntry, toKey, fromKey)
);
}

public static <V> Supplier<IBuilder2<String, V, KeyValue<String, V>>> defaultBuilderForKeyValue(final TypeToken<? super KeyValue<String, V>> typeToken) {
throw new UnsupportedOperationException(String.valueOf(typeToken));
}

public static ITypeAdapterFactory<MultiSet<Object>> defaultForMultiSet = forMultiSet(ApacheCommonsCollections4TypeAdapterFactory::defaultBuilderForMultiSet);

public static <E> ITypeAdapterFactory<MultiSet<E>> forMultiSet(
Expand Down

0 comments on commit 3eb3548

Please sign in to comment.