code: public static <T> T castIfSameClass(Object o, Class<T> c) {
          return o != null && o.getClass() == c ? c.cast(o) : null;
      }
docs: Conditional casting, useful for typical equals() implementations
  
  @param o   The other object
  @param c   The class to check for
  @param <T> The generic type of the target class
  @return The object cast as a T, or null if o.getClass() is not c