diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomCollection.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomCollection.java deleted file mode 100644 index ccdf325105..0000000000 --- a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomCollection.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung.collections.iterable; - -import java.util.Arrays; -import java.util.ConcurrentModificationException; -import java.util.Iterator; -import java.util.NoSuchElementException; - -public class CustomCollection implements Iterable { - - private E[] elementData; - private int size; - - public CustomCollection() { - this.elementData = (E[]) new Object[] {}; - } - - public void add(E element) { - ensureCapacity(size + 1); - elementData[size++] = element; - } - - private void ensureCapacity(int minCapacity) { - int oldCapacity = elementData.length; - int newCapacity = oldCapacity + (oldCapacity >> 1); - if (newCapacity - minCapacity < 0) { - newCapacity = minCapacity; - } - elementData = Arrays.copyOf(elementData, newCapacity); - } - - @Override - public Iterator iterator() { - return new CollectionIterator(); - } - - public class CollectionIterator implements Iterator { - int cursor; - int lastReturned = -1; - - public boolean hasNext() { - return cursor != size; - } - - public E next() { - return getNextElement(); - } - - private E getNextElement() { - int current = cursor; - exist(current); - - E[] elements = CustomCollection.this.elementData; - validate(elements, current); - - cursor = current + 1; - lastReturned = current; - return elements[lastReturned]; - } - - private void exist(int current) { - if (current >= size) { - throw new NoSuchElementException(); - } - } - - private void validate(E[] elements, int current) { - if (current >= elements.length) { - throw new ConcurrentModificationException(); - } - } - } -} diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java index e89e775a6d..c950becd96 100644 --- a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java @@ -4,13 +4,13 @@ class CustomIterableClient { public static void main(String[] args) { - CustomCollection products = new CustomCollection<>(); - products.add(new Product("Tuna", 42)); - products.add(new Product("Eggplant", 65)); - products.add(new Product("Salad", 45)); - products.add(new Product("Banana", 29)); + ShoppingCart shoppingCart = new ShoppingCart(); + shoppingCart.add(new Product("Tuna", 42)); + shoppingCart.add(new Product("Eggplant", 65)); + shoppingCart.add(new Product("Salad", 45)); + shoppingCart.add(new Product("Banana", 29)); - for (Product product : products) { + for (Product product : shoppingCart) { System.out.println(product.getName()); } } diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/Element.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/Element.java new file mode 100644 index 0000000000..e364d85deb --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/Element.java @@ -0,0 +1,36 @@ +package com.baeldung.collections.iterable; + +class Element { + + private Element prev; + private Element next; + private Product product; + + public Element(Product product) { + this.product = product; + } + + public Element getPrev() { + return prev; + } + + public void setPrev(Element prev) { + this.prev = prev; + } + + public Element getNext() { + return next; + } + + public void setNext(Element next) { + this.next = next; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } +} diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/ShoppingCart.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/ShoppingCart.java new file mode 100644 index 0000000000..d759294a55 --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/ShoppingCart.java @@ -0,0 +1,71 @@ +package com.baeldung.collections.iterable; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +class ShoppingCart implements Iterable { + + private Element firstElement; + private Element lastElement; + + public void add(Product product) { + Element newElement = new Element(product); + connectElements(newElement); + } + + private void connectElements(Element newElement) { + if (isEmpty()) { + addFirstElement(newElement); + return; + } + + addLastElement(newElement); + } + + private boolean isEmpty() { + return firstElement == null; + } + + private void addFirstElement(Element newElement) { + firstElement = newElement; + lastElement = newElement; + } + + private void addLastElement(Element newElement) { + newElement.setPrev(lastElement); + lastElement.setNext(newElement); + lastElement = newElement; + } + + @Override + public Iterator iterator() { + return new ShoppingCartIterator(); + } + + class ShoppingCartIterator implements Iterator { + private Element currentElement; + + public ShoppingCartIterator() { + currentElement = ShoppingCart.this.firstElement; + } + + @Override + public boolean hasNext() { + return currentElement != null; + } + + @Override + public Product next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return getNext(); + } + + private Product getNext() { + Element element = currentElement; + currentElement = currentElement.getNext(); + return element.getProduct(); + } + } +}