make IntList cloneable

This commit is contained in:
2017-09-28 20:25:56 +02:00
parent a178086dfa
commit 235565bfe4
2 changed files with 26 additions and 4 deletions

View File

@@ -10,10 +10,8 @@ import java.util.List;
* This class does not (yet) implements all methods a java.util {@link List} * This class does not (yet) implements all methods a java.util {@link List}
* would have. * would have.
*/ */
public class IntList implements Serializable { public class IntList implements Serializable, Cloneable {
// TODO make Serializable
// TODO make Cloneable
// TODO support Iterator // TODO support Iterator
// TODO add mod counts // TODO add mod counts
// TODO support sublists // TODO support sublists
@@ -28,7 +26,7 @@ public class IntList implements Serializable {
// TODO add toArray(int[] a) // TODO add toArray(int[] a)
// TODO remove bounds checks that are handled by java, e.g. negative indices // TODO remove bounds checks that are handled by java, e.g. negative indices
private static final long serialVersionUID = 2588449582065895141L; private static final long serialVersionUID = -6823520157007564746L;
private static final int DEFAULT_CAPACITY = 10; private static final int DEFAULT_CAPACITY = 10;
@@ -399,4 +397,15 @@ public class IntList implements Serializable {
data = local_data; data = local_data;
} }
} }
@Override
public IntList clone() {
try {
final IntList result = (IntList) super.clone();
result.data = Arrays.copyOf(data, index);
return result;
} catch (final CloneNotSupportedException e) {
throw new IllegalStateException(e);
}
}
} }

View File

@@ -455,4 +455,17 @@ public class IntListTest {
list.size(), actual.getCapacity()); list.size(), actual.getCapacity());
} }
} }
@Test
public void testClone() {
final IntList list = new IntList();
list.addAll(1, 2);
final IntList clone = list.clone();
Assert.assertEquals(list, clone);
list.set(1, 0);
Assert.assertNotEquals(list, clone);
}
} }