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

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

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

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

class CompositeList extends AbstractList
{
   protected final List> _lists;

   CompositeList(List> lists)
   {
      _lists = lists;
   }
   
   @Override
   public E get(int index)
   {
      for(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, E element)
   {
      for(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(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(List list : _lists)
      {
         result += list.size();
      }
      
      return result;
   }

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy