package test4; import java.util.ArrayList; import java.util.List; /** * A combination lock is a special kind of lock. Each combination lock * has a combination. The combination lock and its combination * form a composition. * */ public class CombinationLock extends Lock { private List<Integer> combination; /** * Initializes this combination lock to be open and have the given combination. * * @param combination the combination of this combination lock. * @pre. combination != null */ public CombinationLock(List<Integer> combination) { super(); this.setCombination(combination); } /** * Returns the combination of this combination lock. * * @return the combination of this combination lock. */ public List<Integer> getCombination() { return new ArrayList<Integer>(combination); } /** * Sets the combination of this combination lock to the * given combination. * * @param combination the new combination of this * combination lock. * @pre. combination != null */ public void setCombination(List<Integer> combination) { this.combination = new ArrayList<Integer>(combination); } /** * Tests whether this combination lock is the same as the given object. * Two combination locks are the same if they are either both open or * both closed and have the same combination. * * @param object an object. * @return true if this lock is the same as the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { CombinationLock other = (CombinationLock) object; equal = super.equals(object) && this.getCombination().equals(other.getCombination()); } else { equal = false; } return equal; } /** * Returns the hash code of this lock. * * @return the sum of the digits of the combination of this combination lock * if this combination lock is open, the sum of the digits of the combination * of this combination lock plus 1 if this lock is closed. */ public int hashCode() { int sum = super.hashCode(); for (Integer number : this.getCombination()) { sum += number; } return sum; } }