-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNameRepository.java
44 lines (38 loc) · 1.23 KB
/
NameRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package iterator.task;
public class NameRepository implements Aggregate {
private String names[][] = {{"Rob", "Jon", "-"}, {"Jul", "-", "Lor", "Pat", "-"}, {"-", "-", "Ken"}};
//other methods of the NameRepository
@Override
public Iterator getIterator() {
return new NameIterator();
}
private class NameIterator implements Iterator<String> {
int indexX;
int indexY;
@Override
public boolean hasNext() {
for (int i = indexX; i < names.length; i++) {
for (int j = indexY; j < names[i].length; j++) {
if (isValidData(names[i][j])) {
return true;
}
}
}
return false;
}
private boolean isValidData(String data) {
return data != null && !data.trim().equals("-");
}
@Override
public String next() {
for (int i = indexX; i < names.length; i++) {
for (int j = indexY; j < names[i].length; j++) {
if (isValidData(names[i][j])) {
return names[i][j];
}
}
}
return null;
}
}
}