Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lab1-assignments
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jarrett Shan Wei Yeo
lab1-assignments
Commits
98c7d5aa
Commit
98c7d5aa
authored
5 years ago
by
Jarrett Shan Wei Yeo
Browse files
Options
Downloads
Patches
Plain Diff
Update Enumerable.java
parent
1fafabff
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Task_A/src/Enumerable.java
+50
-14
50 additions, 14 deletions
Task_A/src/Enumerable.java
with
50 additions
and
14 deletions
Task_A/src/Enumerable.java
+
50
−
14
View file @
98c7d5aa
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.Iterator
;
public
class
Enumerable
<
T
>
implements
Iterable
<
T
>
{
public
class
Enumerable
<
T
>
implements
Iterable
<
T
>
{
// Define variables
private
Enumerable
<
T
>
newEnumerable
;
private
Iterator
<
T
>
iterator
;
private
Iterator
<
T
>
iterator
;
private
Collection
<
T
>
newCollection
;
// Use collection for iteration operations
public
Enumerable
(
Collection
<
T
>
collection
)
{
public
Enumerable
(
Collection
<
T
>
collection
)
{
this
.
iterator
=
collection
.
iterator
();
this
.
iterator
=
collection
.
iterator
();
this
.
newCollection
=
collection
;
}
}
// Redefined where() method
Enumerable
<
T
>
where
(
IPredicate
<
T
>
predicate
)
{
Enumerable
<
T
>
where
(
IPredicate
<
T
>
predicate
)
{
Iterator
<
T
>
oldIterator
=
iterator
;
this
.
iterator
=
new
Iterator
<
T
>()
{
// Need to create a new Enumerable, or else the same reference will be used
// and you can only iterate through a given list once
newEnumerable
=
new
Enumerable
<
T
>(
newCollection
);
newEnumerable
.
iterator
=
new
Iterator
<
T
>()
{
Iterator
<
T
>
oldIterator
=
newEnumerable
.
iterator
;
@Override
@Override
public
boolean
hasNext
()
{
public
boolean
hasNext
()
{
return
oldIterator
.
hasNext
();
};
return
oldIterator
.
hasNext
();
}
@Override
@Override
public
T
next
()
{
public
T
next
()
{
T
item
=
oldIterator
.
hasNext
()
?
oldIterator
.
next
()
:
null
;
while
(
oldIterator
.
hasNext
()
&&
!
predicate
.
accept
(
item
))
{
// define element as T
item
=
oldIterator
.
next
();
T
element
;
// if iterator has more items, we get it, else return null
if
(
oldIterator
.
hasNext
())
{
element
=
oldIterator
.
next
();
}
else
{
element
=
null
;
}
}
return
item
;
while
(
oldIterator
.
hasNext
()
&&
!
predicate
.
accept
(
element
))
element
=
oldIterator
.
next
();
// handle last element
if
(
!(
oldIterator
.
hasNext
()
||
predicate
.
accept
(
element
))
)
element
=
null
;
return
element
;
}
}
};
};
return
this
;
// cannot use or return this, else the same list cannot be reused after 1 full iteration
return
newEnumerable
;
}
}
@Override
@Override
public
Iterator
<
T
>
iterator
()
{
public
Iterator
<
T
>
iterator
()
{
return
iterator
;
return
iterator
;
...
@@ -39,9 +70,14 @@ public class Enumerable<T> implements Iterable<T> {
...
@@ -39,9 +70,14 @@ public class Enumerable<T> implements Iterable<T> {
void
forEach
(
IAction
<
T
>
action
)
{
void
forEach
(
IAction
<
T
>
action
)
{
while
(
iterator
.
hasNext
())
{
while
(
iterator
.
hasNext
())
{
T
t
=
(
T
)
iterator
.
next
();
action
.
perform
(
t
);
// get next element
T
nextItem
=
(
T
)
iterator
.
next
();
// ignore last (null) element
if
(
nextItem
!=
null
)
action
.
perform
(
nextItem
);
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment