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

net.sf.javagimmicks.collections8.composite.CompositeList Maven / Gradle / Ivy

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

import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Spliterator;

class CompositeList extends AbstractList
{
   protected final List> _lists;

   CompositeList(final List> lists)
   {
      _lists = lists;
   }

   @Override
   public E get(int index)
   {
      for (final List list : _lists)
      {
         final int listSize = list.size();

         if (index < listSize)
         {
            return list.get(index);
         }

         index -= listSize;
      }

      throw new IndexOutOfBoundsException();
   }

   @Override
   public E set(int index, final E element)
   {
      for (final List list : _lists)
      {
         final int listSize = list.size();

         if (index < listSize)
         {
            return list.set(index, element);
         }

         index -= listSize;
      }

      throw new IndexOutOfBoundsException();
   }

   @Override
   public E remove(int index)
   {
      for (final List list : _lists)
      {
         final int listSize = list.size();

         if (index < listSize)
         {
            return list.remove(index);
         }

         index -= listSize;
      }

      throw new IndexOutOfBoundsException();
   }

   @Override
   public int size()
   {
      int result = 0;
      for (final List list : _lists)
      {
         result += list.size();
      }

      return result;
   }

   @Override
   public ListIterator listIterator(final int index)
   {
      if (index < 0 || index > size())
      {
         throw new IndexOutOfBoundsException();
      }

      return new CompositeListIterator(_lists, index);
   }

   @Override
   public Iterator iterator()
   {
      return CompositeIterator.fromCollectionList(_lists);
   }

   @Override
   public Spliterator spliterator()
   {
      return CompositeSpliterator.fromCollectionList(_lists);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy