/** * Indicates whether some other object is "equal to" this one which in terms of contact groups * translates to having the equal names and matching subgroups and child contacts. The resolved * status of contactgroups and contacts is deliberately ignored so that groups and/or contacts * would be assumed equal even if it differs. * * <p> * * @param obj the reference object with which to compare. * @return <code>true</code> if this contact group has the equal child contacts and subgroups to * those of the <code>obj</code> argument. */ @Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof MockContactGroup)) return false; MockContactGroup mockGroup = (MockContactGroup) obj; if (!mockGroup.getGroupName().equals(getGroupName()) || !mockGroup.getUID().equals(getUID()) || mockGroup.countContacts() != countContacts() || mockGroup.countSubgroups() != countSubgroups()) return false; // traverse child contacts Iterator<Contact> theirContacts = mockGroup.contacts(); while (theirContacts.hasNext()) { MockContact theirContact = (MockContact) theirContacts.next(); MockContact ourContact = (MockContact) getContact(theirContact.getAddress()); if (ourContact == null || !ourContact.equals(theirContact)) return false; } // traverse subgroups Iterator<ContactGroup> theirSubgroups = mockGroup.subgroups(); while (theirSubgroups.hasNext()) { MockContactGroup theirSubgroup = (MockContactGroup) theirSubgroups.next(); MockContactGroup ourSubgroup = (MockContactGroup) getGroup(theirSubgroup.getGroupName()); if (ourSubgroup == null || !ourSubgroup.equals(theirSubgroup)) return false; } return true; }
/** * Returns the contact with the specified id or null if no such contact exists. * * @param id the id of the contact we're looking for. * @return MockContact */ public MockContact findContactByID(String id) { // first go through the contacts that are direct children. Iterator<Contact> contactsIter = contacts(); while (contactsIter.hasNext()) { MockContact mContact = (MockContact) contactsIter.next(); if (mContact.getAddress().equals(id)) return mContact; } // if we didn't find it here, let's try in the subougroups Iterator<ContactGroup> groupsIter = subgroups(); while (groupsIter.hasNext()) { MockContactGroup mGroup = (MockContactGroup) groupsIter.next(); MockContact mContact = mGroup.findContactByID(id); if (mContact != null) return mContact; } return null; }
/** * Adds the specified contact to this group. * * @param contactToAdd the MockContact to add to this group. */ public void addContact(MockContact contactToAdd) { this.contacts.add(contactToAdd); contactToAdd.setParentGroup(this); }