|
| 1 | +package com.testspace.java.list; |
| 2 | + |
| 3 | +/** |
| 4 | + * A class to provide a simple list. |
| 5 | + * List resizes automatically. Used to illustrate |
| 6 | + * various design and implementation details of |
| 7 | + * a class in Java. |
| 8 | + * |
| 9 | + * @author scottm |
| 10 | + * |
| 11 | + */ |
| 12 | +public class GenericList |
| 13 | +{ |
| 14 | + // Default capacity |
| 15 | + private static final int DEFAULT_CAP = 10; |
| 16 | + |
| 17 | + // iValues store the elements of the list and may have extra capacity |
| 18 | + private Object[] iValues; |
| 19 | + private int iSize; |
| 20 | + |
| 21 | + /** |
| 22 | + * Creates an empty list. |
| 23 | + */ |
| 24 | + public GenericList() |
| 25 | + { |
| 26 | + //redirect to single int constructor |
| 27 | + this(DEFAULT_CAP); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Creates an empty list. Specify initial capacity. |
| 32 | + * @param initialCap > 0 |
| 33 | + */ |
| 34 | + public GenericList(int initialCap) |
| 35 | + { |
| 36 | + if (initialCap <= 0) |
| 37 | + { |
| 38 | + throw new IllegalArgumentException("Must be a positive integer"); |
| 39 | + } |
| 40 | + iValues = new Object[initialCap]; |
| 41 | + iSize = 0; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get list size. |
| 46 | + * @return size of list. |
| 47 | + */ |
| 48 | + public int size() |
| 49 | + { |
| 50 | + return iSize; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get list element. |
| 55 | + * @param pos of the element in the list. |
| 56 | + * @return the list element. |
| 57 | + */ |
| 58 | + public Object get(int pos) |
| 59 | + { |
| 60 | + return iValues[pos]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Add element to the end of the list. |
| 65 | + * Size of the list goes up by 1. |
| 66 | + * @param element value to add. |
| 67 | + */ |
| 68 | + public void add(Object element) |
| 69 | + { |
| 70 | + insert(iSize, element); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Insert element at position. |
| 75 | + * @param pos of the element. |
| 76 | + * @param element to add. |
| 77 | + */ |
| 78 | + public void insert(int pos, Object element) |
| 79 | + { |
| 80 | + ensureCapcity(); |
| 81 | + for(int i = iSize; i > pos; i--) |
| 82 | + { |
| 83 | + iValues[i] = iValues[i - 1]; |
| 84 | + } |
| 85 | + iValues[pos] = element; |
| 86 | + iSize++; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Remove element at position. |
| 91 | + * @param pos of the element to remove. |
| 92 | + * @return removed element. |
| 93 | + */ |
| 94 | + public Object remove(int pos) |
| 95 | + { |
| 96 | + Object removedElement = iValues[pos]; |
| 97 | + for(int i = pos; i < iSize - 1; i++) |
| 98 | + { |
| 99 | + iValues[i] = iValues[i + 1]; |
| 100 | + } |
| 101 | + iValues[iSize - 1] = null; |
| 102 | + iSize--; |
| 103 | + return removedElement; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Return a String representation of list. |
| 108 | + * Size and elements included. |
| 109 | + * @return string representation of element. |
| 110 | + */ |
| 111 | + public String toString() |
| 112 | + { |
| 113 | + String result = "size: " + iSize + ", elements: ["; |
| 114 | + for(int i = 0; i < iSize - 1; i++) |
| 115 | + { |
| 116 | + result += iValues[i].toString() + ", "; |
| 117 | + } |
| 118 | + if(iSize > 0 ) |
| 119 | + { |
| 120 | + result += iValues[iSize - 1]; |
| 121 | + } |
| 122 | + result += "]"; |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Return a String representation of list. |
| 128 | + * Size and elements included. |
| 129 | + * @return string representation of element. |
| 130 | + */ |
| 131 | + public String toStringUsingStringBuffer() |
| 132 | + { |
| 133 | + StringBuffer result = new StringBuffer(); |
| 134 | + result.append( "size: " ); |
| 135 | + result.append( iSize ); |
| 136 | + result.append(", elements: ["); |
| 137 | + for(int i = 0; i < iSize - 1; i++) |
| 138 | + { |
| 139 | + result.append(iValues[i]); |
| 140 | + result.append(", "); |
| 141 | + } |
| 142 | + if( iSize > 0 ) |
| 143 | + { |
| 144 | + result.append(iValues[iSize - 1]); |
| 145 | + } |
| 146 | + result.append("]"); |
| 147 | + return result.toString(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Check if this list is equal to another. |
| 152 | + * @param other The object to comapre to. |
| 153 | + * @return true if other is a non null, IntList object |
| 154 | + * that is the same size as this IntList and has the |
| 155 | + * same elements in the same order, false otherwise. |
| 156 | + */ |
| 157 | + public boolean equals(Object other) |
| 158 | + { |
| 159 | + boolean result; |
| 160 | + if(other == null) |
| 161 | + { |
| 162 | + // we know this is not null so can't be equal |
| 163 | + result = false; |
| 164 | + } |
| 165 | + else if(this == other) |
| 166 | + { |
| 167 | + // quick check if this and other refer to same IntList object |
| 168 | + result = true; |
| 169 | + } |
| 170 | + else if(this.getClass() != other.getClass()) |
| 171 | + { |
| 172 | + // other is not an IntList they can't be equal |
| 173 | + result = false; |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + // other is not null and refers to an IntList |
| 178 | + GenericList otherList = (GenericList)other; |
| 179 | + result = this.size() == otherList.size(); |
| 180 | + int i = 0; |
| 181 | + while(i < iSize && result) |
| 182 | + { |
| 183 | + result = this.iValues[i].equals( otherList.iValues[i] ); |
| 184 | + i++; |
| 185 | + } |
| 186 | + } |
| 187 | + return result; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Resize internal storage container by a factor of 2, if needed. |
| 192 | + */ |
| 193 | + private void ensureCapcity() |
| 194 | + { |
| 195 | + if(iSize == iValues.length) |
| 196 | + { |
| 197 | + Object[] temp = new Object[iValues.length * 2]; |
| 198 | + System.arraycopy(iValues, 0, temp, 0, iValues.length); |
| 199 | + iValues = temp; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments