modifying ListOfThree class
This commit is contained in:
parent
5a1a468c78
commit
b43787bca5
@ -1,12 +1,21 @@
|
|||||||
package com.baeldung.accessmodifiers.publicmodifier;
|
package com.baeldung.accessmodifiers.publicmodifier;
|
||||||
|
|
||||||
import java.util.AbstractList;
|
import java.util.AbstractList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class ListOfThree<E> extends AbstractList<E> {
|
public class ListOfThree<E> extends AbstractList<E> {
|
||||||
|
|
||||||
private static final int MAX_LENGTH = 3;
|
private static final int LENGTH = 3;
|
||||||
private int size;
|
private Object[] elements;
|
||||||
private Object[] elements = new Object[MAX_LENGTH];
|
|
||||||
|
public ListOfThree(E[] data) {
|
||||||
|
if(data == null
|
||||||
|
|| data.length != LENGTH)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
|
||||||
|
this.elements = Arrays.copyOf(data, data.length); //shallow copy
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -14,18 +23,9 @@ public class ListOfThree<E> extends AbstractList<E> {
|
|||||||
return (E)elements[index];
|
return (E)elements[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean add(E e) {
|
|
||||||
|
|
||||||
elements[size] = e;
|
|
||||||
size++;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return size;
|
return LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -48,31 +48,21 @@ public class PublicAccessModifierUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
|
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
|
||||||
|
|
||||||
List<String> list1 = new ListOfThree<String>();
|
String[] dataSet1 = new String[] {"zero", "one", "two"};
|
||||||
list1.add("zero"); //inherited implementation
|
|
||||||
list1.add("one");
|
List<String> list1 = new ListOfThree<String>(dataSet1);
|
||||||
list1.add("two");
|
|
||||||
|
|
||||||
//our implemented methods
|
//our implemented methods
|
||||||
assertEquals("zero", list1.get(0));
|
assertEquals("one", list1.get(1));
|
||||||
|
|
||||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
|
|
||||||
list1.get(4);
|
|
||||||
});
|
|
||||||
|
|
||||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
|
|
||||||
list1.add("three");
|
|
||||||
});
|
|
||||||
|
|
||||||
assertEquals(3, list1.size());
|
assertEquals(3, list1.size());
|
||||||
|
|
||||||
list1.indexOf("one"); //inherited implementation
|
//inherited implementations
|
||||||
|
assertEquals(1, list1.indexOf("one"));
|
||||||
|
|
||||||
List<String> list2 = new ListOfThree<String>();
|
String[] dataSet2 = new String[] {"two", "zero", "one"};
|
||||||
list2.add("zero");
|
List<String> list2 = new ListOfThree<String>(dataSet2);
|
||||||
list2.add("one");
|
|
||||||
|
|
||||||
assertTrue(list1.containsAll(list2)); //inherited implementation
|
assertTrue(list1.containsAll(list2));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user