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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/*
* Original author: Ben Damm <bdamm@dammfine.com>
* Changes by: Mog
* - Fixed bug with substring handling
* */
package f00f.net.irc.martyr.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
//TODO: Unit test
/**
* This class iterates over the parameter string of an IRC command,
* returning each parameter in order as next() is called. This class
* also knows about the ":" parameters, which is the large string at
* the end of most commands, and treats it specially.
*/
public class ParameterIterator implements Iterator
{
//static Logger log = Logger.getLogger(ParameterIterator.class);
private String paramStr;
private int position;
private String last = null;
public ParameterIterator( String paramStr )
{
//log.debug("ParameterIterator: Params: `" + paramStr + "'");
// We don't check for null here because hasNext is the place
// to do it, according to the definition for Iterator.
// next() should throw an exception.
if( paramStr != null )
{
this.paramStr = paramStr.trim();
position = 0;
}
else
{
this.paramStr = null;
position = -1;
}
}
/**
* @return true if there are more parameters, and false
* otherwise.
*/
public boolean hasNext()
{
if( paramStr == null )
return false;
return position < paramStr.length();
}
/**
* @throws NoSuchElementException if there are no more params
* @return true if the next parameter is also the ":" parameter.
* */
public boolean nextIsLast()
{
if( ! hasNext() )
{
throw new NoSuchElementException("No more parameters.");
}
return paramStr.charAt(position) == ':';
}
/**
* @throws NoSuchElementException if there are no more params
* */
public Object next()
{
if( ! hasNext() )
{
throw new NoSuchElementException("No more parameters.");
}
// If : is the first char, the rest of the string is a
// parameter.
if( paramStr.charAt(position) == ':' )
{
String result = paramStr.substring(position + 1);
position = paramStr.length();
last = result;
return result;
}
int spaceIndex = paramStr.indexOf( ' ', position );
// We can't have a space after the last parameter, it gets
// trimmed in the constructor. Also, we can't have only
// spaces, so we don't need to check for -1. Finally, we are
// guaranteed to have a space before the colon, so we don't
// have to do any checking at all!
String result = paramStr.substring( position, spaceIndex );
position = spaceIndex + 1;
return result;
}
/**
* Forwards the iterator to the last element and returns it. The
* "last" parameter should be the ":" parameter.
*
* @return Last parameter
* */
public String last()
{
while( hasNext() )
next();
return last;
}
public void remove()
{
// hmm, nah. This can be implemented some other time.
throw new UnsupportedOperationException( "Remove on the parameters? Why?" );
}
}
|