Come emulare C # come operatore in Java
Domanda
Ci sono situazioni in cui è pratico che un cast di tipo restituisca un valore null invece di lanciare ClassCastException. C # ha l'operatore as
per farlo. C'è qualcosa di equivalente disponibile in Java, quindi non è necessario controllare esplicitamente ClassCastException?
Soluzione
Ecco un'implementazione di as, come suggerito da @Omar Kooheji:
public static <T> T as(Class<T> clazz, Object o){
if(clazz.isInstance(o)){
return clazz.cast(o);
}
return null;
}
as(A.class, new Object()) --> null
as(B.class, new B()) --> B
Altri suggerimenti
Penso che dovresti fare il tuo:
return (x instanceof Foo) ? (Foo) x : null;
EDIT: se non si desidera che il codice client gestisca i valori null, è possibile introdurre un Null oggetto
interface Foo {
public void doBar();
}
class NullFoo implements Foo {
public void doBar() {} // do nothing
}
class FooUtils {
public static Foo asFoo(Object o) {
return (o instanceof Foo) ? (Foo) o : new NullFoo();
}
}
class Client {
public void process() {
Object o = ...;
Foo foo = FooUtils.asFoo(o);
foo.doBar(); // don't need to check for null in client
}
}
Puoi usare la parola chiave instanceof
al posto del di C # è
, ma non c'è niente come come
.
Esempio:
if(myThing instanceof Foo) {
Foo myFoo = (Foo)myThing; //Never throws ClassCastException
...
}
Potresti scrivere un metodo di utilità statica come questo. Non credo sia terribilmente leggibile, ma è la migliore approssimazione di ciò che stai cercando di fare. E se usi le importazioni statiche non sarebbe un male in termini di leggibilità.
package com.stackoverflow.examples;
public class Utils {
@SuppressWarnings("unchecked")
public static <T> T safeCast(Object obj, Class<T> type) {
if (type.isInstance(obj)) {
return (T) obj;
}
return null;
}
}
Ecco un caso di prova che dimostra come funziona (e che funziona.)
package com.stackoverflow.examples;
import static com.stackoverflow.examples.Utils.safeCast;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import org.junit.Test;
public class UtilsTest {
@Test
public void happyPath() {
Object x = "abc";
String y = safeCast(x, String.class);
assertNotNull(y);
}
@Test
public void castToSubclassShouldFail() {
Object x = new Object();
String y = safeCast(x, String.class);
assertNull(y);
}
@Test
public void castToUnrelatedTypeShouldFail() {
Object x = "abc";
Integer y = safeCast(x, Integer.class);
assertNull(y);
}
}
In java 8 puoi anche usare la sintassi dello stream con Opzionale:
Object o = new Integer(1);
Optional.ofNullable(o)
.filter(Number.class::isInstance)
.map(Number.class::cast)
.ifPresent(n -> System.out.print("o is a number"));
Sto ipotizzando che potresti creare un operatore come
qualcosa come
as<T,Type> (left, right)
which evaluates to
if (typeof(left) == right)
return (right)left
else
return null
Non sono sicuro di come lo faresti, al momento sono un c # e il mio cappello Java è diventato un po 'polveroso da quando ho lasciato l'università.