]> source.dussan.org Git - aspectj.git/commitdiff
More readable API than comparator for common floor operation (semantics-preserving)
authorwisberg <wisberg>
Sat, 19 Apr 2003 08:14:00 +0000 (08:14 +0000)
committerwisberg <wisberg>
Sat, 19 Apr 2003 08:14:00 +0000 (08:14 +0000)
bridge/src/org/aspectj/bridge/CountingMessageHandler.java
bridge/src/org/aspectj/bridge/IMessage.java
bridge/src/org/aspectj/bridge/MessageHandler.java
bridge/src/org/aspectj/bridge/MessageUtil.java
bridge/testsrc/org/aspectj/bridge/MessageTest.java

index 8c565a7b415ead96d383bf3b7212c52be8d6ce64..6c05253ee856cc370df121c408056167910456ce 100644 (file)
@@ -95,7 +95,7 @@ public class CountingMessageHandler implements IMessageHandler {
         } 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);
                 }
             }
index 7cb22910cba58e339d8f0b1d1324724faa23ff4d..7f7407e0fb74bf4e8c6f5715d0cfba2e30a1ba3c 100644 (file)
@@ -81,9 +81,9 @@ public interface IMessage {
                                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 {
@@ -91,7 +91,17 @@ public interface IMessage {
                                }
                        }
                };
-               
+        
+        /**
+         * @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);
                }
index 4bdb8a6ce683b6173d1a23926afabd01016df97c..bc68a0a435d3a7068dc15e03e20ea1ef4f1aef97 100644 (file)
@@ -158,10 +158,9 @@ public class MessageHandler implements IMessageHolder {
                 }
             }
         } 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;
                 }
             }
@@ -184,10 +183,9 @@ public class MessageHandler implements IMessageHolder {
                 }
             }
         } 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++;
                 }
             }
@@ -221,10 +219,9 @@ public class MessageHandler implements IMessageHolder {
                 }
             }
         } 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);
                 }
             }
index ddb93b4760ded9595902e17c3fbb9b7e080ba158..516ab09bfb3b8e36df735267533468f9ab1ce4d9 100644 (file)
@@ -522,15 +522,15 @@ public class MessageUtil {
         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;
@@ -542,11 +542,11 @@ public class MessageUtil {
          * 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) {
index 61488c8ef16d882a1e8c3beb262749bfed5db357..8688f44b45e50d9b4998651f4d51420201bb60bc 100644 (file)
@@ -17,6 +17,7 @@ import java.io.File;
 import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Comparator;
+import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
 
@@ -70,6 +71,23 @@ public class MessageTest extends TestCase {
         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);