PageRenderTime 32ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/src/codegen/java/org/joda/primitives/collection/impl/ArrayXXXCollection.vm

http://github.com/JodaOrg/joda-primitives
Velocity Template Language | 427 lines | 389 code | 38 blank | 0 comment | 0 complexity | ed00525478c6de6403d368cf2dbb48ad MD5 | raw file
Possible License(s): Apache-2.0
  1. ${license}
  2. package org.joda.primitives.collection.impl;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6. import org.joda.primitives.${Type}Utils;
  7. import org.joda.primitives.collection.${Type}Collection;
  8. import org.joda.primitives.iterator.${Type}Iterator;
  9. /**
  10. * Array based implementation of <code>${Type}Collection</code> for
  11. * primitive <code>${type}</code> elements.
  12. * <p>
  13. * This collection implementation allows multiple copies of the same value to be added.
  14. * Internally, it uses an array, and behaves much like a list.
  15. * <p>
  16. * This class implements {@link java.util.Collection Collection} allowing
  17. * seamless integration with other APIs.
  18. * <p>
  19. * Add, Remove and Clear are supported.
  20. *
  21. * @author Stephen Colebourne
  22. * @author Jason Tiscione
  23. * @version CODE GENERATED
  24. * @since 1.0
  25. */
  26. public class Array${Type}Collection extends Abstract${Type}Collection implements Cloneable {
  27. // This file is CODE GENERATED. Do not change manually.
  28. /** The minimum size allowed when growth occurs */
  29. private static final int MIN_GROWTH_SIZE = 4;
  30. /** The amount the collection grows by when resized (3/2) */
  31. private static final int GROWTH_FACTOR_MULTIPLIER = 3;
  32. /** The amount the collection grows by when resized (3/2) */
  33. private static final int GROWTH_FACTOR_DIVISOR = 2;
  34. /** The array of elements */
  35. private ${type}[] data;
  36. /** The current size */
  37. private int size;
  38. /**
  39. * Constructor.
  40. */
  41. public Array${Type}Collection() {
  42. super();
  43. data = ${Type}Utils.${emptyArray};
  44. }
  45. /**
  46. * Constructor that defines an initial size for the internal storage array.
  47. *
  48. * @param initialSize the initial size of the internal array, negative treated as zero
  49. */
  50. public Array${Type}Collection(int initialSize) {
  51. super();
  52. if (initialSize <= 0) {
  53. data = ${Type}Utils.${emptyArray};
  54. } else {
  55. data = new ${type}[initialSize];
  56. }
  57. }
  58. /**
  59. * Constructor that copies the specified values.
  60. *
  61. * @param values an array of values to copy, null treated as zero size array
  62. */
  63. public Array${Type}Collection(${type}[] values) {
  64. super();
  65. if (values == null) {
  66. data = ${Type}Utils.${emptyArray};
  67. } else {
  68. data = (${type}[]) values.clone();
  69. size = values.length;
  70. }
  71. }
  72. /**
  73. * Constructs a new collection by copying values from another collection.
  74. *
  75. * @param coll a collection of values to copy, null treated as zero size collection
  76. */
  77. public Array${Type}Collection(Collection<?> coll) {
  78. super();
  79. if (coll == null) {
  80. data = ${Type}Utils.${emptyArray};
  81. } else if (coll instanceof ${Type}Collection) {
  82. ${Type}Collection c = (${Type}Collection) coll;
  83. size = c.size();
  84. data = new ${type}[size];
  85. c.to${Type}Array(data, 0);
  86. } else {
  87. data = toPrimitiveArray(coll);
  88. size = coll.size();
  89. }
  90. }
  91. /**
  92. * Constructs a new collection by copying values from an iterator.
  93. *
  94. * @param it an iterator of values to extract, null treated as zero size collection
  95. */
  96. public Array${Type}Collection(Iterator<${object}> it) {
  97. super();
  98. if (it == null) {
  99. data = ${Type}Utils.${emptyArray};
  100. } else if (it instanceof ${Type}Iterator) {
  101. ${Type}Iterator typed = (${Type}Iterator) it;
  102. data = new ${type}[MIN_GROWTH_SIZE];
  103. while (typed.hasNext()) {
  104. add(typed.next${Type}());
  105. }
  106. } else {
  107. data = new ${type}[MIN_GROWTH_SIZE];
  108. while (it.hasNext()) {
  109. add(it.next());
  110. }
  111. }
  112. }
  113. // Implementation
  114. //-----------------------------------------------------------------------
  115. /**
  116. * Gets the current size of the collection.
  117. *
  118. * @return the current size
  119. */
  120. public int size() {
  121. return size;
  122. }
  123. /**
  124. * Gets an iterator over this collection capable of accessing the primitive values.
  125. *
  126. * @return an iterator over this collection
  127. */
  128. public ${Type}Iterator iterator() {
  129. return new PIterator(this);
  130. }
  131. /**
  132. * Adds a primitive value to this collection.
  133. *
  134. * @param value the value to add to this collection
  135. * @return <code>true</code> if this collection was modified by this method call
  136. * @throws IllegalArgumentException if value is rejected by this collection
  137. */
  138. public boolean add(${type} value) {
  139. ensureCapacity(size + 1);
  140. data[size++] = value;
  141. return true;
  142. }
  143. // Overrides
  144. //-----------------------------------------------------------------------
  145. /**
  146. * Optimizes the implementation.
  147. * <p>
  148. * This implementation changes the internal array to be the same size as
  149. * the size of the collection.
  150. */
  151. public void optimize() {
  152. if (size < data.length) {
  153. ${type}[] array = new ${type}[size];
  154. System.arraycopy(data, 0, array, 0, size);
  155. data = array;
  156. }
  157. }
  158. /**
  159. * Are the add methods supported.
  160. *
  161. * @return <code>true</code>
  162. */
  163. protected boolean isAddModifiable() {
  164. return true;
  165. }
  166. /**
  167. * Are the remove methods supported.
  168. *
  169. * @return <code>true</code>
  170. */
  171. protected boolean isRemoveModifiable() {
  172. return true;
  173. }
  174. /**
  175. * Checks whether the object can currently be modified.
  176. *
  177. * @return <code>true</code>
  178. */
  179. public boolean isModifiable() {
  180. return true;
  181. }
  182. /**
  183. * Checks whether this collection contains a specified primitive value.
  184. * <p>
  185. * This implementation uses the internal array directly.
  186. *
  187. * @param value the value to search for
  188. * @return <code>true</code> if the value is found
  189. */
  190. public boolean contains(${type} value) {
  191. for (int i = 0; i < size; i++) {
  192. if (data[i] == value) {
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. /**
  199. * Clears the collection of all elements.
  200. * The collection will have a zero size after this method completes.
  201. * <p>
  202. * This implementation resets the size, but does not reduce the internal storage array.
  203. */
  204. public void clear() {
  205. size = 0;
  206. }
  207. /**
  208. * Adds an array of primitive values to this collection.
  209. *
  210. * @param values the values to add to this collection
  211. * @return <code>true</code> if this collection was modified by this method call
  212. */
  213. public boolean addAll(${type}[] values) {
  214. checkAddModifiable();
  215. if (values == null || values.length == 0) {
  216. return false;
  217. }
  218. return doAdd(0, values);
  219. }
  220. /**
  221. * Adds a collection of primitive values to this collection.
  222. *
  223. * @param values the values to add to this collection, null treated as empty collection
  224. * @return <code>true</code> if this collection was modified by this method call
  225. */
  226. public boolean addAll(${Type}Collection values) {
  227. checkAddModifiable();
  228. if (values == null || values.size() == 0) {
  229. return false;
  230. }
  231. int len = values.size();
  232. ensureCapacity(size + len);
  233. values.to${Type}Array(data, size);
  234. size += len;
  235. return true;
  236. }
  237. #if(${type} != "boolean" && ${type} != "float" && ${type} != "double")
  238. /**
  239. * Adds a range of primitive values to this collection.
  240. * <p>
  241. * The range is defined to be inclusive of the start and end.
  242. * If the start is greater than the end then the range is equivalent to an empty collection.
  243. *
  244. * @param startInclusive the inclusive range start value
  245. * @param endInclusive the inclusive range end value
  246. * @return <code>true</code> if this collection was modified by this method call
  247. * @throws IllegalArgumentException if a value is rejected by this set
  248. * @throws UnsupportedOperationException if not supported by this set
  249. */
  250. public boolean addAll(${type} startInclusive, ${type} endInclusive) {
  251. #if(${type} == "long")
  252. long increaseLong = endInclusive - startInclusive + 1;
  253. if (increaseLong < 0L) {
  254. return false;
  255. }
  256. long newSize = size + increaseLong;
  257. if (newSize > Integer.MAX_VALUE) {
  258. throw new IllegalArgumentException("Range too large");
  259. }
  260. ensureCapacity((int) newSize);
  261. #else
  262. int increase = endInclusive - startInclusive + 1;
  263. if (increase < 0) {
  264. return false;
  265. }
  266. ensureCapacity(size + increase);
  267. #end
  268. ${type} i = startInclusive;
  269. while (i < endInclusive) {
  270. data[size++] = i++;
  271. }
  272. data[size++] = i; // handles endInclusive=MAX_VALUE
  273. return true;
  274. }
  275. #end
  276. /**
  277. * Clone implementation that calls Object clone().
  278. *
  279. * @return the clone
  280. */
  281. public Object clone() {
  282. Array${Type}Collection cloned = (Array${Type}Collection) super.clone();
  283. cloned.data = (${type}[]) data.clone();
  284. return cloned;
  285. }
  286. /**
  287. * Copies data from this collection into the specified array.
  288. * This method is pre-validated.
  289. *
  290. * @param fromIndex the index to start from
  291. * @param dest the destination array
  292. * @param destIndex the destination start index
  293. * @param size the number of items to copy
  294. */
  295. protected void arrayCopy(int fromIndex, ${type}[] dest, int destIndex, int size) {
  296. System.arraycopy(data, fromIndex, dest, destIndex, size);
  297. }
  298. // Internal implementation
  299. //-----------------------------------------------------------------------
  300. /**
  301. * Internal implementation to add to this collection at the specified index.
  302. * This method adjusts the capacity and size.
  303. *
  304. * @param index the index to add at, valid
  305. * @param values the array to add, not null
  306. * @return true if the array was updated
  307. */
  308. protected boolean doAdd(int index, ${type}[] values) {
  309. int len = values.length;
  310. ensureCapacity(size + len);
  311. System.arraycopy(values, 0, data, size, len);
  312. size += len;
  313. return (len > 0);
  314. }
  315. /**
  316. * Internal implementation to remove the element at the specified index.
  317. *
  318. * @param index the index, valid
  319. */
  320. protected void doRemoveIndex(int index) {
  321. System.arraycopy(data, index + 1, data, index, size - 1 - index);
  322. size--;
  323. }
  324. /**
  325. * Internal implementation to ensure that the internal storage array has
  326. * at least the specified size.
  327. *
  328. * @param reqCapacity the amount to expand to
  329. */
  330. protected void ensureCapacity(int reqCapacity) {
  331. int curCapacity = data.length;
  332. if (reqCapacity <= curCapacity) {
  333. return;
  334. }
  335. int newCapacity = curCapacity * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR;
  336. if ((newCapacity - curCapacity) < MIN_GROWTH_SIZE) {
  337. newCapacity = curCapacity + MIN_GROWTH_SIZE;
  338. }
  339. if (newCapacity < reqCapacity) {
  340. newCapacity = reqCapacity;
  341. }
  342. ${type}[] newArray = new ${type}[newCapacity];
  343. System.arraycopy(data, 0, newArray, 0, curCapacity);
  344. data = newArray;
  345. }
  346. // Iterator
  347. //-----------------------------------------------------------------------
  348. /**
  349. * Iterator.
  350. */
  351. protected static class PIterator implements ${Type}Iterator {
  352. private final Array${Type}Collection collection;
  353. private int cursor;
  354. private boolean canRemove;
  355. protected PIterator(Array${Type}Collection coll) {
  356. super();
  357. this.collection = coll;
  358. }
  359. public boolean hasNext() {
  360. return (cursor < collection.size);
  361. }
  362. public ${type} next${Type}() {
  363. if (hasNext() == false) {
  364. throw new NoSuchElementException("No more elements available");
  365. }
  366. canRemove = true;
  367. return collection.data[cursor++];
  368. }
  369. public ${object} next() {
  370. return collection.toObject(next${Type}());
  371. }
  372. public void remove() {
  373. if (canRemove == false) {
  374. throw new IllegalStateException("Element cannot be removed");
  375. }
  376. collection.doRemoveIndex(--cursor);
  377. canRemove = false;
  378. }
  379. public boolean isModifiable() {
  380. return collection.isModifiable();
  381. }
  382. public boolean isResettable() {
  383. return true;
  384. }
  385. public void reset() {
  386. cursor = 0;
  387. }
  388. }
  389. }