Merge pull request #3739 from nguyennamthai/BAEL-1537
Second commit for TDD List implementation
This commit is contained in:
commit
5c181c4b72
@ -9,6 +9,58 @@ import java.util.ListIterator;
|
|||||||
public class CustomList<E> implements List<E> {
|
public class CustomList<E> implements List<E> {
|
||||||
private Object[] internal = {};
|
private Object[] internal = {};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
// the first cycle
|
||||||
|
// return true;
|
||||||
|
|
||||||
|
// the second cycle
|
||||||
|
// if (internal.length != 0) {
|
||||||
|
// return false;
|
||||||
|
// } else {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// refactoring
|
||||||
|
return internal.length == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
// the first cycle
|
||||||
|
// if (isEmpty()) {
|
||||||
|
// return 0;
|
||||||
|
// } else {
|
||||||
|
// return internal.length;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// refactoring
|
||||||
|
return internal.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public E get(int index) {
|
||||||
|
// the first cycle
|
||||||
|
// return (E) internal[0];
|
||||||
|
|
||||||
|
// improvement
|
||||||
|
return (E) internal[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean add(E element) {
|
||||||
|
// the first cycle
|
||||||
|
// internal = new Object[] { element };
|
||||||
|
// return true;
|
||||||
|
|
||||||
|
// the second cycle
|
||||||
|
Object[] temp = Arrays.copyOf(internal, internal.length + 1);
|
||||||
|
temp[internal.length] = element;
|
||||||
|
internal = temp;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(int index, E element) {
|
public void add(int index, E element) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
@ -44,40 +96,8 @@ public class CustomList<E> implements List<E> {
|
|||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
return internal.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return internal.length == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean add(E element) {
|
|
||||||
// the first cycle
|
|
||||||
// internal = new Object[1];
|
|
||||||
// internal[0] = element;
|
|
||||||
// return true;
|
|
||||||
|
|
||||||
Object[] temp = new Object[internal.length + 1];
|
|
||||||
System.arraycopy(internal, 0, temp, 0, internal.length);
|
|
||||||
temp[internal.length] = element;
|
|
||||||
internal = temp;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public E get(int index) {
|
|
||||||
return (E) internal[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(Object object) {
|
public boolean contains(Object object) {
|
||||||
// return false
|
|
||||||
|
|
||||||
for (Object element : internal) {
|
for (Object element : internal) {
|
||||||
if (object.equals(element)) {
|
if (object.equals(element)) {
|
||||||
return true;
|
return true;
|
||||||
@ -88,14 +108,6 @@ public class CustomList<E> implements List<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsAll(Collection<?> collection) {
|
public boolean containsAll(Collection<?> collection) {
|
||||||
// the first cycle
|
|
||||||
// for (Object element : collection) {
|
|
||||||
// if (element.equals(internal[0])) {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return false;
|
|
||||||
|
|
||||||
for (Object element : collection)
|
for (Object element : collection)
|
||||||
if (!contains(element)) {
|
if (!contains(element)) {
|
||||||
return false;
|
return false;
|
||||||
@ -118,12 +130,6 @@ public class CustomList<E> implements List<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int indexOf(Object object) {
|
public int indexOf(Object object) {
|
||||||
// the first cycle
|
|
||||||
// if (object.equals(internal[0])) {
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
// return -1;
|
|
||||||
|
|
||||||
for (int i = 0; i < internal.length; i++) {
|
for (int i = 0; i < internal.length; i++) {
|
||||||
if (object.equals(internal[i])) {
|
if (object.equals(internal[i])) {
|
||||||
return i;
|
return i;
|
||||||
@ -134,12 +140,6 @@ public class CustomList<E> implements List<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int lastIndexOf(Object object) {
|
public int lastIndexOf(Object object) {
|
||||||
// the first cycle
|
|
||||||
// if (object.equals(internal[0])) {
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
// return -1;
|
|
||||||
|
|
||||||
for (int i = internal.length - 1; i >= 0; i--) {
|
for (int i = internal.length - 1; i >= 0; i--) {
|
||||||
if (object.equals(internal[i])) {
|
if (object.equals(internal[i])) {
|
||||||
return i;
|
return i;
|
||||||
@ -151,9 +151,6 @@ public class CustomList<E> implements List<E> {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public List<E> subList(int fromIndex, int toIndex) {
|
public List<E> subList(int fromIndex, int toIndex) {
|
||||||
// the first cycle
|
|
||||||
// return (List<E>) Arrays.asList(internal);
|
|
||||||
|
|
||||||
Object[] temp = new Object[toIndex - fromIndex];
|
Object[] temp = new Object[toIndex - fromIndex];
|
||||||
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
|
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
|
||||||
return (List<E>) Arrays.asList(temp);
|
return (List<E>) Arrays.asList(temp);
|
||||||
@ -167,16 +164,6 @@ public class CustomList<E> implements List<E> {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public <T> T[] toArray(T[] array) {
|
public <T> T[] toArray(T[] array) {
|
||||||
// the first cycle
|
|
||||||
// array[0] = (T) internal[0];
|
|
||||||
// return array;
|
|
||||||
|
|
||||||
// the second cycle
|
|
||||||
// if (array.length < internal.length) {
|
|
||||||
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
|
|
||||||
// }
|
|
||||||
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
|
|
||||||
|
|
||||||
if (array.length < internal.length) {
|
if (array.length < internal.length) {
|
||||||
return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
|
return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
|
||||||
}
|
}
|
||||||
@ -209,18 +196,12 @@ public class CustomList<E> implements List<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
// the first cycle
|
|
||||||
// return true;
|
|
||||||
|
|
||||||
return index != internal.length;
|
return index != internal.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public E next() {
|
public E next() {
|
||||||
// the first cycle
|
|
||||||
// return (E) CustomList.this.internal[0];
|
|
||||||
|
|
||||||
E element = (E) CustomList.this.internal[index];
|
E element = (E) CustomList.this.internal[index];
|
||||||
index++;
|
index++;
|
||||||
return element;
|
return element;
|
||||||
|
@ -14,6 +14,58 @@ import java.util.List;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class CustomListUnitTest {
|
public class CustomListUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() {
|
||||||
|
List<Object> list = new CustomList<>();
|
||||||
|
|
||||||
|
assertTrue(list.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonEmptyList_whenIsEmpty_thenFalseIsReturned() {
|
||||||
|
List<Object> list = new CustomList<>();
|
||||||
|
list.add(null);
|
||||||
|
|
||||||
|
assertFalse(list.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListWithAnElement_whenSize_thenOneIsReturned() {
|
||||||
|
List<Object> list = new CustomList<>();
|
||||||
|
list.add(null);
|
||||||
|
|
||||||
|
assertEquals(1, list.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListWithAnElement_whenGet_thenThatElementIsReturned() {
|
||||||
|
List<Object> list = new CustomList<>();
|
||||||
|
list.add("baeldung");
|
||||||
|
Object element = list.get(0);
|
||||||
|
|
||||||
|
assertEquals("baeldung", element);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() {
|
||||||
|
List<Object> list = new CustomList<>();
|
||||||
|
boolean succeeded = list.add(null);
|
||||||
|
|
||||||
|
assertTrue(succeeded);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() {
|
||||||
|
List<Object> list = new CustomList<>();
|
||||||
|
list.add("baeldung");
|
||||||
|
list.add(".com");
|
||||||
|
Object element1 = list.get(0);
|
||||||
|
Object element2 = list.get(1);
|
||||||
|
|
||||||
|
assertEquals("baeldung", element1);
|
||||||
|
assertEquals(".com", element2);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
public void whenAddToSpecifiedIndex_thenExceptionIsThrown() {
|
public void whenAddToSpecifiedIndex_thenExceptionIsThrown() {
|
||||||
new CustomList<>().add(0, null);
|
new CustomList<>().add(0, null);
|
||||||
@ -64,44 +116,6 @@ public class CustomListUnitTest {
|
|||||||
list.retainAll(collection);
|
list.retainAll(collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenEmptyList_whenSize_thenZeroIsReturned() {
|
|
||||||
List<Object> list = new CustomList<>();
|
|
||||||
|
|
||||||
assertEquals(0, list.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() {
|
|
||||||
List<Object> list = new CustomList<>();
|
|
||||||
|
|
||||||
assertTrue(list.isEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() {
|
|
||||||
List<Object> list = new CustomList<>();
|
|
||||||
boolean succeeded = list.add("baeldung");
|
|
||||||
Object element = list.get(0);
|
|
||||||
|
|
||||||
assertTrue(succeeded);
|
|
||||||
assertEquals("baeldung", element);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() {
|
|
||||||
List<Object> list = new CustomList<>();
|
|
||||||
boolean succeeded1 = list.add("baeldung");
|
|
||||||
boolean succeeded2 = list.add(".com");
|
|
||||||
Object element1 = list.get(0);
|
|
||||||
Object element2 = list.get(1);
|
|
||||||
|
|
||||||
assertTrue(succeeded1);
|
|
||||||
assertTrue(succeeded2);
|
|
||||||
assertEquals("baeldung", element1);
|
|
||||||
assertEquals(".com", element2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmptyList_whenContains_thenFalseIsReturned() {
|
public void givenEmptyList_whenContains_thenFalseIsReturned() {
|
||||||
List<Object> list = new CustomList<>();
|
List<Object> list = new CustomList<>();
|
||||||
@ -271,7 +285,7 @@ public class CustomListUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenIteratorNextIsCalledTwice_thenTheSecondReturnsFalse() {
|
public void whenIteratorHasNextIsCalledTwice_thenTheSecondReturnsFalse() {
|
||||||
List<Object> list = new CustomList<>();
|
List<Object> list = new CustomList<>();
|
||||||
list.add("baeldung");
|
list.add("baeldung");
|
||||||
Iterator<Object> iterator = list.iterator();
|
Iterator<Object> iterator = list.iterator();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user