Skip to content
Permalink
Browse files
Made the Buffer objects comparable
git-svn-id: https://svn.apache.org/repos/asf/activemq/activemq-protobuf/trunk@758781 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
chirino committed Mar 26, 2009
1 parent 16a8b64 commit 54e096ef6cb20fc5f7099bdca3bb1f393a87b740
Showing 2 changed files with 39 additions and 2 deletions.
@@ -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 @@ final public static Buffer join(List<Buffer> items, Buffer seperator) {
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;
}

}
@@ -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 UTF8Buffer compact() {

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

0 comments on commit 54e096e

Please sign in to comment.