net.sf.javagimmicks.collections8.transformer.ValueTransformingMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gimmicks Show documentation
Show all versions of gimmicks Show documentation
Utility classes, APIs and tools for Java
package net.sf.javagimmicks.collections8.transformer;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import net.sf.javagimmicks.transform8.Transforming;
class ValueTransformingMap
extends AbstractMap
implements Transforming
{
protected final Map _internalMap;
private final Function _transformer;
ValueTransformingMap(Map map, Function valueTransformer)
{
_internalMap = map;
_transformer = valueTransformer;
}
public Function getTransformerFunction()
{
return _transformer;
}
public void clear()
{
_internalMap.clear();
}
public boolean containsKey(Object key)
{
return _internalMap.containsKey(key);
}
public Set> entrySet()
{
return TransformerUtils.decorate(
_internalMap.entrySet(),
new ValueTransformingEntryTransformer(getTransformerFunction()));
}
public VT get(Object key)
{
VF result = _internalMap.get(key);
if(result == null && !containsKey(key))
{
return null;
}
return transform(result);
}
public boolean isEmpty()
{
return _internalMap.isEmpty();
}
public Set keySet()
{
return _internalMap.keySet();
}
public VT remove(Object key)
{
if(!containsKey(key))
{
return null;
}
VF result = _internalMap.remove(key);
return transform(result);
}
public int size()
{
return _internalMap.size();
}
public Collection values()
{
return TransformerUtils.decorate(
_internalMap.values(),
getTransformerFunction());
}
protected VT transform(VF element)
{
return getTransformerFunction().apply(element);
}
protected static class ValueTransformingEntry
implements Entry, Transforming
{
protected final Function _transformer;
protected final Entry _internalEntry;
public ValueTransformingEntry(Entry entry, Function transformer)
{
_transformer = transformer;
_internalEntry = entry;
}
public Function getTransformerFunction()
{
return _transformer;
}
public K getKey()
{
return _internalEntry.getKey();
}
public VT getValue()
{
return getTransformerFunction().apply(_internalEntry.getValue());
}
public VT setValue(VT value)
{
throw new UnsupportedOperationException();
}
}
protected static class ValueTransformingEntryTransformer
implements Function, Entry>
{
protected final Function _transformer;
public ValueTransformingEntryTransformer(Function transformer)
{
_transformer = transformer;
}
public Entry apply(Entry source)
{
return new ValueTransformingEntry(source, _transformer);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy