activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r758781 - in /activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf: Buffer.java UTF8Buffer.java
Date Thu, 26 Mar 2009 18:10:23 GMT
Author: chirino
Date: Thu Mar 26 18:10:01 2009
New Revision: 758781

URL: http://svn.apache.org/viewvc?rev=758781&view=rev
Log:
Made the Buffer objects comparable

Modified:
    activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
    activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java

Modified: activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
URL: http://svn.apache.org/viewvc/activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java?rev=758781&r1=758780&r2=758781&view=diff
==============================================================================
--- activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
(original)
+++ activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
Thu Mar 26 18:10:01 2009
@@ -19,7 +19,7 @@
 
 import java.util.List;
 
-public class Buffer {
+public class Buffer implements Comparable<Buffer> {
 
     final public byte[] data;
     final public int offset;
@@ -177,5 +177,32 @@
     public String toStringUtf8() {
         return UTF8Buffer.decode(this);
     }
+
+    public int compareTo(Buffer o) {
+        int minLength = Math.min(length, o.length);
+        if (offset == o.offset) {
+            int pos = offset;
+            int limit = minLength + offset;
+            while (pos < limit) {
+                byte b1 = data[pos];
+                byte b2 = o.data[pos];
+                if (b1 != b2) {
+                    return b1 - b2;
+                }
+                pos++;
+            }
+        } else {
+            int offset1 = offset;
+            int offset2 = o.offset;
+            while ( minLength-- != 0) {
+                byte b1 = data[offset1++];
+                byte b2 = o.data[offset2++];
+                if (b1 != b2) {
+                    return b1 - b2;
+                }
+            }
+        }
+        return length - o.length;
+    }
         
 }

Modified: activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
URL: http://svn.apache.org/viewvc/activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java?rev=758781&r1=758780&r2=758781&view=diff
==============================================================================
--- activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
(original)
+++ activemq/activemq-protobuf/trunk/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
Thu Mar 26 18:10:01 2009
@@ -5,6 +5,7 @@
 final public class UTF8Buffer extends Buffer {
 
     int hashCode;
+    String value; 
     
     public UTF8Buffer(Buffer other) {
         super(other);
@@ -31,7 +32,16 @@
 
     public String toString()
     {
-        return decode(this);
+        if( value==null ) {
+            value = decode(this); 
+        }
+        return value;
+    }
+    
+    @Override
+    public int compareTo(Buffer other) {
+        // Do a char comparison.. not a byte for byte comparison.
+        return toString().compareTo(other.toString());
     }
 
     @Override



Mime
View raw message