Guava: Use Objects.hashCode() to implement .hashCode().

This commit is contained in:
Andreas Schildbach 2015-01-24 13:00:14 +01:00
parent 27d55413cb
commit 797448ef0e
11 changed files with 37 additions and 75 deletions

View file

@ -97,22 +97,7 @@ public final class Departure implements Serializable
@Override
public int hashCode()
{
int hashCode = 0;
hashCode += nullSafeHashCode(plannedTime);
hashCode *= 29;
hashCode += nullSafeHashCode(predictedTime);
hashCode *= 29;
hashCode += nullSafeHashCode(line);
hashCode *= 29;
hashCode += nullSafeHashCode(destination);
return hashCode;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)
return 0;
return o.hashCode();
return Objects.hashCode(plannedTime, predictedTime, line, destination);
}
public static final Comparator<Departure> TIME_COMPARATOR = new Comparator<Departure>()

View file

@ -71,4 +71,10 @@ public final class Fare implements Serializable
return false;
return true;
}
@Override
public int hashCode()
{
return Objects.hashCode(network, type, currency, fare, unitName, units);
}
}

View file

@ -102,7 +102,7 @@ public final class Line implements Serializable, Comparable<Line>
@Override
public int hashCode()
{
return nullSafeHashCode(label);
return Objects.hashCode(label);
}
public int compareTo(final Line other)
@ -117,11 +117,4 @@ public final class Line implements Serializable, Comparable<Line>
return this.label.compareTo(other.label);
}
private int nullSafeHashCode(final Object o)
{
if (o == null)
return 0;
return o.hashCode();
}
}

View file

@ -64,17 +64,6 @@ public final class LineDestination implements Serializable
@Override
public int hashCode()
{
int hashCode = 0;
hashCode += nullSafeHashCode(line);
hashCode *= 29;
hashCode += nullSafeHashCode(destination);
return hashCode;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)
return 0;
return o.hashCode();
return Objects.hashCode(line, destination);
}
}

View file

@ -185,26 +185,9 @@ public final class Location implements Serializable
@Override
public int hashCode()
{
int hashCode = 0;
hashCode += type.hashCode();
hashCode *= 29;
if (id != null)
{
hashCode += id.hashCode();
}
else if (lat != 0 || lon != 0)
{
hashCode += lat;
hashCode *= 29;
hashCode += lon;
}
return hashCode;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)
return 0;
return o.hashCode();
return Objects.hashCode(type, id);
else
return Objects.hashCode(type, lat, lon);
}
}

View file

@ -19,6 +19,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -61,6 +63,6 @@ public final class Point implements Serializable
@Override
public int hashCode()
{
return lat + 27 * lon;
return Objects.hashCode(lat, lon);
}
}

View file

@ -63,6 +63,12 @@ public final class Position implements Serializable
return true;
}
@Override
public int hashCode()
{
return Objects.hashCode(name, section);
}
@Override
public String toString()
{
@ -71,20 +77,4 @@ public final class Position implements Serializable
builder.append(section);
return builder.toString();
}
@Override
public int hashCode()
{
int hashCode = 0;
hashCode += name.hashCode();
hashCode += nullSafeHashCode(section);
return hashCode;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)
return 0;
return o.hashCode();
}
}

View file

@ -67,4 +67,10 @@ public final class StationDepartures implements Serializable
return false;
return true;
}
@Override
public int hashCode()
{
return Objects.hashCode(location, departures, lines);
}
}

View file

@ -252,4 +252,12 @@ public final class Stop implements Serializable
return false;
return true;
}
@Override
public int hashCode()
{
return Objects.hashCode(location, plannedArrivalTime, predictedArrivalTime, plannedArrivalPosition, predictedArrivalPosition,
arrivalCancelled, plannedDepartureTime, predictedDepartureTime, plannedDeparturePosition, predictedDeparturePosition,
departureCancelled);
}
}

View file

@ -70,7 +70,7 @@ public final class SuggestedLocation implements Serializable, Comparable<Suggest
@Override
public int hashCode()
{
return location.hashCode();
return Objects.hashCode(location);
}
@Override

View file

@ -227,7 +227,7 @@ public final class Trip implements Serializable
@Override
public int hashCode()
{
return getId().hashCode();
return Objects.hashCode(getId());
}
public abstract static class Leg implements Serializable