| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.flatfile; |
| 18 | |
|
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.Collection; |
| 21 | |
import java.util.Collections; |
| 22 | |
import java.util.List; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | 56 | public class EntityArray extends EntityCollectionSupport implements |
| 30 | |
IndexedEntityCollection { |
| 31 | |
private static final long serialVersionUID = 8716199462287161060L; |
| 32 | 38 | private int size = -1; |
| 33 | 38 | private int minimumSize = 0; |
| 34 | 38 | private int maximumSize = Integer.MAX_VALUE; |
| 35 | 38 | private boolean resizable = false; |
| 36 | |
private List<Entity> children; |
| 37 | |
private Entity prototype; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 0 | public EntityArray() { |
| 43 | |
|
| 44 | 0 | } |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | public EntityArray(int size) { |
| 51 | 0 | setSize(size); |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 38 | public EntityArray(Entity prototype) { |
| 59 | 38 | setPrototype(prototype); |
| 60 | 38 | } |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public EntityArray(Entity prototype, int size) { |
| 68 | 0 | this(prototype); |
| 69 | 0 | setSize(size); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public Entity getChild(int index) { |
| 78 | 49 | initialize(); |
| 79 | 49 | return (Entity) children.get(index); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public synchronized Collection<Entity> getChildren() { |
| 86 | 115 | initialize(); |
| 87 | 114 | return Collections.unmodifiableCollection(children); |
| 88 | |
} |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public synchronized Entity getPrototype() { |
| 95 | 12 | return prototype; |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
public synchronized void setPrototype(Entity prototype) { |
| 103 | 38 | if (prototype == null) { |
| 104 | 0 | throw new IllegalArgumentException("prototype Entity was null"); |
| 105 | |
} |
| 106 | 38 | this.prototype = prototype; |
| 107 | 38 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public synchronized void setSize(int size) { |
| 114 | 46 | if (this.size >= 0 && !isResizable()) { |
| 115 | 2 | if (size == this.size()) { |
| 116 | 1 | return; |
| 117 | |
} |
| 118 | 1 | throw new IllegalStateException("size already set"); |
| 119 | |
} |
| 120 | 44 | if (size < minimumSize || size > maximumSize) { |
| 121 | 3 | throw new IllegalArgumentException("illegal collection size: " |
| 122 | |
+ size + "; should be <= " + maximumSize + " and >= " |
| 123 | |
+ minimumSize); |
| 124 | |
} |
| 125 | 41 | this.size = size; |
| 126 | 41 | } |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public synchronized int size() { |
| 133 | 122 | return size; |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public synchronized boolean isSizable() { |
| 141 | 5 | return size == -1 || isResizable(); |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
@Override |
| 149 | |
public synchronized EntityArray clone() { |
| 150 | 56 | EntityArray result = (EntityArray) super.clone(); |
| 151 | 56 | if (children != null) { |
| 152 | 16 | result.children = new ArrayList<Entity>(); |
| 153 | 16 | for (Entity e : children) { |
| 154 | 349 | result.children.add(e.clone()); |
| 155 | |
} |
| 156 | |
} |
| 157 | 56 | return result; |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
private synchronized void initialize() { |
| 164 | 164 | if (children != null) { |
| 165 | 121 | return; |
| 166 | |
} |
| 167 | 43 | if (size < 0) { |
| 168 | 1 | throw new IllegalStateException("EntityArray size not set"); |
| 169 | |
} |
| 170 | 42 | if (size == 0) { |
| 171 | 4 | children = Collections.<Entity>emptyList(); |
| 172 | 4 | return; |
| 173 | |
} |
| 174 | 38 | if (prototype == null) { |
| 175 | 0 | throw new IllegalStateException("Prototype child entity not set"); |
| 176 | |
} |
| 177 | 38 | children = new ArrayList<Entity>(size); |
| 178 | 38 | adjustSize(); |
| 179 | 38 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
private synchronized void adjustSize() { |
| 185 | 38 | if (children.size() > size) { |
| 186 | 0 | children.subList(size, children.size()).clear(); |
| 187 | 0 | return; |
| 188 | |
} |
| 189 | 360 | for (int i = children.size(); i < size; i++) { |
| 190 | 322 | children.add(prototype.clone()); |
| 191 | |
} |
| 192 | 38 | } |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public synchronized int getMaximumSize() { |
| 199 | 5 | return maximumSize; |
| 200 | |
} |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
public synchronized void setMaximumSize(int maximumSize) { |
| 207 | 3 | if (maximumSize > Integer.MAX_VALUE || maximumSize < getMinimumSize()) { |
| 208 | 0 | throw new IllegalArgumentException(Integer.toString(maximumSize)); |
| 209 | |
} |
| 210 | 3 | this.maximumSize = maximumSize; |
| 211 | 3 | } |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
public synchronized int getMinimumSize() { |
| 218 | 5 | return minimumSize; |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public synchronized void setMinimumSize(int minimumSize) { |
| 226 | 3 | if (minimumSize < 0 || minimumSize > getMaximumSize()) { |
| 227 | 0 | throw new IllegalArgumentException(Integer.toString(minimumSize)); |
| 228 | |
} |
| 229 | 3 | this.minimumSize = minimumSize; |
| 230 | 3 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
public boolean isResizable() { |
| 237 | 3 | return resizable; |
| 238 | |
} |
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public void setResizable(boolean resizable) { |
| 245 | 0 | this.resizable = resizable; |
| 246 | 0 | } |
| 247 | |
|
| 248 | |
} |