net.sf.javagimmicks.collections8.mapping.AbstractMappings Maven / Gradle / Ivy
package net.sf.javagimmicks.collections8.mapping;
import java.io.Serializable;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* An abstract implementation of {@link Mappings} that provides default
* implementations for almost all methods.
*
* Methods to be implemented by subclasses are:
*
* - {@link #getLeftView()}
* - {@link #getRightView()}
* - {@link #put(Object, Object)}
*
*/
public abstract class AbstractMappings implements Mappings, Serializable
{
private static final long serialVersionUID = -8390090502631423671L;
protected AbstractMappings()
{}
@Override
public Set> getMappingSet()
{
return new MappingSet(getLeftView().entrySet());
}
@Override
public Mappings invert()
{
return new InverseMappings(this);
}
@Override
public boolean put(final L left, final R right)
{
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty()
{
return getMappingSet().isEmpty();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof Mappings, ?>))
{
return false;
}
final Mappings, ?> other = (Mappings, ?>) obj;
return getMappingSet().equals(other.getMappingSet());
}
@Override
public String toString()
{
return new StringBuilder()
.append(getLeftView())
.append(" | ")
.append(getRightView())
.toString();
}
protected static class MappingSet extends AbstractSet>
{
protected final Set>> _entries;
public MappingSet(final Set>> entries)
{
_entries = entries;
}
@Override
public Iterator> iterator()
{
return new MappingIterator(_entries.iterator());
}
@Override
public int size()
{
int size = 0;
for (final Entry> entry : _entries)
{
size += entry.getValue().size();
}
return size;
}
}
protected static class MappingIterator implements Iterator>
{
protected final Iterator>> _entryIterator;
protected Iterator _valueItertor;
protected Entry> _currentEntry;
protected R _currentValue;
protected MappingIterator(final Iterator>> entryIterator)
{
_entryIterator = entryIterator;
}
@Override
public boolean hasNext()
{
return (_valueItertor != null && _valueItertor.hasNext()) || _entryIterator.hasNext();
}
@Override
public Mapping next()
{
moveNext();
return new DefaultMapping(_currentEntry.getKey(), _currentValue);
}
@Override
public void remove()
{
_valueItertor.remove();
if (_currentEntry.getValue().isEmpty())
{
_entryIterator.remove();
}
}
private void moveNext()
{
if (_valueItertor == null || !_valueItertor.hasNext())
{
_currentEntry = _entryIterator.next();
_valueItertor = _currentEntry.getValue().iterator();
}
_currentValue = _valueItertor.next();
}
}
protected static class InverseMappings extends AbstractMappings
{
private static final long serialVersionUID = -4525522714547396946L;
protected final Mappings _partner;
public InverseMappings(final Mappings partner)
{
_partner = partner;
}
@Override
public Mappings invert()
{
return _partner;
}
@Override
public boolean put(final R left, final L right)
{
return _partner.put(right, left);
}
@Override
public void clear()
{
_partner.clear();
}
@Override
public Map> getLeftView()
{
return _partner.getRightView();
}
@Override
public Set getAllForLeftKey(final R left)
{
return _partner.getAllForRightKey(left);
}
@Override
public Set getAllForRightKey(final L right)
{
return _partner.getAllForLeftKey(right);
}
@Override
public Map> getRightView()
{
return _partner.getLeftView();
}
@Override
public boolean remove(final R left, final L right)
{
return _partner.remove(right, left);
}
@Override
public Set removeLeftKey(final R left)
{
return _partner.removeRightKey(left);
}
@Override
public Set removeRightKey(final L right)
{
return _partner.removeLeftKey(right);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy