net.sf.javagimmicks.collections.transformer.KeyBidiTransformingMap 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.collections.transformer;
import java.util.Map;
import java.util.Set;
import net.sf.javagimmicks.transform.BidiTransformer;
import net.sf.javagimmicks.transform.BidiTransforming;
import net.sf.javagimmicks.transform.Transformers;
class KeyBidiTransformingMap
extends KeyTransformingMap
implements BidiTransforming
{
/**
* @deprecated Use TranformerUtils.decorateKeyBased() instead
*/
@Deprecated
public KeyBidiTransformingMap(Map map, BidiTransformer transformer)
{
super(map, transformer);
}
public BidiTransformer getBidiTransformer()
{
return (BidiTransformer)_transformer;
}
@Override
public Set> entrySet()
{
return TransformerUtils.decorate(
_internalMap.entrySet(),
new KeyBidiTransformingEntryBidiTransformer(
getBidiTransformer()));
}
@Override
public Set keySet()
{
return TransformerUtils.decorate(
_internalMap.keySet(),
getBidiTransformer());
}
@Override
public V put(KT key, V value)
{
return _internalMap.put(transformBack(key), value);
}
@SuppressWarnings("unchecked")
@Override
public V get(Object key)
{
try
{
return _internalMap.get(transformBack((KT)key));
}
catch(ClassCastException ex)
{
return super.get(key);
}
}
@SuppressWarnings("unchecked")
@Override
public boolean containsKey(Object key)
{
try
{
return _internalMap.containsKey(transformBack((KT)key));
}
catch(ClassCastException ex)
{
return super.containsKey(key);
}
}
@SuppressWarnings("unchecked")
@Override
public V remove(Object key)
{
try
{
return _internalMap.remove(transformBack((KT)key));
}
catch(ClassCastException ex)
{
return super.remove(key);
}
}
protected KF transformBack(KT element)
{
return getBidiTransformer().transformBack(element);
}
protected static class KeyBidiTransformingEntry
extends KeyTransformingEntry
implements BidiTransforming
{
public KeyBidiTransformingEntry(Entry entry, BidiTransformer transformer)
{
super(entry, transformer);
}
public BidiTransformer getBidiTransformer()
{
return (BidiTransformer)getTransformer();
}
}
protected static class KeyBidiTransformingEntryBidiTransformer
extends KeyTransformingEntryTransformer
implements BidiTransformer, Entry>
{
public KeyBidiTransformingEntryBidiTransformer(BidiTransformer transformer)
{
super(transformer);
}
public BidiTransformer, Entry> invert()
{
return Transformers.invert(this);
}
public Entry transformBack(Entry source)
{
return new KeyBidiTransformingEntry(source, getTransformer().invert());
}
protected BidiTransformer getTransformer()
{
return (BidiTransformer)_transformer;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy