1 24 package org.archive.util; 25 26 27 import java.io.Serializable ; 28 import java.util.AbstractList ; 29 import java.util.List ; 30 31 32 44 public class SubList<E> extends AbstractList <E> implements Serializable { 45 46 private static final long serialVersionUID = 1L; 47 48 51 final private List <E> delegate; 52 53 56 private int start; 57 58 61 private int end; 62 63 64 74 public SubList(List <E> delegate, int start, int end) { 75 if ((start < 0) || (start > delegate.size())) { 76 throw new IndexOutOfBoundsException (); 77 } 78 if ((end < 0) || (end > delegate.size())) { 79 throw new IndexOutOfBoundsException (); 80 } 81 if (end < start) { 82 throw new IllegalArgumentException (); 83 } 84 this.delegate = delegate; 85 this.start = start; 86 this.end = end; 87 } 88 89 96 private void ensureInside(int index) { 97 if ((index < 0) || (index >= end - start)) { 98 throw new IndexOutOfBoundsException (); 99 } 100 } 101 102 110 private void ensureBorder(int index) { 111 if ((index < 0) || (index > end - start)) { 112 throw new IndexOutOfBoundsException (); 113 } 114 } 115 116 117 @Override 118 public E get(int index) { 119 ensureInside(index); 120 return delegate.get(start + index); 121 } 122 123 124 @Override 125 public int size() { 126 return end - start; 127 } 128 129 130 @Override 131 public E set(int index, E value) { 132 ensureInside(index); 133 return delegate.set(start + index, value); 134 } 135 136 137 @Override 138 public void add(int index, E value) { 139 ensureBorder(index); 140 delegate.add(start + index, value); 141 end++; 142 } 143 144 145 @Override 146 public E remove(int index) { 147 ensureInside(index); 148 return delegate.remove(start + index); 149 } 150 } 151 | Popular Tags |