public abstract class Solution extends Object
equals(Object)
and hashCode()
that compare solutions by value (instead of reference) and
compute consistent hash codes, respectively. The default implementations inherited from Object
have been
erased and replaced with abstract methods without implementation.
Every solution class should also implement the abstract method copy()
which is used to create a deep
copy of a solution. The returned deep copy should always have the exact same type as the solution on which
the method was called. A static method checkedCopy(Solution)
is provided to create type safe copies of
any solution: it returns a deep copy of the exact same type as its argument, given that the general contract
of copy()
is followed in every solution type implementation. If this contract is violated, calling
checkedCopy(Solution)
will throw a detailed exception that precisely indicates the cause of the
occurred type mismatch.
Constructor and Description |
---|
Solution() |
Modifier and Type | Method and Description |
---|---|
static <T extends Solution> |
checkedCopy(T solution)
Creates a checked deep copy of the given solution with specific type
T
(a subclass of Solution ). |
abstract Solution |
copy()
Creates a deep copy of this solution.
|
abstract boolean |
equals(Object other)
Overrides default
Object.equals(Object) to be abstract so that all extending solution types are
required to provide an appropriate implementation that compares solutions by value instead of by reference. |
abstract int |
hashCode()
Overrides default
Object.hashCode() to be abstract so that all extending solution types are required
to provide a hash code computation that is consistent with the implementation of equals(Object) . |
public static <T extends Solution> T checkedCopy(T solution)
T
(a subclass of Solution
). Both the given solution and return type are of the
same type T
. This method calls copy()
on the given solution and
casts the result to the respective type T
. If this cast fails, an exception
with a detailed error message is thrown, precisely indicating the expected cause of the
type mismatch: the method copy()
does not return a solution of the correct type
T
, either because an undesired implementation is inherited from a super class
or because the direct implementation violates the general contract of copy()
.T
- solution type, required to extend Solution
solution
- solution to copy, of type T
T
SolutionCopyException
- if calling copy()
on the given solution of type T
does not yield a copy of the exact same type T
, indicating
a faulty implementation (contains a detailed error message)public abstract Solution copy()
public abstract boolean equals(Object other)
Object.equals(Object)
to be abstract so that all extending solution types are
required to provide an appropriate implementation that compares solutions by value instead of by reference.
A consistent implementation of hashCode()
should also be provided.public abstract int hashCode()
Object.hashCode()
to be abstract so that all extending solution types are required
to provide a hash code computation that is consistent with the implementation of equals(Object)
.
If two solutions are equal according to equals(Object)
they should have the same hash code.Copyright © 2016. All rights reserved.