Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Takes two persons and looks at their friends. If p1 is befriended with p2,
- * or if p1 knows someone who is befriended with p2 over any number of friends,
- * returns true.
- * @param p1 The first Person
- * @param p2 The second Person
- * @return Returns whether p1 and p2 are connected over a chain of friendships.
- */
- public boolean areConnected(Person p1, Person p2) {
- boolean areConnected = false;
- if (p1 != null && p2 != null) {
- PersonProfile from = getProfile(p1);
- PersonProfile to = getProfile(p2);
- areConnected = areConnected(from, to, null);
- }
- return areConnected;
- }
- /**
- * Recursive working method for areConnected(Person, Person)
- * @param from Profile to find a chain from.
- * @param to Profile to find a chain to.
- * @param master 'from' from the next higher instance. Is null on first call.
- * @return Returns whether from and to are connected.
- */
- private boolean areConnected(PersonProfile from, PersonProfile to, PersonProfile master) {
- boolean areConnected = false;
- if (from.isBefriendedWith(to)) {
- areConnected = true;
- } else {
- PersonProfile[] friends = from.getFriends();
- for (int i = 0; i < friends.length && areConnected == false; i++) {
- if (!(friends[i] == master || friends[i].isBefriendedWith(master))) {
- areConnected = areConnected(friends[i], to, from);
- }
- }
- }
- return areConnected;
- }
Add Comment
Please, Sign In to add comment