Fix iterable example

Change the example for the Iterable interface implementation
This commit is contained in:
apeterlic
2022-04-30 07:43:16 +02:00
parent 0c50022f58
commit 4015cbc038
4 changed files with 113 additions and 78 deletions

View File

@@ -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<E> implements Iterable<E> {
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<E> iterator() {
return new CollectionIterator();
}
public class CollectionIterator implements Iterator<E> {
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();
}
}
}
}

View File

@@ -4,13 +4,13 @@ class CustomIterableClient {
public static void main(String[] args) {
CustomCollection<Product> 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());
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,71 @@
package com.baeldung.collections.iterable;
import java.util.Iterator;
import java.util.NoSuchElementException;
class ShoppingCart implements Iterable<Product> {
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<Product> iterator() {
return new ShoppingCartIterator();
}
class ShoppingCartIterator implements Iterator<Product> {
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();
}
}
}