package test4; import java.util.HashSet; import java.util.Set; /** * A table is a piece of furniture. Each table * has a collection of legs. The table and its collection * of legs form a composition. */ public class Table extends Furniture implements Comparable<Table> { private Set<Leg> legs; /** * Initializes this table with the given brand and the * given collection of legs. * * @param brand the brand of the table. * @pre. brand != null * @param legs the legs of the table. * @pre. legs != null */ public Table(String brand, Set<Leg> legs) { super(brand); this.setLegs(legs); } /** * Returns the legs of this table. * * @return the legs of this table. */ public Set<Leg> getLegs() { return new HashSet<Leg>(this.legs); } /** * Sets the legs of this table to the given collection of legs. * * @param legs the legs of this table. * @pre. legs != null */ public void setLegs(Set<Leg> legs) { this.legs = new HashSet<Leg>(legs); } /** * Tests for equality of this table and the given other object. * Two tables are equal if they have the same brand and the * same number of legs. * * @param object Another object. * @return true if this table and the other are the same, false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Table other = (Table) object; equal = super.equals(other) && this.getLegs().size() == other.getLegs().size(); } else { equal = false; } return equal; } /** * Compares this table with the given table. * This table is smaller than the other table if its unique id * is smaller than the unique id of the other table. * * @param other Another table. * @return (unique id of this table) - (unique id of other table). * @throws NullPointerException if other is null. */ public int compareTo(Table other) { return Integer.parseInt(this.toString()) - Integer.parseInt(other.toString()); } }