1 22 23 package com.sosnoski.util.array; 24 25 import com.sosnoski.util.*; 26 27 47 48 public abstract class ArrayBase extends GrowableBase 49 { 50 51 protected int m_countPresent; 52 53 60 61 public ArrayBase(int size, int growth, Class type) { 62 super(size, growth, type); 63 } 64 65 71 72 public ArrayBase(int size, Class type) { 73 this(size, Integer.MAX_VALUE, type); 74 } 75 76 81 82 public ArrayBase(ArrayBase base) { 83 super(base); 84 System.arraycopy(base.getArray(), 0, getArray(), 0, 85 base.m_countPresent); 86 m_countPresent = base.m_countPresent; 87 } 88 89 97 98 protected static Object getArray(ArrayBase other) { 99 return other.getArray(); 100 } 101 102 110 111 protected final int getAddIndex() { 112 int index = m_countPresent++; 113 if (m_countPresent > m_countLimit) { 114 growArray(m_countPresent); 115 } 116 return index; 117 } 118 119 124 125 protected void makeInsertSpace(int index) { 126 if (index >= 0 && index <= m_countPresent) { 127 if (++m_countPresent > m_countLimit) { 128 growArray(m_countPresent); 129 } 130 if (index < m_countPresent - 1) { 131 Object array = getArray(); 132 System.arraycopy(array, index, array, index + 1, 133 m_countPresent - index - 1); 134 } 135 } else { 136 throw new ArrayIndexOutOfBoundsException ("Invalid index value"); 137 } 138 } 139 140 147 148 public void remove(int from, int to) { 149 if (from >= 0 && to <= m_countPresent && from <= to) { 150 if (to < m_countPresent){ 151 int change = from - to; 152 Object base = getArray(); 153 System.arraycopy(base, to, base, from, m_countPresent - to); 154 discardValues(m_countPresent+change, m_countPresent); 155 m_countPresent += change; 156 } 157 } else { 158 throw new ArrayIndexOutOfBoundsException ("Invalid remove range"); 159 } 160 } 161 162 168 169 public void remove(int index) { 170 remove(index, index+1); 171 } 172 173 178 179 public final int size() { 180 return m_countPresent; 181 } 182 183 191 192 public void setSize(int count) { 193 if (count > m_countLimit) { 194 growArray(count); 195 } else if (count < m_countPresent) { 196 discardValues(count, m_countPresent); 197 } 198 m_countPresent = count; 199 } 200 201 204 205 public final void clear() { 206 setSize(0); 207 } 208 209 220 221 protected Object buildArray(Class type, int offset, int length) { 222 if (offset + length <= m_countPresent) { 223 return super.buildArray(type, offset, length); 224 } else { 225 throw new ArrayIndexOutOfBoundsException (); 226 } 227 } 228 } 229 | Popular Tags |