[MNG-6630] - Make ComparableVersion faster

By not allocating the canonical representation for equals/hashcode,
but instead using the items we already have. This saves both time
and memory.

I left the canonical field around for testing purposes.
This commit is contained in:
Stefan Oehme 2019-04-09 15:35:07 +02:00 committed by Hervé Boutemy
parent baed5a294f
commit ebac165990
1 changed files with 100 additions and 4 deletions

View File

@ -148,6 +148,30 @@ public int compareTo( Item item )
}
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
IntItem intItem = (IntItem) o;
return value == intItem.value;
}
@Override
public int hashCode()
{
return value;
}
@Override
public String toString()
{
@ -209,6 +233,30 @@ public int compareTo( Item item )
}
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
LongItem longItem = (LongItem) o;
return value == longItem.value;
}
@Override
public int hashCode()
{
return (int) ( value ^ ( value >>> 32 ) );
}
@Override
public String toString()
{
@ -270,6 +318,29 @@ public int compareTo( Item item )
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
BigIntegerItem that = (BigIntegerItem) o;
return value.equals( that.value );
}
@Override
public int hashCode()
{
return value.hashCode();
}
public String toString()
{
return value.toString();
@ -382,6 +453,29 @@ public int compareTo( Item item )
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
StringItem that = (StringItem) o;
return value.equals( that.value );
}
@Override
public int hashCode()
{
return value.hashCode();
}
public String toString()
{
return value;
@ -581,8 +675,6 @@ else if ( Character.isDigit( c ) )
list = (ListItem) stack.pop();
list.normalize();
}
canonical = items.toString();
}
private static Item parseItem( boolean isDigit, String buf )
@ -636,19 +728,23 @@ public String toString()
public String getCanonical()
{
if ( canonical == null )
{
canonical = items.toString();
}
return canonical;
}
@Override
public boolean equals( Object o )
{
return ( o instanceof ComparableVersion ) && canonical.equals( ( (ComparableVersion) o ).canonical );
return ( o instanceof ComparableVersion ) && items.equals( ( (ComparableVersion) o ).items );
}
@Override
public int hashCode()
{
return canonical.hashCode();
return items.hashCode();
}
// CHECKSTYLE_OFF: LineLength