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.sequence; 021 022 import java.util.AbstractList; 023 024 public class PositionArray<E,P extends GenericArrayPosition<E,P>> extends AbstractList<P>{ 025 026 private P[] array; 027 private int size; 028 private ArrayStrategy<P> factory; 029 030 public PositionArray(int size, ArrayStrategy<P> factory){ 031 this.factory=factory; 032 this.array=this.factory.initialize(size); 033 } 034 035 public P get(int index) throws IndexOutOfBoundsException{ 036 if(index>=this.size) throw new IndexOutOfBoundsException(); 037 return this.array[index]; 038 } 039 040 public int size(){ 041 return this.size; 042 } 043 044 public void insert(int index, P position) throws IndexOutOfBoundsException{ 045 if(index<0 || index>this.size) throw new IndexOutOfBoundsException(); 046 this.array=this.factory.enlarge(this.size,this.array); 047 for(int i=this.size,j=this.size-1;i>index;i--,j--){ 048 P p=this.array[j]; 049 p.setRank(i); 050 this.array[i]=p; 051 } 052 position.setRank(index); 053 this.array[index]=position; 054 this.size++; 055 } 056 057 public P delete(int index) throws IndexOutOfBoundsException{ 058 P position=this.get(index); 059 this.size--; 060 for(int i=index,j=index+1;i<this.size;i++,j++){ 061 P p=this.array[j]; 062 p.setRank(i); 063 this.array[i]=p; 064 } 065 this.array=this.factory.shrink(this.size,this.array); 066 return position; 067 } 068 }