Skip to content

Commit dfa9b2b

Browse files
committed
CustomLinkedList implements Collection instead of Iterable
1 parent 7248318 commit dfa9b2b

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

src/main/java/by/andd3dfx/collections/custom/CustomLinkedList.java

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package by.andd3dfx.collections.custom;
22

33
import lombok.AllArgsConstructor;
4+
import org.apache.commons.lang3.NotImplementedException;
45

6+
import java.util.Collection;
57
import java.util.Iterator;
68

79
/**
810
* @see <a href="https://youtu.be/vHjvNHBQP3o">Video solution</a>
911
*/
10-
public class CustomLinkedList<T> implements Iterable<T> {
12+
public class CustomLinkedList<T> implements Collection<T> {
1113

1214
@AllArgsConstructor
1315
public static class Node<M> {
@@ -26,8 +28,10 @@ public String toString() {
2628
private Node<T> head;
2729
private int size = 0;
2830

29-
public void add(T value) {
31+
@Override
32+
public boolean add(T value) {
3033
add(size, value);
34+
return true;
3135
}
3236

3337
public void add(int index, T value) {
@@ -53,14 +57,37 @@ public void add(int index, T value) {
5357
size++;
5458
}
5559

60+
@Override
5661
public int size() {
5762
return size;
5863
}
5964

65+
@Override
6066
public boolean isEmpty() {
6167
return size == 0;
6268
}
6369

70+
@Override
71+
public boolean contains(Object o) {
72+
var curr = head;
73+
if (o == null) {
74+
while (curr != null) {
75+
if (curr.value == null) {
76+
return true;
77+
}
78+
curr = curr.next;
79+
}
80+
} else {
81+
while (curr != null) {
82+
if (o.equals(curr.value)) {
83+
return true;
84+
}
85+
curr = curr.next;
86+
}
87+
}
88+
return false;
89+
}
90+
6491
public T get(int index) {
6592
if (index < 0 || index >= size) {
6693
throw new IndexOutOfBoundsException(String.format("Wrong index: %d", index));
@@ -120,7 +147,8 @@ public T remove(int index) {
120147
return curr.value;
121148
}
122149

123-
public boolean remove(T value) {
150+
@Override
151+
public boolean remove(Object value) {
124152
var i = 0;
125153
var curr = head;
126154
while (curr != null) {
@@ -134,7 +162,35 @@ public boolean remove(T value) {
134162
return false;
135163
}
136164

137-
private boolean checkEquality(T value1, T value2) {
165+
@Override
166+
public boolean containsAll(Collection<?> c) {
167+
for (var element : c) {
168+
if (!contains(element)) {
169+
return false;
170+
}
171+
}
172+
return true;
173+
}
174+
175+
@Override
176+
public boolean addAll(Collection<? extends T> c) {
177+
// TODO add implementation
178+
throw new UnsupportedOperationException();
179+
}
180+
181+
@Override
182+
public boolean removeAll(Collection<?> c) {
183+
// TODO add implementation
184+
throw new UnsupportedOperationException();
185+
}
186+
187+
@Override
188+
public boolean retainAll(Collection<?> c) {
189+
// TODO add implementation
190+
throw new UnsupportedOperationException();
191+
}
192+
193+
private boolean checkEquality(Object value1, Object value2) {
138194
if (value1 == null) {
139195
return value2 == null;
140196
}
@@ -162,6 +218,7 @@ public void reverse() {
162218
head = prev;
163219
}
164220

221+
@Override
165222
public void clear() {
166223
head = null;
167224
size = 0;
@@ -172,6 +229,18 @@ public Iterator<T> iterator() {
172229
return new CustomIterator<>(head);
173230
}
174231

232+
@Override
233+
public Object[] toArray() {
234+
// TODO add implementation
235+
throw new NotImplementedException();
236+
}
237+
238+
@Override
239+
public <T1> T1[] toArray(T1[] a) {
240+
// TODO add implementation
241+
throw new NotImplementedException();
242+
}
243+
175244
@AllArgsConstructor
176245
public static class CustomIterator<E> implements Iterator<E> {
177246
private Node<E> curr;

src/test/java/by/andd3dfx/collections/custom/CustomLinkedListTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Test;
44

55
import java.util.ArrayList;
6+
import java.util.Arrays;
67
import java.util.List;
78

89
import static org.assertj.core.api.Assertions.assertThat;
@@ -18,8 +19,8 @@ public void addNGet() {
1819
assertThat(linkedList.size()).isEqualTo(0);
1920
assertTrue(linkedList.isEmpty());
2021

21-
linkedList.add(3);
22-
linkedList.add(7);
22+
assertTrue(linkedList.add(3));
23+
assertTrue(linkedList.add(7));
2324

2425
assertThat(linkedList.size()).isEqualTo(2);
2526
assertFalse(linkedList.isEmpty());
@@ -340,4 +341,32 @@ public void testToString() {
340341

341342
assertThat(result).isEqualTo("CustomLinkedList{head={3, next={7, next={12, next={34}}}}}");
342343
}
344+
345+
@Test
346+
public void contains() {
347+
var linkedList = new CustomLinkedList<>();
348+
linkedList.add(3);
349+
linkedList.add(7);
350+
assertThat(linkedList.contains(null)).isFalse();
351+
352+
linkedList.add(null);
353+
linkedList.add(12);
354+
assertThat(linkedList.contains(3)).isTrue();
355+
assertThat(linkedList.contains(99)).isFalse();
356+
assertThat(linkedList.contains(12)).isTrue();
357+
assertThat(linkedList.contains(null)).isTrue();
358+
}
359+
360+
@Test
361+
public void containsAll() {
362+
var linkedList = new CustomLinkedList<>();
363+
linkedList.add(3);
364+
linkedList.add(7);
365+
linkedList.add(null);
366+
linkedList.add(99);
367+
368+
assertThat(linkedList.containsAll(List.of(3, 99))).isTrue();
369+
assertThat(linkedList.containsAll(Arrays.asList(7, null, 99))).isTrue();
370+
assertThat(linkedList.containsAll(List.of(7, 12))).isFalse();
371+
}
343372
}

0 commit comments

Comments
 (0)