All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.collections.mapping.AbstractMappings Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.collections.mapping;

import java.io.Serializable;
import java.util.AbstractSet;
import java.util.Collection;
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 Iterator> iterator() { return getMappingSet().iterator(); } @Override public Mappings invert() { return new InverseMappings(this); } @Override public Set getAllForLeftKey(final L left) { return getLeftView().get(left); } @Override public Set getAllForRightKey(final R right) { return getRightView().get(right); } @Override public boolean put(final L left, final R right) { throw new UnsupportedOperationException(); } @Override public boolean putAllForRightKey(final R right, final Collection c) { boolean result = false; for (final L left : c) { result |= put(left, right); } return result; } @Override public boolean putAllForLeftKey(final L left, final Collection c) { boolean result = false; for (final R right : c) { result |= put(left, right); } return result; } @Override public boolean remove(final L left, final R right) { final Set mappedValuesLeft = getAllForLeftKey(left); return mappedValuesLeft != null ? mappedValuesLeft.remove(right) : false; } @Override public boolean containsLeftKey(final L left) { return getLeftView().containsKey(left); } @Override public boolean containsRightKey(final R right) { return getRightView().containsKey(right); } @Override public boolean contains(final L left, final R right) { final Set rightSet = getAllForLeftKey(left); return rightSet != null && rightSet.contains(right); } @Override public Set removeLeftKey(final L left) { return getLeftView().remove(left); } @Override public Set removeRightKey(final R right) { return getRightView().remove(right); } @Override public void clear() { getLeftView().clear(); } @Override public int size() { return getMappingSet().size(); } @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