Difference between revisions of "Iterable Immutable List Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 45: Line 45:
 
=Diagrams=
 
=Diagrams=
 
==ImmutableList==
 
==ImmutableList==
 
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C'); </nowiki>
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C'); </nowiki>
  
Line 57: Line 56:
 
[[File:ImmutableList_iterator.svg|600px]]
 
[[File:ImmutableList_iterator.svg|600px]]
  
==State after next()==
+
==Iterator State after next()==
  
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
Line 64: Line 63:
 
     char a = iter.next();
 
     char a = iter.next();
 
} </nowiki>
 
} </nowiki>
 +
 
[[File:ImmutableList_iterator_next.svg|600px]]
 
[[File:ImmutableList_iterator_next.svg|600px]]
  
==State after next() next()==
+
==Iterator State after next() next()==
 
 
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
 
Iterator<Char> iter = letters.iterator();
 
Iterator<Char> iter = letters.iterator();
char a = iter.next();  
+
if(iter.hasNext()) {
char b = iter.next(); </nowiki>
+
    char a = iter.next();
 +
    if(iter.hasNext()) {
 +
        char b = iter.next();
 +
    }
 +
} </nowiki>
  
 
[[File:ImmutableList_iterator_next_next.svg|600px]]
 
[[File:ImmutableList_iterator_next_next.svg|600px]]
 
+
==Iterator State after next() next() next()==
==State after next() next() next()==
 
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
 
  <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
 
Iterator<Char> iter = letters.iterator();
 
Iterator<Char> iter = letters.iterator();
char a = iter.next();  
+
if(iter.hasNext()) {
char b = iter.next();
+
    char a = iter.next();
char c = iter.next(); </nowiki>
+
    if(iter.hasNext()) {
 +
        char b = iter.next();
 +
        if(iter.hasNext()) {
 +
            char c = iter.next();
 +
        }
 +
    }
 +
} </nowiki>
  
 
[[File:ImmutableList_iterator_next_next_next.svg|600px]]
 
[[File:ImmutableList_iterator_next_next_next.svg|600px]]

Revision as of 11:56, 19 December 2020

Studio To Revisit

In this studio we will complete the DefaultImmutableList implementation from the ImmutableList studio.

ImmutableList

The ImmutableList interface extends Iterable which has one method: iterator().

public interface ImmutableList<E> extends Iterable<E> {
	E head();
	ImmutableList<E> tail();
	boolean isEmpty();
}

Iterable

interface java.lang.Iterable<T>

  1. Iterator<T> iterator()

Iterator

interface java.util.Iterator<T>

  1. boolean hasNext()
  2. T next()

Examples

Using Iterable

Implementing Iterable

DoubleRange

ArrayIterable

Example

class: IterableImmutableListExample.java Presentation icon-72a7cf.svg
package: immutable.list.example
source folder: src/main/java

The code:

ImmutableList<Integer> numbers = Lists.brackets(4, 66, 99);
for (Integer i : numbers) {
	System.out.println(i);
}

produces:

4
66
99

Diagrams

ImmutableList

ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C'); 

ImmutableList letters.svg

Initial Iterator State

ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
Iterator<Char> iter = letters.iterator(); 

ImmutableList iterator.svg

Iterator State after next()

ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
Iterator<Char> iter = letters.iterator();
if(iter.hasNext()) {
    char a = iter.next();
} 

ImmutableList iterator next.svg

Iterator State after next() next()

ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
Iterator<Char> iter = letters.iterator();
if(iter.hasNext()) {
    char a = iter.next();
    if(iter.hasNext()) {
        char b = iter.next();
    }
} 

ImmutableList iterator next next.svg

Iterator State after next() next() next()

ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C');
Iterator<Char> iter = letters.iterator();
if(iter.hasNext()) {
    char a = iter.next();
    if(iter.hasNext()) {
        char b = iter.next();
        if(iter.hasNext()) {
            char c = iter.next();
        }
    }
} 

ImmutableList iterator next next next.svg

Code To Implement

enum EmptyImmutableList

class: EmptyImmutableList.java Java.png
methods: iterator
package: immutable.list.assignment
source folder: src/main/java

iterator()

return an Iterator for the EmptyImmutableList.

Question to ask yourself: Does an iterator for an EmptyImmutableList ever have a next?

class NonEmptyImmutableList

class: NonEmptyImmutableList.java Java.png
methods: iterator
package: immutable.list.assignment
source folder: src/main/java

iterator()

return an Iterator for this instance of NonEmptyImmutableList.

Note: If you are within an anonymous inner class, the way to get the outer class's this instance is to qualify it with the out class identifier. For example:

NonEmptyImmutableList.this

Test

class: IterableAssignmentTestSuite.java Junit.png
package: immutable.list.assignment
source folder: src/test/java
class: ImmutableListIteratorMethodTestSuite.java Junit.png
package: immutable.list.assignment
source folder: src/test/java