net.sf.javagimmicks.collections8.transformer.KeyTransformingMap 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 KeyTransformingMap
extends AbstractMap
implements Transforming
{
protected final Map _internalMap;
protected final Function _transformer;
KeyTransformingMap(Map map, Function transformer)
{
_internalMap = map;
_transformer = transformer;
}
public Function getTransformerFunction()
{
return _transformer;
}
@Override
public void clear()
{
_internalMap.clear();
}
@Override
public boolean containsValue(Object value)
{
return _internalMap.containsValue(value);
}
@Override
public Set> entrySet()
{
return TransformerUtils.decorate(
_internalMap.entrySet(),
new KeyTransformingEntryTransformer(getTransformerFunction()));
}
@Override
public boolean isEmpty()
{
return _internalMap.isEmpty();
}
@Override
public Set keySet()
{
return TransformerUtils.decorate(
_internalMap.keySet(),
getTransformerFunction());
}
@Override
public int size()
{
return _internalMap.size();
}
@Override
public Collection values()
{
return _internalMap.values();
}
protected KT transform(KF element)
{
return getTransformerFunction().apply(element);
}
protected static class KeyTransformingEntry
implements Entry, Transforming
{
protected final Entry _internalEntry;
protected final Function _transformer;
public KeyTransformingEntry(Entry entry, Function transformer)
{
_internalEntry = entry;
_transformer = transformer;
}
public Function getTransformerFunction()
{
return _transformer;
}
public KT getKey()
{
return _transformer.apply(_internalEntry.getKey());
}
public V getValue()
{
return _internalEntry.getValue();
}
public V setValue(V value)
{
return _internalEntry.setValue(value);
}
}
protected static class KeyTransformingEntryTransformer
implements Function, Entry>
{
protected final Function _transformer;
public KeyTransformingEntryTransformer(Function transformer)
{
_transformer = transformer;
}
public Entry apply(Entry source)
{
return new KeyTransformingEntry(source, _transformer);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy