title: Check if only specific properties of 2 objects are equal
body: <p>Say I have a a class <code>Person</code> (assume all of the properties can be set and can also be null):</p>
  
  <pre><code>public class Person {
    private String firstName;
    private String secondName;
    private Address address;
  
    public String getFirstName() { return firstName; }
    public String getSecondName() { return secondName; }
    public String getAddress() { return address; }
  }
  </code></pre>
  
  <p>If I have two instances of <code>Person</code>, and I want to check if they both have the same <code>firstName</code> and <code>secondName</code>, I cannot simply call <code>equals()</code> as that will also check <code>address</code> for equality (and any other properties for that matter).</p>
  
  <p>I would have to write a function like this:</p>
  
  <pre><code>boolean areNamesEqual(Person person1, Person person2) {
    if(person1 == null || person2 == null) {
      return person1 == person2;
    }
    if(person1.getFirstName() != null ? !person1.getFirstName().equals(person2.getFirstName()) : person2.getFirstName() != null) {
      return false;
    }
    if(person1.getSecondName() != null ? !person1.getSecondName().equals(person2.getSecondName()) : person2.getSecondName() != null) {
      return false;
    }
    return true;
  }
  </code></pre>
  
  <p>Is there any cleaner way to express this? It feels like Java is making jump through quite a few hoops to express this simple idea. I have started looking at Google Guava, and have seen I can use <code>Objects.equal()</code> to improve matters:</p>
  
  <pre><code>boolean areNamesEqual(Person person1, Person person2) {
    if(person1 == null || person2 == null) {
      return person1 == person2;
    }
    if(Objects.equal(person1.getFirstName(), person2.getFirstName())) {
      return false;
    }
    if(Objects.equal(person1.getSecondName(), person2.getSecondName())) {
      return false;
    }
    return true;
  }
  </code></pre>
  
  <p>But I still have to check for the Person objects themselves being null, and write getFirstName() and getSecondName() twice each. It feels like there must be a better way expressing this.</p>
  
  <p>Code like this would be ideal:</p>
  
  <pre><code>(person1, person2).arePropertiesEqual(firstName, secondName)
  </code></pre>
  
  <p>With this I don't have check for null anywhere, I don't have to return early, and I don't have to write <code>firstName</code> or <code>secondName</code> more than once.</p>
  
  <p>Any ideas?</p>