Guava: Use Objects.equal() to implement .equals().

This commit is contained in:
Andreas Schildbach 2015-01-24 13:15:19 +01:00
parent 113d5bb5ff
commit 27d55413cb
10 changed files with 117 additions and 69 deletions

View file

@ -21,6 +21,8 @@ import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -81,13 +83,13 @@ public final class Departure implements Serializable
if (!(o instanceof Departure))
return false;
final Departure other = (Departure) o;
if (!nullSafeEquals(this.plannedTime, other.plannedTime))
if (!Objects.equal(this.plannedTime, other.plannedTime))
return false;
if (!nullSafeEquals(this.predictedTime, other.predictedTime))
if (!Objects.equal(this.predictedTime, other.predictedTime))
return false;
if (!nullSafeEquals(this.line, other.line))
if (!Objects.equal(this.line, other.line))
return false;
if (!nullSafeEquals(this.destination, other.destination))
if (!Objects.equal(this.destination, other.destination))
return false;
return true;
}
@ -106,15 +108,6 @@ public final class Departure implements Serializable
return hashCode;
}
private boolean nullSafeEquals(final Object o1, final Object o2)
{
if (o1 == null && o2 == null)
return true;
if (o1 != null && o1.equals(o2))
return true;
return false;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)

View file

@ -20,6 +20,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import java.util.Currency;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -46,4 +48,27 @@ public final class Fare implements Serializable
this.unitName = unitName;
this.units = units;
}
@Override
public boolean equals(final Object o)
{
if (o == this)
return true;
if (!(o instanceof Fare))
return false;
final Fare other = (Fare) o;
if (!Objects.equal(this.network, other.network))
return false;
if (!Objects.equal(this.type, other.type))
return false;
if (!Objects.equal(this.currency, other.currency))
return false;
if (this.fare != other.fare)
return false;
if (!Objects.equal(this.unitName, other.unitName))
return false;
if (!Objects.equal(this.units, other.units))
return false;
return true;
}
}

View file

@ -20,6 +20,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import java.util.Set;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -94,7 +96,7 @@ public final class Line implements Serializable, Comparable<Line>
if (!(o instanceof Line))
return false;
final Line other = (Line) o;
return nullSafeEquals(this.label, other.label);
return Objects.equal(this.label, other.label);
}
@Override
@ -116,15 +118,6 @@ public final class Line implements Serializable, Comparable<Line>
return this.label.compareTo(other.label);
}
private boolean nullSafeEquals(final Object o1, final Object o2)
{
if (o1 == null && o2 == null)
return true;
if (o1 != null && o1.equals(o2))
return true;
return false;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)

View file

@ -19,6 +19,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -52,9 +54,9 @@ public final class LineDestination implements Serializable
if (!(o instanceof LineDestination))
return false;
final LineDestination other = (LineDestination) o;
if (!nullSafeEquals(this.line, other.line))
if (!Objects.equal(this.line, other.line))
return false;
if (!nullSafeEquals(this.destination, other.destination))
if (!Objects.equal(this.destination, other.destination))
return false;
return true;
}
@ -69,15 +71,6 @@ public final class LineDestination implements Serializable
return hashCode;
}
private boolean nullSafeEquals(final Object o1, final Object o2)
{
if (o1 == null && o2 == null)
return true;
if (o1 != null && o1.equals(o2))
return true;
return false;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)

View file

@ -20,6 +20,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import java.util.Arrays;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -165,17 +167,17 @@ public final class Location implements Serializable
if (!(o instanceof Location))
return false;
final Location other = (Location) o;
if (this.type != other.type)
if (!Objects.equal(this.type, other.type))
return false;
if (this.id != null)
return this.id.equals(other.id);
return Objects.equal(this.id, other.id);
if (this.lat != 0 && this.lon != 0)
return this.lat == other.lat && this.lon == other.lon;
// only discriminate by name/place if no ids are given
if (!nullSafeEquals(this.name, other.name))
if (!Objects.equal(this.name, other.name))
return false;
if (!nullSafeEquals(this.place, other.place))
if (!Objects.equal(this.place, other.place))
return false;
return true;
}
@ -199,15 +201,6 @@ public final class Location implements Serializable
return hashCode;
}
private boolean nullSafeEquals(final Object o1, final Object o2)
{
if (o1 == null && o2 == null)
return true;
if (o1 != null && o1.equals(o2))
return true;
return false;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)

