001 /* 002 * www.ti.bfh.ch 003 * 004 * Copyright 2007, Berne University of Applied Sciences, 005 * School of Engineering and Information Technology 006 * and individual contributors as indicated by the @authors tag. 007 * 008 * This is free software; you can redistribute it and/or modify it under the terms of the 009 * GNU Lesser General Public License as published by the Free Software Foundation; 010 * either version 3 of the License, or (at your option) any later version. 011 * 012 * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 013 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 014 * See the GNU Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License along with this software; 017 * if not, see <http://www.gnu.org/licenses/>. 018 * 019 */ 020 package ch.bfh.algo.core.graph; 021 022 import java.util.AbstractSequentialList; 023 import java.util.List; 024 import java.util.ListIterator; 025 026 public class ListMergeAdapter<E> extends AbstractSequentialList<E>{ 027 028 private final List<E> list0; 029 private final List<E> list1; 030 031 public ListMergeAdapter(List<E> list0, List<E> list1){ 032 this.list0=list0; 033 this.list1=list1; 034 } 035 036 public int size(){ 037 return this.list0.size()+this.list1.size(); 038 } 039 040 public ListIterator<E> listIterator(int index){ 041 int firstSize=this.list0.size(); 042 final boolean first=( index==0 || index<firstSize ); 043 if(!first) index-=firstSize; 044 final ListIterator<E> iterator=this.list0.listIterator(index); 045 return new ListIterator<E>(){ 046 private ListIterator<E> iter=iterator; 047 private boolean isFirst=first; 048 049 public boolean hasNext(){ 050 if(!this.iter.hasNext()&&this.isFirst){ 051 this.iter=ListMergeAdapter.this.list1.listIterator(); 052 this.isFirst=false; 053 } 054 return this.iter.hasNext(); 055 } 056 057 public E next(){ 058 return this.iter.next(); 059 } 060 061 public boolean hasPrevious(){ 062 if(!this.iter.hasPrevious()&&!this.isFirst){ 063 this.iter=ListMergeAdapter.this.list0.listIterator(ListMergeAdapter.this.list0.size()); 064 this.isFirst=true; 065 } 066 return this.iter.hasPrevious(); 067 } 068 069 public E previous(){ 070 return this.iter.previous(); 071 } 072 073 public int nextIndex(){ 074 if(this.isFirst) return this.iter.nextIndex(); 075 else return ListMergeAdapter.this.list0.size()+this.iter.nextIndex(); 076 } 077 078 public int previousIndex(){ 079 if(this.isFirst) return this.iter.previousIndex(); 080 else return ListMergeAdapter.this.list0.size()+this.iter.previousIndex(); 081 } 082 083 public void remove(){ 084 this.iter.remove(); 085 } 086 087 public void set(E o){ 088 this.iter.set(o); 089 } 090 091 public void add(E o) { 092 this.iter.add(o); 093 } 094 }; 095 } 096 }