|
| 1 | +package com.mayankrastogi.cs474.hw2.examples; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +public class IteratorExamplesMain { |
| 10 | + |
| 11 | + private static final Logger LOGGER = LoggerFactory.getLogger(IteratorExamplesMain.class); |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + System.out.println("\n" + |
| 15 | + "=======================================================================================================\n" + |
| 16 | + " Design Pattern Verifier - Iterator Design Pattern Example Programs\n" + |
| 17 | + "=======================================================================================================\n" + |
| 18 | + "The iterator design pattern \"provides a way to access the elements of an aggregate object sequentially\n" + |
| 19 | + "without exposing its underlying representation.\n" + |
| 20 | + "\n" + |
| 21 | + "This application demonstrates 3 classes that use the iterator pattern and are annotated with different\n" + |
| 22 | + "variations of the @Iterator and @IterableAggregate (and their nested) annotations:\n" + |
| 23 | + "\n" + |
| 24 | + " 1. StudentCollection: A class annotated with @IterableAggregate that implements `java.lang.Iterable`.\n" + |
| 25 | + " It's iterator class is annotated with @Iterator and implements `java.util.Iterator`. The\n" + |
| 26 | + " iterator returns a `Student` object during iteration.\n" + |
| 27 | + " 2. RangeGenerator: A class annotated with @Iterator that generates a primitive `int` value on each\n" + |
| 28 | + " iteration within the range specified while constructing its object.\n" + |
| 29 | + " 3. Tree: A class annotated with @IterableAggregate that provides two methods annotated with\n" + |
| 30 | + " @IteratorFactory. The two methods return instances of `Tree.TreeIterator` that allow the user to\n" + |
| 31 | + " iterate the nodes of the tree in depth-first and breadth-first manners.\n" + |
| 32 | + "=======================================================================================================" |
| 33 | + ); |
| 34 | + |
| 35 | + printExampleName("Student Collection Iteration Example"); |
| 36 | + runStudentCollectionExample(); |
| 37 | + |
| 38 | + printExampleName("RangeGenerator Iteration Example"); |
| 39 | + runRangeExample(); |
| 40 | + |
| 41 | + printExampleName("Tree Iteration Example"); |
| 42 | + runTreeExample(); |
| 43 | + } |
| 44 | + |
| 45 | + private static void printExampleName(String exampleName) { |
| 46 | + System.out.println("\n\n" + |
| 47 | + "-------------------------------------------------------------------------------------------------------\n" + |
| 48 | + exampleName + "\n" + |
| 49 | + "-------------------------------------------------------------------------------------------------------\n" + |
| 50 | + "\n" |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + private static void runStudentCollectionExample() { |
| 55 | + LOGGER.debug("Creating StudentCollection..."); |
| 56 | + var students = new StudentCollection(); |
| 57 | + |
| 58 | + students.addStudent(new Student("Alex", 20, Degree.Bachelors, 3.2f)); |
| 59 | + students.addStudent(new Student("Bob", 25, Degree.Masters, 3.6f)); |
| 60 | + students.addStudent(new Student("Chuck", 28, Degree.PhD, 3.5f)); |
| 61 | + |
| 62 | + LOGGER.debug("Iterating StudentCollection..."); |
| 63 | + for (var student : students) { |
| 64 | + System.out.println("Name: " + student.getName()); |
| 65 | + System.out.println("Age: " + student.getAge()); |
| 66 | + System.out.println("Degree: " + student.getDegree()); |
| 67 | + System.out.println("GPA: " + student.getGPA()); |
| 68 | + System.out.println(); |
| 69 | + } |
| 70 | + LOGGER.debug("StudentCollection Iteration Example Finished."); |
| 71 | + } |
| 72 | + |
| 73 | + private static void runRangeExample() { |
| 74 | + int from = 1; |
| 75 | + int to = 10; |
| 76 | + |
| 77 | + LOGGER.debug(String.format("Creating a RangeGenerator(from: %s, to: %s)...", from, to)); |
| 78 | + var range = new RangeGenerator(from, to); |
| 79 | + |
| 80 | + LOGGER.debug("Iterating RangeGenerator..."); |
| 81 | + while (!range.isDone()) { |
| 82 | + System.out.println(range.next()); |
| 83 | + } |
| 84 | + |
| 85 | + LOGGER.debug("RangeGenerator Iteration Example Finished."); |
| 86 | + } |
| 87 | + |
| 88 | + private static void runTreeExample() { |
| 89 | + System.out.println("" + |
| 90 | + "Sample Tree:\n" + |
| 91 | + "-----------\n" + |
| 92 | + "\n" + |
| 93 | + " 1\n" + |
| 94 | + " _______________________________|_______________________________\n" + |
| 95 | + " | | |\n" + |
| 96 | + " 2 3 4\n" + |
| 97 | + " _______|_______ _______|_______ _______|_______\n" + |
| 98 | + " | | | | | |\n" + |
| 99 | + " 5 6 7 8 9 10\n" + |
| 100 | + "\n"); |
| 101 | + |
| 102 | + LOGGER.debug("Creating 10 nodes using the RangeGenerator..."); |
| 103 | + var allNodes = new ArrayList<Node<Integer>>(); |
| 104 | + var range = new RangeGenerator(1, 10); |
| 105 | + while (!range.isDone()) { |
| 106 | + allNodes.add(new Node<>(range.next())); |
| 107 | + } |
| 108 | + LOGGER.debug("allNodes: " + allNodes); |
| 109 | + |
| 110 | + LOGGER.debug("Creating Sample Tree..."); |
| 111 | + |
| 112 | + allNodes.get(0).children = Arrays.asList(allNodes.get(1), allNodes.get(2), allNodes.get(3)); |
| 113 | + allNodes.get(1).children = Arrays.asList(allNodes.get(4), allNodes.get(5)); |
| 114 | + allNodes.get(2).children = Arrays.asList(allNodes.get(6), allNodes.get(7)); |
| 115 | + allNodes.get(3).children = Arrays.asList(allNodes.get(8), allNodes.get(9)); |
| 116 | + |
| 117 | + var tree = new Tree<>(allNodes.get(0)); |
| 118 | + LOGGER.debug("tree: " + tree); |
| 119 | + |
| 120 | + LOGGER.debug("Iterating tree using `dfsIterator()`..."); |
| 121 | + |
| 122 | + System.out.println("Depth-first tree traversal:"); |
| 123 | + var iterator = tree.dfsIterator(); |
| 124 | + while (!iterator.isDone()) { |
| 125 | + System.out.print(" " + iterator.next().data); |
| 126 | + } |
| 127 | + System.out.println(); |
| 128 | + |
| 129 | + LOGGER.debug("Iterating tree using `bfsIterator()`..."); |
| 130 | + |
| 131 | + System.out.println("Breadth-first tree traversal:"); |
| 132 | + iterator = tree.bfsIterator(); |
| 133 | + while (!iterator.isDone()) { |
| 134 | + System.out.print(" " + iterator.next().data); |
| 135 | + } |
| 136 | + System.out.println(); |
| 137 | + |
| 138 | + LOGGER.debug("Tree Iteration Example Finished."); |
| 139 | + } |
| 140 | +} |
0 commit comments