문제

ClassCastException을 던지는 대신 유형 캐스트가 널 값을 반환하는 것이 실용적인 상황이 있습니다. C#은 다음과 같습니다 as 이 작업을 수행하는 운영자. Java에서 사용할 수있는 것이 있으므로 ClassCastException을 명시 적으로 확인할 필요가 없습니까?

도움이 되었습니까?

해결책

다음은 @omar kooheji가 제안한 AS의 구현입니다.

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

다른 팁

나는 당신이 당신의 자신을 굴려야한다고 생각합니다.

return (x instanceof Foo) ? (Foo) x : null;

편집 : 클라이언트 코드가 Nulls를 처리하는 것을 원하지 않으면 널 객체

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
    }
}

당신은 사용할 수 있습니다 instanceof C#대신 키워드 is, 그러나 같은 것은 없습니다 as.

예시:

if(myThing instanceof Foo) {
   Foo myFoo = (Foo)myThing; //Never throws ClassCastException
   ...
}

이와 같은 정적 유틸리티 방법을 쓸 수 있습니다. 나는 그것이 읽기 쉬운 것이라고 생각하지 않지만, 그것은 당신이하려는 일의 가장 근사치입니다. 정적 가져 오기를 사용하면 가독성 측면에서 나쁘지 않습니다.

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;
    }
}

다음은 작동 방식을 보여주는 테스트 사례가 있습니다 (그리고 그것이 작동한다는 것을 보여줍니다.)

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);
    }
}

Java 8에서는 선택 사항과 함께 스트림 구문을 사용할 수도 있습니다.

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"));

나는 당신이 아마도 연산자로 주름을 줄 수 있다고 추측합니다

같은 것

as<T,Type> (left, right)  
which evaluates to 
if (typeof(left) == right)
   return (right)left
else
    return null

나는 당신이 어떻게할지 잘 모르겠습니다. 나는 현재 AC#이고 대학을 떠난 후 내 Java 모자가 약간 먼지가 많았습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top