From da265d4f810aecefb012906d432aaa153ca44610 Mon Sep 17 00:00:00 2001 From: Bhaskar Ghosh Dastidar Date: Thu, 18 May 2023 02:02:04 +0530 Subject: [PATCH] [BAEL-6339] custom iterator with testcases (#14038) Co-authored-by: Bhaskar --- .../core-java-collections-5/pom.xml | 20 ++- .../customiterators/CustomMovieIterator.java | 39 +++++ .../com/baeldung/customiterators/Movie.java | 37 +++++ .../com/baeldung/customiterators/MyList.java | 139 ++++++++++++++++++ .../customiterators/PalindromIterator.java | 44 ++++++ .../customiterators/IteratorsUnitTest.java | 53 +++++++ 6 files changed, 328 insertions(+), 4 deletions(-) create mode 100644 core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/CustomMovieIterator.java create mode 100644 core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/Movie.java create mode 100644 core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/MyList.java create mode 100644 core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/PalindromIterator.java create mode 100644 core-java-modules/core-java-collections-5/src/test/java/com/baeldung/customiterators/IteratorsUnitTest.java diff --git a/core-java-modules/core-java-collections-5/pom.xml b/core-java-modules/core-java-collections-5/pom.xml index 6baecc2802..84f62c696d 100644 --- a/core-java-modules/core-java-collections-5/pom.xml +++ b/core-java-modules/core-java-collections-5/pom.xml @@ -1,10 +1,22 @@ - + 4.0.0 core-java-collections-5 core-java-collections-5 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + + jar @@ -54,5 +66,5 @@ 0.9.38 1.36 - + diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/CustomMovieIterator.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/CustomMovieIterator.java new file mode 100644 index 0000000000..a1e34dd4c5 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/CustomMovieIterator.java @@ -0,0 +1,39 @@ +package com.baeldung.customiterators; + +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +public class CustomMovieIterator implements Iterator { + private int currentIndex; + private final List list; + + public CustomMovieIterator(List list) { + this.list = list; + this.currentIndex = 0; + } + + @Override + public boolean hasNext() { + while (currentIndex < list.size()) { + Movie currentMovie = list.get(currentIndex); + if (isMovieEligible(currentMovie)) { + return true; + } + currentIndex++; + } + return false; + } + + @Override + public Movie next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return list.get(currentIndex++); + } + + private boolean isMovieEligible(Movie movie) { + return movie.getRating() >= 8; + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/Movie.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/Movie.java new file mode 100644 index 0000000000..e1188e69bf --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/Movie.java @@ -0,0 +1,37 @@ +package com.baeldung.customiterators; + +public class Movie { + private String name; + private String director; + private float rating; + + public Movie(String name, String director, float rating) { + this.name = name; + this.director = director; + this.rating = rating; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDirector() { + return director; + } + + public void setDirector(String director) { + this.director = director; + } + + public float getRating() { + return rating; + } + + public void setRating(float rating) { + this.rating = rating; + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/MyList.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/MyList.java new file mode 100644 index 0000000000..a0b2f4a943 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/MyList.java @@ -0,0 +1,139 @@ +package com.baeldung.customiterators; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +/** + * Please note that this class has been added for representation purposes of how a custom collection and its iterator would be. + * This does not have working code. + */ +public class MyList implements List { + @Override + public int size() { + return 0; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object o) { + return false; + } + + @Override + public boolean add(E e) { + return false; + } + + @Override + public boolean remove(Object o) { + return false; + } + + @Override + public Iterator iterator() { + return new MyListIterator(); + } + + private class MyListIterator implements Iterator { + @Override + public boolean hasNext() { + return false; + } + + @Override + public E next() { + return null; + } + } + + @Override + public Object[] toArray() { + return new Object[0]; + } + + @Override + public T[] toArray(T[] a) { + return null; + } + + @Override + public boolean containsAll(Collection c) { + return false; + } + + @Override + public boolean addAll(Collection c) { + return false; + } + + @Override + public boolean addAll(int index, Collection c) { + return false; + } + + @Override + public boolean removeAll(Collection c) { + return false; + } + + @Override + public boolean retainAll(Collection c) { + return false; + } + + @Override + public void clear() { + + } + + @Override + public E get(int index) { + return null; + } + + @Override + public E set(int index, E element) { + return null; + } + + @Override + public void add(int index, E element) { + + } + + @Override + public E remove(int index) { + return null; + } + + @Override + public int indexOf(Object o) { + return 0; + } + + @Override + public int lastIndexOf(Object o) { + return 0; + } + + @Override + public ListIterator listIterator() { + return null; + } + + @Override + public ListIterator listIterator(int index) { + return null; + } + + @Override + public List subList(int fromIndex, int toIndex) { + return null; + } +} diff --git a/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/PalindromIterator.java b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/PalindromIterator.java new file mode 100644 index 0000000000..50263f52ec --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/main/java/com/baeldung/customiterators/PalindromIterator.java @@ -0,0 +1,44 @@ +package com.baeldung.customiterators; + +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +public class PalindromIterator implements Iterator { + private final List list; + private int currentIndex; + + public PalindromIterator(List list) { + this.list = list; + this.currentIndex = 0; + } + + @Override + public boolean hasNext() { + while (currentIndex < list.size()) { + String currString = list.get(currentIndex); + if (isPalindrome(currString)) { + return true; + } + currentIndex++; + } + return false; + } + + @Override + public String next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return list.get(currentIndex++); + } + + private boolean isPalindrome(String input) { + for (int i = 0; i < input.length() / 2; i++) { + if (input.charAt(i) != input.charAt(input.length() - i - 1)) { + return false; + } + } + return true; + } +} diff --git a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/customiterators/IteratorsUnitTest.java b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/customiterators/IteratorsUnitTest.java new file mode 100644 index 0000000000..04389eb1d2 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/customiterators/IteratorsUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.customiterators; + +import static org.junit.Assert.assertEquals; + +import java.util.Iterator; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +public class IteratorsUnitTest { + @Test + public void givenListOfStrings_whenIteratedWithDefaultIterator() { + List listOfStrings = List.of("hello", "world", "this", "is", "a", "test"); + Iterator iterator = listOfStrings.iterator(); + Assert.assertTrue(iterator.hasNext()); + assertEquals(iterator.next(), "hello"); + } + + @Test + public void givenListOfStrings_whenPalindromIterator_thenOnlyPalindromes() { + List listOfStrings = List.of("oslo", "madam", "car", "deed", "wow", "test"); + PalindromIterator palindromIterator = new PalindromIterator(listOfStrings); + int count = 0; + while (palindromIterator.hasNext()) { + palindromIterator.next(); + count++; + } + assertEquals(count, 3); + } + + @Test + public void givenMovieList_whenMovieIteratorUsed_thenOnlyHighRatedMovies() { + List movies = getMovies(); + CustomMovieIterator movieIterator = new CustomMovieIterator(movies); + int count = 0; + while (movieIterator.hasNext()) { + movieIterator.next(); + count++; + } + assertEquals(4, movies.size()); + assertEquals(2, count); + } + + private List getMovies() { + Movie movie1 = new Movie("The Dark Knight", "Nolan", 10); + Movie movie2 = new Movie("Avatar", "Cameron", 9); + Movie movie3 = new Movie("Tenet", "Nolan", 7); + Movie movie4 = new Movie("Extraction", "Hargrave", 5); + return List.of(movie1, movie2, movie3, movie4); + } + +}