View file

@ -19,6 +19,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -46,15 +48,6 @@ public final class Position implements Serializable
this.section = section;
}
@Override
public String toString()
{
final StringBuilder builder = new StringBuilder(name);
if (section != null)
builder.append(section);
return builder.toString();
}
@Override
public boolean equals(final Object o)
{
@ -63,13 +56,22 @@ public final class Position implements Serializable
if (!(o instanceof Position))
return false;
final Position other = (Position) o;
if (!this.name.equals(other.name))
if (!Objects.equal(this.name, other.name))
return false;
if (!nullSafeEquals(this.section, other.section))
if (!Objects.equal(this.section, other.section))
return false;
return true;
}
@Override
public String toString()
{
final StringBuilder builder = new StringBuilder(name);
if (section != null)
builder.append(section);
return builder.toString();
}
@Override
public int hashCode()
{
@ -79,15 +81,6 @@ public final class Position implements Serializable
return hashCode;
}
private boolean nullSafeEquals(final Object o1, final Object o2)
{
if (o1 == null && o2 == null)
return true;
if (o1 != null && o1.equals(o2))
return true;
return false;
}
private int nullSafeHashCode(final Object o)
{
if (o == null)

View file

@ -20,6 +20,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import java.util.List;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -48,4 +50,21 @@ public final class StationDepartures implements Serializable
builder.append("]");
return builder.toString();
}
@Override
public boolean equals(final Object o)
{
if (o == this)
return true;
if (!(o instanceof StationDepartures))
return false;
final StationDepartures other = (StationDepartures) o;
if (!Objects.equal(this.location, other.location))
return false;
if (!Objects.equal(this.departures, other.departures))
return false;
if (!Objects.equal(this.lines, other.lines))
return false;
return true;
}
}

View file

@ -20,6 +20,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import java.util.Date;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -217,4 +219,37 @@ public final class Stop implements Serializable
builder.append(")");
return builder.toString();
}
@Override
public boolean equals(final Object o)
{
if (o == this)
return true;
if (!(o instanceof Stop))
return false;
final Stop other = (Stop) o;
if (!Objects.equal(this.location, other.location))
return false;
if (!Objects.equal(this.plannedArrivalTime, other.plannedArrivalTime))
return false;
if (!Objects.equal(this.predictedArrivalTime, other.predictedArrivalTime))
return false;
if (!Objects.equal(this.plannedArrivalPosition, other.plannedArrivalPosition))
return false;
if (!Objects.equal(this.predictedArrivalPosition, other.predictedArrivalPosition))
return false;
if (this.arrivalCancelled != other.arrivalCancelled)
return false;
if (!Objects.equal(this.plannedDepartureTime, other.plannedDepartureTime))
return false;
if (!Objects.equal(this.predictedDepartureTime, other.predictedDepartureTime))
return false;
if (!Objects.equal(this.plannedDeparturePosition, other.plannedDeparturePosition))
return false;
if (!Objects.equal(this.predictedDeparturePosition, other.predictedDeparturePosition))
return false;
if (this.departureCancelled != other.departureCancelled)
return false;
return true;
}
}

View file

@ -19,6 +19,8 @@ package de.schildbach.pte.dto;
import java.io.Serializable;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -62,7 +64,7 @@ public final class SuggestedLocation implements Serializable, Comparable<Suggest
if (!(o instanceof SuggestedLocation))
return false;
final SuggestedLocation other = (SuggestedLocation) o;
return location.equals(other.location);
return Objects.equal(this.location, other.location);
}
@Override

View file

@ -24,6 +24,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import com.google.common.base.Objects;
/**
* @author Andreas Schildbach
*/
@ -219,7 +221,7 @@ public final class Trip implements Serializable
if (!(o instanceof Trip))
return false;
final Trip other = (Trip) o;
return getId().equals(other.getId());
return Objects.equal(this.getId(), other.getId());
}
@Override