pkt1583 / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Missing utility method in Predicates to compare identity of objects #277

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The Predicates utility class is missing a method that compares the
'identity' of objects.
Similar to the equalTo method a 'sameAs' utility method could be added to
the Predicates utility class.

This new 'sameAs' method could be easily be implements as follows:
------------------------------------------------------------------

  /**
   * Returns a predicate that evaluates to {@code true} if the object being
   * tested has the same identity (meaning is exactly the same object
   * instance) as the given target or both are null.
   */
  public static <T> Predicate<T> sameAs(@Nullable T target) {
    // TODO: Change signature to return Predicate<Object>.
    return (target == null)
        ? Predicates.<T>isNull()
        : new IsSameAsPredicate<T>(target);
  }

  private enum IsSameAsPredicate implements Predicate<Object> {
    private final Object target;

    public boolean apply(Object o) {
      return target == o;
    }
    @Override public String toString() {
      return "IsSameAs";
    }
  }

Original issue reported on code.google.com by emil.van...@logica.com on 26 Oct 2009 at 6:13

GoogleCodeExporter commented 9 years ago
It's missing on purpose.  We had it internally to Google for a long time, and 
then we 
reviewed its usages.

First, it had not very many usages at all. Second, nearly every single one of 
those 
usages would have been perfectly fine (or better off) using 
Predicates.equalTo(). 
There may have been ONE case where we modified the code to spell out the 
predicate 
longhand.

In short, it's too rare to actually need this, and it's slightly less rare to 
think 
you need it when you don't.  It doesn't belong in Predicates.

Original comment by kevin...@gmail.com on 26 Oct 2009 at 6:19