コード例 #1
0
  /**
   * An equals method for ObjectWatcher that compares the HashMap and LinkedList. This will need to
   * be updated if more functionality is added.
   *
   * @param obj : the object for comparison
   * @return : a boolean value true or false
   */
  public boolean equals(Object obj) {
    if (this == obj) {
      return true; // The same object
    }
    if (!(obj instanceof ObjectWatcher)) {
      return false; // Not of the same type
    }

    ObjectWatcher other = (ObjectWatcher) obj;

    if (!(itemMap.equals(other.getItemMap()))) {
      // If the HashMaps aren't equal, return false
      return false;
    } else if (!(playerList.equals(other.getPlayerList()))) {
      // If the lists aren't equal, return false
      return false;
    } else {
      // If the hash maps and lists are equal, return true
      return true;
    }
  }
コード例 #2
0
  /**
   * This is the clone method for ObjectWatcher. It overrides the clone() method of object, and is
   * meant to be overridden where needed by subclass methods.
   */
  @Override
  public ObjectWatcher clone() {
    try {
      ObjectWatcher copy = (ObjectWatcher) super.clone();

      // new HashMap
      copy.itemMap = new HashMap<Integer, ArrayList<Item>>();

      Set<Integer> itemKeys = itemMap.keySet();

      // copy the mapped elements of itemMap
      for (Integer iLoc : itemKeys) {
        ArrayList<Item> newList = new ArrayList<Item>();

        ArrayList<Item> tempList = itemMap.get(iLoc);

        // copy all elements of tempList to newList
        for (int i = 0; i < tempList.size(); i++) {
          newList.set(i, tempList.get(i).clone());
        }

        // put the element of itemMap in the copy's itemMap
        copy.itemMap.put(iLoc, newList);
      }
      // End for loop for itemMap elements

      // new playerList
      copy.playerList = new LinkedList<Integer>();

      // copy of all elements of playerList
      for (int i = 0; i < playerList.size(); i++) {
        copy.playerList.set(i, playerList.get(i));
      }

      return copy;
    } catch (CloneNotSupportedException e) {
      throw new InternalError();
    }
  }