} else {
for (Iterator iter = IMessage.KINDS.iterator(); iter.hasNext();) {
IMessage.Kind k = (IMessage.Kind) iter.next();
- if (0 >= IMessage.Kind.COMPARATOR.compare(kind, k)) {
+ if (kind.isSameOrLessThan(k)) {
result += numMessages(k);
}
}
Kind one = (Kind) o1;
Kind two = (Kind) o2;
if (null == one) {
- return (null == two ? 0 : 1);
+ return (null == two ? 0 : -1);
} else if (null == two) {
- return -1;
+ return 1;
} else if (one == two) {
return 0;
} else {
}
}
};
-
+
+ /**
+ * @param kind the Kind floor
+ * @return false if kind is null or this
+ * has less precedence than kind,
+ * true otherwise.
+ */
+ public boolean isSameOrLessThan(Kind kind) {
+ return (0 >= COMPARATOR.compare(this, kind));
+ }
+
public int compareTo(Object other) {
return COMPARATOR.compare(this, other);
}
}
}
} else {
- final Comparator c = IMessage.Kind.COMPARATOR;
for (Iterator iter = messages.iterator(); iter.hasNext();) {
IMessage m = (IMessage) iter.next();
- if (0 >= c.compare(kind, m.getKind())) {
+ if (kind.isSameOrLessThan(m.getKind())) {
return true;
}
}
}
}
} else {
- final Comparator c = IMessage.Kind.COMPARATOR;
for (Iterator iter = messages.iterator(); iter.hasNext();) {
IMessage m = (IMessage) iter.next();
- if (0 >= c.compare(kind, m.getKind())) {
+ if (kind.isSameOrLessThan(m.getKind())) {
result++;
}
}
}
}
} else {
- final Comparator c = IMessage.Kind.COMPARATOR;
for (Iterator iter = messages.iterator(); iter.hasNext();) {
- IMessage m = (IMessage) iter.next();
- if (0 >= c.compare(kind, m.getKind())) {
+ IMessage m = (IMessage) iter.next();
+ if (kind.isSameOrLessThan(m.getKind())) {
result.add(m);
}
}
final IMessage.Kind sought;
final boolean floor;
final String infix;
-
+
KindSelector(IMessage.Kind sought) {
this(sought, false);
}
-
+
KindSelector(IMessage.Kind sought, boolean floor) {
this(sought, floor, null);
}
-
+
KindSelector(IMessage.Kind sought, boolean floor, String infix) {
this.sought = sought;
this.floor = floor;
* or if this has the exact kind we seek
* and this has any text sought
*/
- public boolean handleMessage(IMessage message) {
+ public boolean handleMessage(IMessage message) {
return ((null != message) && !isIgnoring(message.getKind())
&& textIn(message));
- }
-
+ }
+
/** @return true if handleMessage would return false for a message of this kind */
public boolean isIgnoring(IMessage.Kind kind) {
if (!floor) {
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
+import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
checkListOrder(IMessage.KINDS, IMessage.Kind.COMPARATOR);
}
+ public void testKind_isSameOrLessThan() {
+ IMessage.Kind last;
+ IMessage.Kind next = null;
+ for (Iterator iter = IMessage.KINDS.iterator(); iter.hasNext();) {
+ last = next;
+ next = (IMessage.Kind) iter.next();
+ if (null == last) {
+ continue;
+ }
+ String label = "last: " + last + " next: " + next;
+ assertTrue(label, !next.isSameOrLessThan(null));
+ assertTrue(label, !next.isSameOrLessThan(last));
+ assertTrue(label, last.isSameOrLessThan(next));
+ assertTrue(label, next.isSameOrLessThan(next));
+ }
+ }
+
public void testMessageHandler() {
boolean handleMessageResult = true;
checkEmptyMessageHolder(new MessageHandler(handleMessageResult), handleMessageResult);