org.jamesframework.core.problems.sol

## Class Solution

• Direct Known Subclasses:
SubsetSolution

public abstract class Solution
extends Object
Represents an abstract solution. Every extending solution type is required to provide appropriate implementations of 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.

Author:
Herman De Beukelaer
• ### Constructor Summary

Constructors
Constructor and Description
Solution()
• ### Method Summary

All Methods
Modifier and Type Method and Description
static <T extends Solution>T 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).
• ### Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
• ### Constructor Detail

• #### Solution

public Solution()
• ### Method Detail

• #### checkedCopy

public static <T extends Solution> T checkedCopy(T solution)
Creates a checked deep copy of the given solution with specific type 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().
Type Parameters:
T - solution type, required to extend Solution
Parameters:
solution - solution to copy, of type T
Returns:
copy of type T
Throws:
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)
• #### copy

public abstract Solution copy()
Creates a deep copy of this solution. The implementation of this method should always return a solution of the exact same type as the solution on which the method was called. Violating this contract might result in exceptions when copying solutions from within various classes of the framework. It is therefore of utmost importance that every solution class directly implements this method, also when extending an other concrete solution class in which the method has already been implemented, as an inherited implementation will never return a copy of the correct type.
Returns:
deep copy of this solution, which is equal to the original solution but does not share any references of contained objects with this original solution
• #### equals

public 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. A consistent implementation of hashCode() should also be provided.
Overrides:
equals in class Object
Parameters:
other - other object to compare for equality
Returns:
true if the given object represents the same solution
• #### hashCode

public 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). If two solutions are equal according to equals(Object) they should have the same hash code.
Overrides:
hashCode in class Object
Returns:
hash code of this solution