blob: 95100dd4d39d476aa6c1bdc0c3b0f92d90e6d9a1 (
plain) (
blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.hitfield;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* A specialized list iterator to manipulate tokens in HitField objects.
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public class TokenFieldIterator implements ListIterator<FieldPart> {
private int index = 0;
private int prevReturned = 0;
private final List<FieldPart> fieldList;
private final HitField hitField;
public TokenFieldIterator(List<FieldPart> fieldList, HitField hitField) {
this.fieldList = fieldList;
this.hitField = hitField;
}
public void add(FieldPart o) {
fieldList.add(index, o);
index++;
hitField.markDirty();
}
public boolean hasNext() {
int i = index;
while (i < fieldList.size()) {
if (fieldList.get(i).isToken())
return true;
i++;
}
return false;
}
public boolean hasPrevious() {
int i = index;
while (i > 0) {
i--;
if (fieldList.get(i).isToken())
return true;
}
return false;
}
public FieldPart next() {
int i = index;
while (i < fieldList.size()) {
if (fieldList.get(i).isToken()) {
index = i + 1;
prevReturned = i;
return fieldList.get(i);
}
i++;
}
throw new NoSuchElementException("No more tokens available.");
}
public int nextIndex() {
int i = index;
while (i < fieldList.size()) {
if (fieldList.get(i).isToken())
return i;
i++;
}
return fieldList.size();
}
public FieldPart previous() {
int i = index;
while (i > 0) {
i--;
if (fieldList.get(i).isToken()) {
index = i;
prevReturned = i;
return fieldList.get(i);
}
}
throw new NoSuchElementException("Trying to go before first token available.");
}
public int previousIndex() {
int i = index;
while (i > 0) {
i--;
if (fieldList.get(i).isToken())
return i;
}
return -1;
}
public void remove() {
fieldList.remove(prevReturned);
if (prevReturned < index)
index--;
hitField.markDirty();
}
public void set(FieldPart o) {
fieldList.set(prevReturned, o);
hitField.markDirty();
}
}
|