Reputation: 20966
Is there any implementatin of as operator in Java?
I'm looking something as simple as following snippet of code in some shared library.
public static <T> T as(Class<T> clazz, Object object) {
if (object == null)
return null;
if (clazz.isAssignableFrom(object.getClass()))
return (T) object;
return null;
}
Something I could use like
public boolean equals(Object obj) {
Peer peer = as(Peer.class, obj);
if (peer == null) return false;
...
}
Upvotes: 0
Views: 30
Reputation: 73558
No, the equivalent idiom in Java would be
public boolean equals(Object obj) {
if(!obj instanceof Peer)
return false;
Peer peer = (Peer)obj;
...
}
Upvotes: 3