title: Combine 3rd party enum with function
body: <p>I have 3rd party enum declaration:</p>
  <pre><code>enum Code {
    COUNTRY, STATE, CITY, ... 
  }
  </code></pre>
  <p>My object:</p>
  <pre><code>public class A {
    String country;
    String state;
    String city;
    ...
  }
  </code></pre>
  <p>I need to associate this enum with my object state changes. Something like:</p>
  <pre><code>public Set&lt;Code&gt; getChangedValuesCodes(A oldA, A newA) {
    Set&lt;Code&gt; codes = new HashSet&lt;&gt;();
    getCountryCode(oldA, newA).ifPresent(codes::add);
    getStateCode(oldA, newA).ifPresent(codes::add);
    getCityCode(oldA, newA).ifPresent(codes::add);
    return codes;
  }
  
  private Optional&lt;Code&gt; getCountryCode(A oldA, A newA) {
    return getCode(oldA.getCountry(), newA.getCountry(), Code.COUNTRY);
  }
  
  private Optional&lt;Code&gt; getStateCode(A oldA, A newA) {
    return getCode(oldA.getState(), newA.getState(), Code.STATE);
  }
  
  private Optional&lt;Code&gt; getCityCode(A oldA, A newA) {
    return getCode(oldA.getCity(), newA.getCity(), Code.CITY);
  }
  
  private Optional&lt;Code&gt; getCode(Object oldObj, Object newObj, Code code) {
    return !oldObj.equals(newObj) &amp;&amp; newObj != null
      ? of(code)
      : empty();
  }
  </code></pre>
  <p>Problem is that there are a lot of codes and <code>A</code> object is pretty big.
  Having a class looks a little messy too me.</p>
  <p>I am thinking about association through the enum.</p>
  <pre><code>public enum ACode {
    COUNTRY(Code.COUNTRY) {
      @Override
      boolean isChanged(A oldA, A newA) {
        return ACode.isPropertyChanged(oldA.getCountry(), newA.getCountry());
      }
    },
    CITY(Code.CITY) {
      @Override
      boolean isChanged(A oldA, A newA) {
        return ACode.isPropertyChanged(oldA.getCity(), newA.getCity());
      }
    },
    STATE(Code.STATE) {
      @Override
      boolean isChanged(A oldA, A newA) {
        return ACode.isPropertyChanged(oldA.getState(), newA.getState());
      }
    };
  
    @Getter
    private final Code code;
  
    ACode(Code code) {
      this.code = code;
    }
  
    public static Set&lt;Code&gt; getChangedValuesCodes(A oldA, A newA) {
      return Arrays.stream(ACode.values())
          .filter(code -&gt; code.isChanged(oldA, newA))
          .map(ACode::getCode)
          .collect(Collectors.toSet());
    }
  
    abstract boolean isChanged(A oldA, A newA);
  
    private static boolean isPropertyChanged(Object oldObject, Object newObject) {
      return !oldObject.equals(newObject) &amp;&amp; newObject != null;
    }
  }
  </code></pre>
  <p>This one looks better than upper one but still clumsy as I am not technically using this enum - more like a connector of enum and the function.</p>
  <p>How could I achieve a clean association between this enum and the function in order to get all changed values codes?</p>