|
|
@@ -38,13 +38,13 @@ public class MapUtils {
|
|
|
* @param <RV> target type
|
|
|
* @return read-only view
|
|
|
*/
|
|
|
- public static <K, TV, RV> Map<K,RV> map(Map<K,TV> map, Function<? super TV, ? extends RV> function) {
|
|
|
- return new MapAdapterKV<>(map, function, null);
|
|
|
+ public static <K, TV, RV> Map<K,RV> valueAdapter(Map<K,TV> map, Function<? super TV, ? extends RV> function) {
|
|
|
+ return new MapAdapter<>(map, function, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Creates view which transforms values stored inside source map.
|
|
|
- * Returned view supports modifications: it uses passed projection for conversion in both directions.
|
|
|
+ * Returned view supports insertions and replacements: it uses passed projection for conversion in both directions.
|
|
|
*
|
|
|
* @param map source map
|
|
|
* @param projection projection
|
|
|
@@ -53,8 +53,8 @@ public class MapUtils {
|
|
|
* @param <RV> target type
|
|
|
* @return mutable view
|
|
|
*/
|
|
|
- public static <K, TV, RV> Map<K,RV> map(Map<K,TV> map, ProjectionFunction<TV, RV> projection) {
|
|
|
- return new MapProjection<>(map, projection);
|
|
|
+ public static <K, TV, RV> Map<K,RV> valueProjection(Map<K,TV> map, ProjectionFunction<TV, RV> projection) {
|
|
|
+ return new MapAdapter<>(map, projection, (Serializable & BiFunction<K,RV,RV>)(k,v) -> projection.apply(map.put(k, projection.invert(v))));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -64,15 +64,15 @@ public class MapUtils {
|
|
|
* It passes to inserter "key" and "new value".
|
|
|
*
|
|
|
* @param map source map
|
|
|
- * @param function mapping function
|
|
|
+ * @param getter mapping function
|
|
|
* @param inserter insertion function
|
|
|
* @param <K> K
|
|
|
* @param <TV> source type
|
|
|
* @param <RV> target type
|
|
|
* @return mutable view
|
|
|
*/
|
|
|
- public static <K, TV, RV> Map<K,RV> mapKV(Map<K,TV> map, Function<? super TV, ? extends RV> function, BiFunction<? super K, ? super RV, ? extends RV> inserter) {
|
|
|
- return new MapAdapterKV<>(map, function, inserter);
|
|
|
+ public static <K, TV, RV> Map<K,RV> valueProjection(Map<K,TV> map, Function<? super TV, ? extends RV> getter, BiFunction<? super K, ? super RV, ? extends RV> inserter) {
|
|
|
+ return new MapAdapter<>(map, getter, inserter);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -82,15 +82,15 @@ public class MapUtils {
|
|
|
* It passes to replacer "previous value" and "new value".
|
|
|
*
|
|
|
* @param map source map
|
|
|
- * @param function mapping function
|
|
|
- * @param replacer insertion function
|
|
|
+ * @param getter mapping function
|
|
|
+ * @param setter insertion function
|
|
|
* @param <K> K
|
|
|
* @param <TV> source type
|
|
|
* @param <RV> target type
|
|
|
* @return mutable view
|
|
|
*/
|
|
|
- public static <K, TV, RV> Map<K,RV> mapVV(Map<K,TV> map, Function<? super TV, ? extends RV> function, BiFunction<? super TV, ? super RV, ? extends RV> replacer) {
|
|
|
- return new MapAdapterVV<>(map, function, replacer);
|
|
|
+ public static <K, TV, RV> Map<K,RV> valueDelegate(Map<K,TV> map, Function<? super TV, ? extends RV> getter, BiFunction<? super TV, ? super RV, ? extends RV> setter) {
|
|
|
+ return new MapDelegate<>(map, getter, setter);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -105,8 +105,8 @@ public class MapUtils {
|
|
|
* @return read-only view
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static <K, TV, RV> MultiMap<K,RV> map(MultiMap<K,TV> map, Function<? super TV, ? extends RV> function) {
|
|
|
- return new MultiMapAdapterKV<>(map, function, null);
|
|
|
+ public static <K, TV, RV> MultiMap<K,RV> valueAdapter(MultiMap<K,TV> map, Function<? super TV, ? extends RV> function) {
|
|
|
+ return new MultiMapAdapter<>(map, function, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -120,8 +120,8 @@ public class MapUtils {
|
|
|
* @param <RV> target type
|
|
|
* @return mutable view
|
|
|
*/
|
|
|
- public static <K, TV, RV> MultiMap<K,RV> map(MultiMap<K,TV> map, ProjectionFunction<TV, RV> projection) {
|
|
|
- return new MultiMapProjection<>(map, projection);
|
|
|
+ public static <K, TV, RV> MultiMap<K,RV> valueProjection(MultiMap<K,TV> map, ProjectionFunction<TV, RV> projection) {
|
|
|
+ return new MultiMapAdapter<>(map, projection, (k,v) -> projection.apply(map.put(k, projection.invert(v))));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -137,8 +137,8 @@ public class MapUtils {
|
|
|
* @param <RV> target type
|
|
|
* @return mutable view
|
|
|
*/
|
|
|
- public static <K, TV, RV> MultiMap<K,RV> mapKV(MultiMap<K,TV> map, Function<? super TV, ? extends RV> function, BiFunction<? super K, ? super RV, ? extends RV> inserter) {
|
|
|
- return new MultiMapAdapterKV<>(map, function, inserter);
|
|
|
+ public static <K, TV, RV> MultiMap<K,RV> valueProjection(MultiMap<K,TV> map, Function<? super TV, ? extends RV> function, BiFunction<? super K, ? super RV, ? extends RV> inserter) {
|
|
|
+ return new MultiMapAdapter<>(map, function, inserter);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -155,8 +155,8 @@ public class MapUtils {
|
|
|
* @param <RV> target type
|
|
|
* @return mutable view
|
|
|
*/
|
|
|
- public static <K, TV, RV> MultiMap<K,RV> mapVV(MultiMap<K,TV> map, Function<? super TV, ? extends RV> function, BiFunction<? super TV, ? super RV, ? extends RV> replacer) {
|
|
|
- return new MultiMapAdapterVV<>(map, function, replacer);
|
|
|
+ public static <K, TV, RV> MultiMap<K,RV> valueDelegate(MultiMap<K,TV> map, Function<? super TV, ? extends RV> function, BiFunction<? super TV, ? super RV, ? extends RV> replacer) {
|
|
|
+ return new MultiMapDelegate<>(map, function, replacer);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -167,13 +167,12 @@ public class MapUtils {
|
|
|
*
|
|
|
* @param content content
|
|
|
* @param acceptor acceptor
|
|
|
- * @param function function
|
|
|
* @param <K> K
|
|
|
* @param <V> V
|
|
|
* @return view
|
|
|
*/
|
|
|
- public static <K,V> Map<K,V> keymap(Map<K, V> content, Predicate<Object> acceptor, Function<K, K> function) {
|
|
|
- return new KeyMapFilter<>(content, acceptor, function);
|
|
|
+ public static <K,V> Map<K,V> keyFilter(Map<K, V> content, Predicate<Object> acceptor) {
|
|
|
+ return new KeyFilter<>(content, acceptor);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -184,11 +183,27 @@ public class MapUtils {
|
|
|
* @param keyMapper keyMapper
|
|
|
* @param <TK> TK
|
|
|
* @param <RK> RK
|
|
|
- * @param <V> V
|
|
|
+ * @param <V> TV
|
|
|
+ * @return view
|
|
|
+ */
|
|
|
+ public static <TK, RK, V> Map<RK, V> keyMap(Map<TK, ? extends V> map, Function<TK, RK> keyMapper) {
|
|
|
+ return new KeyAdapter<>(map, keyMapper, Function.identity());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * It cretes read-only view of map which keys are created by provided functions.
|
|
|
+ * Maps keys at access time, before lookup.
|
|
|
+ *
|
|
|
+ * @param map map
|
|
|
+ * @param keyMapper keyMapper
|
|
|
+ * @param <TK> TK
|
|
|
+ * @param <RK> RK
|
|
|
+ * @param <TV> TV
|
|
|
+ * @param <RV> RV
|
|
|
* @return view
|
|
|
*/
|
|
|
- public static <TK, RK, V> Map<RK,V> keymap(Map<TK, ? extends V> map, Function<TK, RK> keyMapper) {
|
|
|
- return new KeyMapAdapter<>(map, keyMapper, Function.identity());
|
|
|
+ public static <TK, RK, TV, RV> Map<RK,RV> keyMap(Map<TK, ? extends TV> map, Function<TK, RK> keyMapper, Function<TV, RV> valueMapper) {
|
|
|
+ return new KeyAdapter<>(map, keyMapper, valueMapper);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -272,126 +287,7 @@ public class MapUtils {
|
|
|
return target;
|
|
|
}
|
|
|
|
|
|
- private static class KeyMapAdapter<TK, RK, TV, RV> extends AMap<RK, RV> {
|
|
|
-
|
|
|
- private final Map<TK,? extends TV> content;
|
|
|
-
|
|
|
- private final Function<TK, RK> kmap;
|
|
|
-
|
|
|
- private final Function<TV, RV> vmap;
|
|
|
-
|
|
|
- public KeyMapAdapter(Map<TK,? extends TV> content, Function<TK, RK> kmap, Function<TV, RV> vmap) {
|
|
|
- this.content = content;
|
|
|
- this.kmap = kmap;
|
|
|
- this.vmap = vmap;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int size() {
|
|
|
- return content.size();
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- @Override
|
|
|
- public boolean containsKey(Object key) {
|
|
|
- try {
|
|
|
- return content.containsKey(kmap.apply((TK) key));
|
|
|
- } catch (ClassCastException e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- @Override
|
|
|
- public RV get(Object key) {
|
|
|
- try {
|
|
|
- return vmap.apply(content.get(kmap.apply((TK) key)));
|
|
|
- } catch (ClassCastException e) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- @Override
|
|
|
- public boolean containsValue(Object value) {
|
|
|
- try {
|
|
|
- return content.containsValue(vmap.apply((TV) value));
|
|
|
- } catch (ClassCastException e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Set<RK> keySet() {
|
|
|
- return SetUtils.map(content.keySet(), kmap);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Collection<RV> values() {
|
|
|
- return CollectionUtils.map(content.values(), vmap);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Set<Entry<RK, RV>> entrySet() {
|
|
|
- return SetUtils.map(content.entrySet(), e -> new SimpleEntry<>(kmap.apply(e.getKey()), vmap.apply(e.getValue())));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static class MultiMapAdapterKV<K, TV, RV> extends MapAdapterKV<K, TV, RV> implements MultiMap<K, RV> {
|
|
|
-
|
|
|
- private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
- private final MultiMap<K, TV> mcontent;
|
|
|
-
|
|
|
- public MultiMapAdapterKV(MultiMap<K, TV> mcontent, Function<? super TV, ? extends RV> function, BiFunction<? super K, ? super RV, ? extends RV> setter) {
|
|
|
- super(mcontent, function, setter);
|
|
|
- this.mcontent = mcontent;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Collection<RV> getAll(Object key) {
|
|
|
- return CollectionUtils.map(mcontent.getAll(key), function);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private static class MultiMapAdapterVV<K, TV, RV> extends MapAdapterVV<K,TV,RV> implements MultiMap<K, RV> {
|
|
|
-
|
|
|
- private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
- private final MultiMap<K, TV> mcontent;
|
|
|
-
|
|
|
- public MultiMapAdapterVV(MultiMap<K, TV> content, Function<? super TV, ? extends RV> getter, BiFunction<? super TV, ? super RV, ? extends RV> setter) {
|
|
|
- super(content, getter, setter);
|
|
|
- this.mcontent = content;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Collection<RV> getAll(Object key) {
|
|
|
- return CollectionUtils.map(mcontent.getAll(key), getter);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private static class MultiMapProjection<K, TV, RV> extends MapProjection<K,TV,RV> implements MultiMap<K, RV> {
|
|
|
-
|
|
|
- private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
- private final MultiMap<K, TV> mcontent;
|
|
|
-
|
|
|
- public MultiMapProjection(MultiMap<K, TV> content, ProjectionFunction<TV, RV> function) {
|
|
|
- super(content, function);
|
|
|
- this.mcontent = content;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Collection<RV> getAll(Object key) {
|
|
|
- return CollectionUtils.map(mcontent.getAll(key), function);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private static class MapAdapterKV<K, TV, RV> extends AMap<K, RV> implements Map<K, RV> {
|
|
|
+ private static class MapAdapter<K, TV, RV> extends AMap<K, RV> implements Map<K, RV> {
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
@@ -404,7 +300,7 @@ public class MapUtils {
|
|
|
protected final BiFunction<? super K, ? super RV, ? extends RV> inserter;
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public MapAdapterKV(Map<K, TV> content, Function<? super TV, ? extends RV> function, BiFunction<? super K, ? super RV, ? extends RV> setter) {
|
|
|
+ public MapAdapter(Map<K, TV> content, Function<? super TV, ? extends RV> function, BiFunction<? super K, ? super RV, ? extends RV> setter) {
|
|
|
this.content = content;
|
|
|
this.function = function;
|
|
|
this.inserter = null != setter ? setter : NSE;
|
|
|
@@ -488,7 +384,7 @@ public class MapUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static class MapAdapterVV<K, TV, RV> extends AMap<K, RV> implements Map<K, RV> {
|
|
|
+ private static class MapDelegate<K, TV, RV> extends AMap<K, RV> implements Map<K, RV> {
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
@@ -496,7 +392,7 @@ public class MapUtils {
|
|
|
protected final Function<? super TV, ? extends RV> getter;
|
|
|
protected final BiFunction<? super TV, ? super RV, ? extends RV> setter;
|
|
|
|
|
|
- public MapAdapterVV(Map<K,TV> content, Function<? super TV, ? extends RV> getter, BiFunction<? super TV, ? super RV, ? extends RV> setter) {
|
|
|
+ public MapDelegate(Map<K,TV> content, Function<? super TV, ? extends RV> getter, BiFunction<? super TV, ? super RV, ? extends RV> setter) {
|
|
|
this.content = content;
|
|
|
this.getter = getter;
|
|
|
this.setter = setter;
|
|
|
@@ -525,8 +421,8 @@ public class MapUtils {
|
|
|
|
|
|
@Override
|
|
|
public RV put(K key, RV value) {
|
|
|
- TV var = content.get(key);
|
|
|
- return null!=var ? setter.apply(var, value) : null;
|
|
|
+ TV original = content.get(key);
|
|
|
+ return null!=original ? setter.apply(original, value) : null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -571,165 +467,151 @@ public class MapUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static class MapProjection<K, TV, RV> extends AMap<K, RV> implements Map<K, RV> {
|
|
|
-
|
|
|
- private static final long serialVersionUID = 1L;
|
|
|
+ private static class MultiMapAdapter<K, TV, RV> extends MapAdapter<K, TV, RV> implements MultiMap<K, RV> {
|
|
|
|
|
|
- protected final Map<K, TV> content;
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
|
|
|
- protected final ProjectionFunction<TV, RV> function;
|
|
|
+ private final MultiMap<K, TV> mcontent;
|
|
|
|
|
|
- public MapProjection(Map<K, TV> content, ProjectionFunction<TV, RV> function) {
|
|
|
- this.content = content;
|
|
|
- this.function = function;
|
|
|
- }
|
|
|
-
|
|
|
- public MapProjection(MultiMap<K, TV> content, ProjectionFunction<TV, RV> function) {
|
|
|
- this.content = content;
|
|
|
- this.function = function;
|
|
|
+ public MultiMapAdapter(MultiMap<K, TV> mcontent, Function<? super TV, ? extends RV> function, BiFunction<? super K, ? super RV, ? extends RV> setter) {
|
|
|
+ super(mcontent, function, setter);
|
|
|
+ this.mcontent = mcontent;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public int size() {
|
|
|
- return content.size();
|
|
|
+ public Collection<RV> getAll(Object key) {
|
|
|
+ return CollectionUtils.map(mcontent.getAll(key), function);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public boolean containsKey(Object key) {
|
|
|
- return content.containsKey(key);
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- @Override
|
|
|
- public boolean containsValue(Object value) {
|
|
|
- try {
|
|
|
- return content.containsValue(function.invert((RV)value));
|
|
|
- } catch(ClassCastException _ex) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class MultiMapDelegate<K, TV, RV> extends MapDelegate<K,TV,RV> implements MultiMap<K, RV> {
|
|
|
+
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+
|
|
|
+ private final MultiMap<K, TV> mcontent;
|
|
|
+
|
|
|
+ public MultiMapDelegate(MultiMap<K, TV> content, Function<? super TV, ? extends RV> getter, BiFunction<? super TV, ? super RV, ? extends RV> setter) {
|
|
|
+ super(content, getter, setter);
|
|
|
+ this.mcontent = content;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void clear() {
|
|
|
- content.clear();
|
|
|
+ public Collection<RV> getAll(Object key) {
|
|
|
+ return CollectionUtils.map(mcontent.getAll(key), getter);
|
|
|
}
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ static class EmptyMultiMap<K,V> extends AMap<K,V> implements MultiMap<K, V> {
|
|
|
+
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+
|
|
|
@Override
|
|
|
- public RV remove(Object key) {
|
|
|
- TV prev = content.remove(key);
|
|
|
- return prev==null ? null : function.apply(prev);
|
|
|
+ public Set<Map.Entry<K, V>> entrySet() {
|
|
|
+ return Collections.emptySet();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Set<K> keySet() {
|
|
|
- return content.keySet();
|
|
|
+ public int size() {
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Collection<RV> values() {
|
|
|
- return CollectionUtils.map(content.values(), function);
|
|
|
+ public boolean containsKey(Object key) {
|
|
|
+ return false;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
- public Set<Map.Entry<K, RV>> entrySet() {
|
|
|
- return SetUtils.map(content.entrySet(), e -> new EntryProjection(e));
|
|
|
+ public boolean containsValue(Object value) {
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public RV get(Object key) {
|
|
|
- return function.apply(content.get(key));
|
|
|
+ public V get(Object key) {
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public RV put(K key, RV value) {
|
|
|
- return function.apply(content.put(key, function.invert(value)));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public boolean remove(Object key, Object value) {
|
|
|
- Object v;
|
|
|
- try {
|
|
|
- v = function.invert((RV)value);
|
|
|
- } catch(ClassCastException _e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- return content.remove(key, v);
|
|
|
+ public Collection<V> getAll(Object key) {
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
- private final class EntryProjection extends AEntry<K, RV> {
|
|
|
-
|
|
|
- private final Map.Entry<K,TV> delegate;
|
|
|
+ }
|
|
|
|
|
|
- public EntryProjection(Map.Entry<K, TV> delegate) {
|
|
|
- this.delegate = delegate;
|
|
|
- }
|
|
|
+ private static class KeyAdapter<TK, RK, TV, RV> extends AMap<RK, RV> {
|
|
|
|
|
|
- @Override
|
|
|
- public K getKey() {
|
|
|
- return delegate.getKey();
|
|
|
- }
|
|
|
+ private final Map<TK,? extends TV> content;
|
|
|
|
|
|
- @Override
|
|
|
- public RV getValue() {
|
|
|
- return function.apply(delegate.getValue());
|
|
|
- }
|
|
|
+ private final Function<TK, RK> kmap;
|
|
|
|
|
|
- @Override
|
|
|
- public RV setValue(RV value) {
|
|
|
- return function.apply(content.put(getKey(), function.invert(value)));
|
|
|
- }
|
|
|
+ private final Function<TV, RV> vmap;
|
|
|
|
|
|
+ public KeyAdapter(Map<TK,? extends TV> content, Function<TK, RK> kmap, Function<TV, RV> vmap) {
|
|
|
+ this.content = content;
|
|
|
+ this.kmap = kmap;
|
|
|
+ this.vmap = vmap;
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- static class EmptyMM<K,V> extends AMap<K,V> implements MultiMap<K, V> {
|
|
|
-
|
|
|
- private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
@Override
|
|
|
- public Set<Map.Entry<K, V>> entrySet() {
|
|
|
- return Collections.emptySet();
|
|
|
+ public int size() {
|
|
|
+ return content.size();
|
|
|
}
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
@Override
|
|
|
- public int size() {
|
|
|
- return 0;
|
|
|
+ public boolean containsKey(Object key) {
|
|
|
+ try {
|
|
|
+ return content.containsKey(kmap.apply((TK) key));
|
|
|
+ } catch (ClassCastException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
@Override
|
|
|
- public boolean containsKey(Object key) {
|
|
|
- return false;
|
|
|
+ public RV get(Object key) {
|
|
|
+ try {
|
|
|
+ return vmap.apply(content.get(kmap.apply((TK) key)));
|
|
|
+ } catch (ClassCastException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
@Override
|
|
|
public boolean containsValue(Object value) {
|
|
|
- return false;
|
|
|
+ try {
|
|
|
+ return content.containsValue(vmap.apply((TV) value));
|
|
|
+ } catch (ClassCastException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public V get(Object key) {
|
|
|
- return null;
|
|
|
+ public Set<RK> keySet() {
|
|
|
+ return SetUtils.map(content.keySet(), kmap);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
- public Collection<V> getAll(Object key) {
|
|
|
- return Collections.emptyList();
|
|
|
+ public Collection<RV> values() {
|
|
|
+ return CollectionUtils.map(content.values(), vmap);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Set<Entry<RK, RV>> entrySet() {
|
|
|
+ return SetUtils.map(content.entrySet(), e -> new SimpleEntry<>(kmap.apply(e.getKey()), vmap.apply(e.getValue())));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static class KeyMapFilter<K, V> extends AMap<K, V> {
|
|
|
+ private static class KeyFilter<K, V> extends AMap<K, V> {
|
|
|
|
|
|
protected final Map<K, V> content;
|
|
|
- protected final Predicate<Object> acceptor;
|
|
|
- protected final Function<K, K> function;
|
|
|
+ protected final Predicate<Object> filter;
|
|
|
|
|
|
- KeyMapFilter(Map<K, V> content, Predicate<Object> acceptor, Function<K, K> function) {
|
|
|
+ KeyFilter(Map<K, V> content, Predicate<Object> acceptor) {
|
|
|
this.content = content;
|
|
|
- this.acceptor = acceptor;
|
|
|
- this.function = function;
|
|
|
+ this.filter = acceptor;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -749,17 +631,17 @@ public class MapUtils {
|
|
|
|
|
|
@Override
|
|
|
public V get(Object key) {
|
|
|
- return acceptor.test(key) ? content.get($key(key)) : null;
|
|
|
+ return filter.test(key) ? content.get($key(key)) : null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public V getOrDefault(Object key, V defaultValue) {
|
|
|
- return acceptor.test(key) ? content.getOrDefault($key(key), defaultValue) : defaultValue;
|
|
|
+ return filter.test(key) ? content.getOrDefault($key(key), defaultValue) : defaultValue;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean containsKey(Object key) {
|
|
|
- return acceptor.test(key) && content.containsKey($key(key));
|
|
|
+ return filter.test(key) && content.containsKey($key(key));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -778,7 +660,7 @@ public class MapUtils {
|
|
|
|
|
|
@Override
|
|
|
public V remove(Object key) {
|
|
|
- return acceptor.test(key) ? content.remove($key(key)) : null;
|
|
|
+ return filter.test(key) ? content.remove($key(key)) : null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -788,7 +670,7 @@ public class MapUtils {
|
|
|
|
|
|
@Override
|
|
|
public boolean remove(Object key, Object value) {
|
|
|
- return acceptor.test(key) && content.remove($key(key), value);
|
|
|
+ return filter.test(key) && content.remove($key(key), value);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -823,7 +705,7 @@ public class MapUtils {
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
private K $key(Object key) {
|
|
|
- return key!=null ? function.apply((K)key) : null;
|
|
|
+ return (K)key;
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|