Skip to content

Commit 5c181c4

Browse files
authored
Merge pull request eugenp#3739 from nguyennamthai/BAEL-1537
Second commit for TDD List implementation
2 parents 07111d1 + c7e0852 commit 5c181c4

2 files changed

Lines changed: 104 additions & 109 deletions

File tree

core-java/src/main/java/com/baeldung/java/list/CustomList.java

Lines changed: 51 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,94 @@ public class CustomList<E> implements List<E> {
1010
private Object[] internal = {};
1111

1212
@Override
13-
public void add(int index, E element) {
14-
throw new UnsupportedOperationException();
13+
public boolean isEmpty() {
14+
// the first cycle
15+
// return true;
16+
17+
// the second cycle
18+
// if (internal.length != 0) {
19+
// return false;
20+
// } else {
21+
// return true;
22+
// }
23+
24+
// refactoring
25+
return internal.length == 0;
1526
}
1627

1728
@Override
18-
public boolean addAll(Collection<? extends E> collection) {
19-
throw new UnsupportedOperationException();
29+
public int size() {
30+
// the first cycle
31+
// if (isEmpty()) {
32+
// return 0;
33+
// } else {
34+
// return internal.length;
35+
// }
36+
37+
// refactoring
38+
return internal.length;
2039
}
2140

41+
@SuppressWarnings("unchecked")
2242
@Override
23-
public boolean addAll(int index, Collection<? extends E> collection) {
24-
throw new UnsupportedOperationException();
43+
public E get(int index) {
44+
// the first cycle
45+
// return (E) internal[0];
46+
47+
// improvement
48+
return (E) internal[index];
2549
}
2650

2751
@Override
28-
public E remove(int index) {
29-
throw new UnsupportedOperationException();
52+
public boolean add(E element) {
53+
// the first cycle
54+
// internal = new Object[] { element };
55+
// return true;
56+
57+
// the second cycle
58+
Object[] temp = Arrays.copyOf(internal, internal.length + 1);
59+
temp[internal.length] = element;
60+
internal = temp;
61+
return true;
3062
}
3163

3264
@Override
33-
public boolean remove(Object object) {
65+
public void add(int index, E element) {
3466
throw new UnsupportedOperationException();
3567
}
3668

3769
@Override
38-
public boolean removeAll(Collection<?> collection) {
70+
public boolean addAll(Collection<? extends E> collection) {
3971
throw new UnsupportedOperationException();
4072
}
4173

4274
@Override
43-
public boolean retainAll(Collection<?> collection) {
75+
public boolean addAll(int index, Collection<? extends E> collection) {
4476
throw new UnsupportedOperationException();
4577
}
4678

4779
@Override
48-
public int size() {
49-
return internal.length;
80+
public E remove(int index) {
81+
throw new UnsupportedOperationException();
5082
}
5183

5284
@Override
53-
public boolean isEmpty() {
54-
return internal.length == 0;
85+
public boolean remove(Object object) {
86+
throw new UnsupportedOperationException();
5587
}
5688

5789
@Override
58-
public boolean add(E element) {
59-
// the first cycle
60-
// internal = new Object[1];
61-
// internal[0] = element;
62-
// return true;
63-
64-
Object[] temp = new Object[internal.length + 1];
65-
System.arraycopy(internal, 0, temp, 0, internal.length);
66-
temp[internal.length] = element;
67-
internal = temp;
68-
return true;
90+
public boolean removeAll(Collection<?> collection) {
91+
throw new UnsupportedOperationException();
6992
}
7093

71-
@SuppressWarnings("unchecked")
7294
@Override
73-
public E get(int index) {
74-
return (E) internal[index];
95+
public boolean retainAll(Collection<?> collection) {
96+
throw new UnsupportedOperationException();
7597
}
7698

7799
@Override
78100
public boolean contains(Object object) {
79-
// return false
80-
81101
for (Object element : internal) {
82102
if (object.equals(element)) {
83103
return true;
@@ -88,14 +108,6 @@ public boolean contains(Object object) {
88108

89109
@Override
90110
public boolean containsAll(Collection<?> collection) {
91-
// the first cycle
92-
// for (Object element : collection) {
93-
// if (element.equals(internal[0])) {
94-
// return true;
95-
// }
96-
// }
97-
// return false;
98-
99111
for (Object element : collection)
100112
if (!contains(element)) {
101113
return false;
@@ -118,12 +130,6 @@ public void clear() {
118130

119131
@Override
120132
public int indexOf(Object object) {
121-
// the first cycle
122-
// if (object.equals(internal[0])) {
123-
// return 0;
124-
// }
125-
// return -1;
126-
127133
for (int i = 0; i < internal.length; i++) {
128134
if (object.equals(internal[i])) {
129135
return i;
@@ -134,12 +140,6 @@ public int indexOf(Object object) {
134140

135141
@Override
136142
public int lastIndexOf(Object object) {
137-
// the first cycle
138-
// if (object.equals(internal[0])) {
139-
// return 0;
140-
// }
141-
// return -1;
142-
143143
for (int i = internal.length - 1; i >= 0; i--) {
144144
if (object.equals(internal[i])) {
145145
return i;
@@ -151,9 +151,6 @@ public int lastIndexOf(Object object) {
151151
@SuppressWarnings("unchecked")
152152
@Override
153153
public List<E> subList(int fromIndex, int toIndex) {
154-
// the first cycle
155-
// return (List<E>) Arrays.asList(internal);
156-
157154
Object[] temp = new Object[toIndex - fromIndex];
158155
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
159156
return (List<E>) Arrays.asList(temp);
@@ -167,16 +164,6 @@ public Object[] toArray() {
167164
@SuppressWarnings("unchecked")
168165
@Override
169166
public <T> T[] toArray(T[] array) {
170-
// the first cycle
171-
// array[0] = (T) internal[0];
172-
// return array;
173-
174-
// the second cycle
175-
// if (array.length < internal.length) {
176-
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
177-
// }
178-
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
179-
180167
if (array.length < internal.length) {
181168
return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
182169
}
@@ -209,18 +196,12 @@ private class CustomIterator implements Iterator<E> {
209196

210197
@Override
211198
public boolean hasNext() {
212-
// the first cycle
213-
// return true;
214-
215199
return index != internal.length;
216200
}
217201

218202
@SuppressWarnings("unchecked")
219203
@Override
220204
public E next() {
221-
// the first cycle
222-
// return (E) CustomList.this.internal[0];
223-
224205
E element = (E) CustomList.this.internal[index];
225206
index++;
226207
return element;

core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,58 @@
1414
import org.junit.Test;
1515

1616
public class CustomListUnitTest {
17+
@Test
18+
public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() {
19+
List<Object> list = new CustomList<>();
20+
21+
assertTrue(list.isEmpty());
22+
}
23+
24+
@Test
25+
public void givenNonEmptyList_whenIsEmpty_thenFalseIsReturned() {
26+
List<Object> list = new CustomList<>();
27+
list.add(null);
28+
29+
assertFalse(list.isEmpty());
30+
}
31+
32+
@Test
33+
public void givenListWithAnElement_whenSize_thenOneIsReturned() {
34+
List<Object> list = new CustomList<>();
35+
list.add(null);
36+
37+
assertEquals(1, list.size());
38+
}
39+
40+
@Test
41+
public void givenListWithAnElement_whenGet_thenThatElementIsReturned() {
42+
List<Object> list = new CustomList<>();
43+
list.add("baeldung");
44+
Object element = list.get(0);
45+
46+
assertEquals("baeldung", element);
47+
}
48+
49+
@Test
50+
public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() {
51+
List<Object> list = new CustomList<>();
52+
boolean succeeded = list.add(null);
53+
54+
assertTrue(succeeded);
55+
}
56+
57+
@Test
58+
public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() {
59+
List<Object> list = new CustomList<>();
60+
list.add("baeldung");
61+
list.add(".com");
62+
Object element1 = list.get(0);
63+
Object element2 = list.get(1);
64+
65+
assertEquals("baeldung", element1);
66+
assertEquals(".com", element2);
67+
}
68+
1769
@Test(expected = UnsupportedOperationException.class)
1870
public void whenAddToSpecifiedIndex_thenExceptionIsThrown() {
1971
new CustomList<>().add(0, null);
@@ -64,44 +116,6 @@ public void whenRetainAll_thenExceptionIsThrown() {
64116
list.retainAll(collection);
65117
}
66118

67-
@Test
68-
public void givenEmptyList_whenSize_thenZeroIsReturned() {
69-
List<Object> list = new CustomList<>();
70-
71-
assertEquals(0, list.size());
72-
}
73-
74-
@Test
75-
public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() {
76-
List<Object> list = new CustomList<>();
77-
78-
assertTrue(list.isEmpty());
79-
}
80-
81-
@Test
82-
public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() {
83-
List<Object> list = new CustomList<>();
84-
boolean succeeded = list.add("baeldung");
85-
Object element = list.get(0);
86-
87-
assertTrue(succeeded);
88-
assertEquals("baeldung", element);
89-
}
90-
91-
@Test
92-
public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() {
93-
List<Object> list = new CustomList<>();
94-
boolean succeeded1 = list.add("baeldung");
95-
boolean succeeded2 = list.add(".com");
96-
Object element1 = list.get(0);
97-
Object element2 = list.get(1);
98-
99-
assertTrue(succeeded1);
100-
assertTrue(succeeded2);
101-
assertEquals("baeldung", element1);
102-
assertEquals(".com", element2);
103-
}
104-
105119
@Test
106120
public void givenEmptyList_whenContains_thenFalseIsReturned() {
107121
List<Object> list = new CustomList<>();
@@ -271,7 +285,7 @@ public void givenListWithOneElement_whenIterator_thenThisElementIsNext() {
271285
}
272286

273287
@Test
274-
public void whenIteratorNextIsCalledTwice_thenTheSecondReturnsFalse() {
288+
public void whenIteratorHasNextIsCalledTwice_thenTheSecondReturnsFalse() {
275289
List<Object> list = new CustomList<>();
276290
list.add("baeldung");
277291
Iterator<Object> iterator = list.iterator();

0 commit comments

Comments
 (0)