java - Safe to Cast when method depends on higher level interface - Stack Overflow

admin2025-04-20  0

Consider the code below:

public class DummySupplier<T> implements Supplier<T> {

    private final Supplier<T> supplier;

    public static<T> DummySupplier<T> of(Supplier<T> supplier) {
        return new DummySupplier<>(supplier);  
    }

    private DummySupplier(Supplier<T> supplier) {
        this.supplier = supplier;
    }

    @Override
    public T get() {
        return supplier.get();
    }
}

public class Main {

    public static void main(String[] args) {
        DummySupplier<String> stringSupplier = DummySupplier.of(() -> "Hello, World!");

        // Calling function with multiple arguments
        processSuppliers(42, "Test", stringSupplier, 3.14);
    }

    //Method getting Object... args
    public static void processSuppliers(Object... args) {
        for (Object arg : args) {
            Supplier<?> supplier;
            if (arg instanceof Supplier<?>) {
                arg = arg.get();
            }

            System.out.println("Supplied: " + arg.toString());
        }
    }
}

Will it be safe just to typecast the input argment to Supplier<T>.of to DummySupplier<T> rather than creating a new object of DummySupplier? The other functions which are using depends only on Supplier, not on DummySupplier.

Why DummySupplier? Because other methods are accepting Object ...args. Here, I want to pass Supplier. Now, that method is taking care of Supplier<?>. Using Supplier lambda, I cannot pass as Object as an argument.

Consider the code below:

public class DummySupplier<T> implements Supplier<T> {

    private final Supplier<T> supplier;

    public static<T> DummySupplier<T> of(Supplier<T> supplier) {
        return new DummySupplier<>(supplier);  
    }

    private DummySupplier(Supplier<T> supplier) {
        this.supplier = supplier;
    }

    @Override
    public T get() {
        return supplier.get();
    }
}

public class Main {

    public static void main(String[] args) {
        DummySupplier<String> stringSupplier = DummySupplier.of(() -> "Hello, World!");

        // Calling function with multiple arguments
        processSuppliers(42, "Test", stringSupplier, 3.14);
    }

    //Method getting Object... args
    public static void processSuppliers(Object... args) {
        for (Object arg : args) {
            Supplier<?> supplier;
            if (arg instanceof Supplier<?>) {
                arg = arg.get();
            }

            System.out.println("Supplied: " + arg.toString());
        }
    }
}

Will it be safe just to typecast the input argment to Supplier<T>.of to DummySupplier<T> rather than creating a new object of DummySupplier? The other functions which are using depends only on Supplier, not on DummySupplier.

Why DummySupplier? Because other methods are accepting Object ...args. Here, I want to pass Supplier. Now, that method is taking care of Supplier<?>. Using Supplier lambda, I cannot pass as Object as an argument.

Share Improve this question edited Mar 3 at 8:07 doptimusprime asked Mar 3 at 7:18 doptimusprimedoptimusprime 9,4116 gold badges56 silver badges94 bronze badges 5
  • 2 Please edit your post - I suspect you don't actually have a blank line after each real line, and wouldn't put up with working the code as it's presented here - so please don't ask other people to do so either. – Jon Skeet Commented Mar 3 at 7:21
  • 3 "Will it be safe just to typecast the input argment" - the input argument to what? You've presented a constructor and a method, but you haven't clarified which one you're proposing on changing. It's still not clear to me why this exists - if you've already got to provide a supplier, what's the benefit in having a whole other implementation? (And how would you expect to cast something to DummySupplier<> if you never create an instance of it?) Showing a minimal reproducible example of how you're using this and why you think it's worth implementing Supplier<> yourself would make the question clearer. – Jon Skeet Commented Mar 3 at 7:24
  • @JonSkeet Updated. It is the input argument of – doptimusprime Commented Mar 3 at 7:58
  • @JonSkeet Demo code: ideone/CoUDGA – doptimusprime Commented Mar 3 at 8:08
  • 1 did you mean "typecast the input argument from Supplier<T> to DummySupplier<T>"? (instead of "typecast the input argment to Supplier<T>.of to DummySupplier<T>" - emphasis added) - that is, something like return (DummySupplier<T>) supplier; ? If so, the answer depends on your definition of "safe" (will it compile, will it always work without Exception, will it create code that recognizes something went wrong, ...) – user85421 Commented Mar 3 at 8:11
Add a comment  | 

1 Answer 1

Reset to default 3

It seems to me like you don't need a DummySupplier<> at all. Just change your main method to:

public static void main(String[] args) {
    Supplier<String> stringSupplier = () -> "Hello, World!";
    // Calling function with multiple arguments
    processSuppliers(42, "Test", stringSupplier, 3.14);
}

That works fine. You were already depending on the conversion from a lambda expression to a Supplier<> - there's no need to create another implementation to wrap that.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745105730a285277.html

最新回复(0)