Made the Buffer objects comparable

git-svn-id: https://svn.apache.org/repos/asf/activemq/activemq-protobuf/trunk@758781 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
index 144aa44..214d742 100644
--- a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
+++ b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
@@ -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;

+    }

         

 }

diff --git a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
index 1ddb52c..c069988 100644
--- a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
+++ b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
@@ -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