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

net.sf.javagimmicks.collections.bidimap.DualSortedBidiMap Maven / Gradle / Ivy

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

import java.util.AbstractMap;
import java.util.Comparator;
import java.util.Set;
import java.util.SortedMap;

class DualSortedBidiMap extends DualBidiMap implements SortedBidiMap
{
   DualSortedBidiMap(final SortedMap forwardMap, final SortedMap reverseMap)
   {
      super(forwardMap, reverseMap);
   }

   @Override
   public SortedBidiMap inverseBidiSortedMap()
   {
      return new InverseDualSortedBidiMap(getReverseMap(), getForwardMap());
   }

   @Override
   public SortedMap headMap(final K toKey)
   {
      return new SortedSubMapDecorator(getForwardMap().headMap(toKey));
   }

   @Override
   public SortedMap subMap(final K fromKey, final K toKey)
   {
      return new SortedSubMapDecorator(getForwardMap().subMap(fromKey, toKey));
   }

   @Override
   public SortedMap tailMap(final K fromKey)
   {
      return new SortedSubMapDecorator(getForwardMap().tailMap(fromKey));
   }

   @Override
   public Comparator comparator()
   {
      return getForwardMap().comparator();
   }

   @Override
   public K firstKey()
   {
      return getForwardMap().firstKey();
   }

   @Override
   public K lastKey()
   {
      return getForwardMap().lastKey();
   }

   @Override
   protected SortedMap getForwardMap()
   {
      return (SortedMap) super.getForwardMap();
   }

   @Override
   protected SortedMap getReverseMap()
   {
      return (SortedMap) super.getReverseMap();
   }

   protected class InverseDualSortedBidiMap extends DualSortedBidiMap
   {
      protected InverseDualSortedBidiMap(final SortedMap reverseMap, final SortedMap forwardMap)
      {
         super(reverseMap, forwardMap);
      }

      @Override
      public SortedBidiMap inverseBidiSortedMap()
      {
         return DualSortedBidiMap.this;
      }
   }

   protected class SortedSubMapDecorator extends AbstractMap implements SortedMap
   {
      protected final SortedMap m_oSubMap;

      protected SortedSubMapDecorator(final SortedMap oSubMap)
      {
         m_oSubMap = oSubMap;
      }

      @Override
      public Set> entrySet()
      {
         return new DualBidiEntrySet(m_oSubMap.entrySet());
      }

      @Override
      public SortedMap headMap(final K toKey)
      {
         return new SortedSubMapDecorator(m_oSubMap.headMap(toKey));
      }

      @Override
      public SortedMap subMap(final K fromKey, final K toKey)
      {
         return new SortedSubMapDecorator(m_oSubMap.subMap(fromKey, toKey));
      }

      @Override
      public SortedMap tailMap(final K fromKey)
      {
         return new SortedSubMapDecorator(m_oSubMap.tailMap(fromKey));
      }

      @Override
      public V put(final K key, final V value)
      {
         checkValue(value);

         final V oldValue = m_oSubMap.put(key, value);
         final K oldKey = getReverseMap().put(value, key);

         getForwardMap().remove(oldKey);
         getReverseMap().remove(oldValue);

         return oldValue;
      }

      @Override
      public void clear()
      {
         m_oSubMap.clear();
      }

      @Override
      public Comparator comparator()
      {
         return m_oSubMap.comparator();
      }

      @Override
      public boolean containsKey(final Object key)
      {
         return m_oSubMap.containsKey(key);
      }

      @Override
      public boolean containsValue(final Object value)
      {
         return m_oSubMap.containsValue(value);
      }

      @Override
      public K firstKey()
      {
         return m_oSubMap.firstKey();
      }

      @Override
      public V get(final Object key)
      {
         return m_oSubMap.get(key);
      }

      @Override
      public boolean isEmpty()
      {
         return m_oSubMap.isEmpty();
      }

      @Override
      public K lastKey()
      {
         return m_oSubMap.lastKey();
      }

      @Override
      public int size()
      {
         return m_oSubMap.size();
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy