Skip to content

Commit

Permalink
compareTo should live in base class
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Nov 2, 2017
1 parent bde38ab commit 24f8667
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
Expand Up @@ -79,6 +79,29 @@ public String prettyPrint()
return format( "'%s'", value() ); return format( "'%s'", value() );
} }


@Override
public int compareTo( TextValue other )
{
String thisString = value();
String thatString = other.stringValue();
int len1 = thisString.length();
int len2 = thatString.length();
int lim = Math.min( len1, len2 );

int k = 0;
while ( k < lim )
{
int c1 = thisString.codePointAt( k );
int c2 = thatString.codePointAt( k );
if ( c1 != c2 )
{
return c1 - c2;
}
k += Character.charCount( c1 );
}
return length() - other.length();
}

static TextValue EMTPY = new StringValue() static TextValue EMTPY = new StringValue()
{ {
@Override @Override
Expand Down
Expand Up @@ -62,29 +62,6 @@ public int computeHash()
return h; return h;
} }


@Override
public int compareTo( TextValue other )
{
String thisString = value;
String thatString = other.stringValue();
int len1 = thisString.length();
int len2 = thatString.length();
int lim = Math.min( len1, len2 );

int k = 0;
while ( k < lim )
{
int c1 = thisString.codePointAt( k );
int c2 = thatString.codePointAt( k );
if ( c1 != c2 )
{
return c1 - c2;
}
k += Character.charCount( c1 );
}
return length() - other.length();
}

@Override @Override
public TextValue substring( int start, int length ) public TextValue substring( int start, int length )
{ {
Expand Down
Expand Up @@ -261,7 +261,7 @@ public int compareTo( TextValue other )
{ {
if ( !(other instanceof UTF8StringValue) ) if ( !(other instanceof UTF8StringValue) )
{ {
return -other.compareTo( this ); return super.compareTo( other );
} }
UTF8StringValue otherUTF8 = (UTF8StringValue) other; UTF8StringValue otherUTF8 = (UTF8StringValue) other;
int len1 = bytes.length; int len1 = bytes.length;
Expand Down

0 comments on commit 24f8667

Please sign in to comment.