How to create a Enum with a abstract method that returns a generic type that can be a Functional Interface like functions, consumers, suppliers

huangapple go评论65阅读模式
英文:

How to create a Enum with a abstract method that returns a generic type that can be a Functional Interface like functions, consumers, suppliers

问题

以下是翻译好的部分:

我有一个枚举其中有一个字段```value```和一个抽象方法该方法返回一个可以是函数接口的泛型类型以便每个常量都具有特定的行为

public enum TestEnum {

    PERIOD("PERIOD") {
        @Override
        public <T> T operation() {
            return () -> Map.of(
                    "INIT", LocalDate.now()
                            .minusDays(1L)
                            .with(TemporalAdjusters.firstDayOfMonth())
                            .minusMonths(1L),
                    "END", LocalDate.now()
                            .minusDays(1L)
                            .minusMonths(1L)
                            .with(TemporalAdjusters.lastDayOfMonth())
            );
        }
    };

    private final String value;

    public abstract <T> T operation();
}

在这种情况下,可以返回一个供应商,但我有其他情况可能需要返回FunctionBiConsumers

这样做时,我收到一个需要类型T但我提供了一个lambda表达式的错误。是否有方法可以做到这一点?

英文:

I have a Enum that have a field value and a abstract method that returns a generic type that can be a functional interface to each constant will have your particular behavior.

public enum TestEnum {

    PERIOD(&quot;PERIOD&quot;) {
        @Override
        public &lt;T&gt; T operation() {
            return () -&gt; Map.of(
                    &quot;INIT&quot;, LocalDate.now()
                            .minusDays(1L)
                            .with(TemporalAdjusters.firstDayOfMonth())
                            .minusMonths(1L),
                    &quot;END&quot;, LocalDate.now()
                            .minusDays(1L)
                            .minusMonths(1L)
                            .with(TemporalAdjusters.lastDayOfMonth())
            );
        }
    };

    private final String value;

    public abstract &lt;T&gt; T operation();
}

In this can return a supplier but I have other scenarios that can return a Function, and BiConsumers.

Doing this I'm receiving a error that required type T but I'm providing a lambda expression.

Is there a way to do that?

答案1

得分: 1

使用一般的 Function 可能允许您声明其他任意操作:

public enum TestEnum {
    PERIOD("PERIOD") {
        @Override
        public Function<Void, Map<String, LocalDate>> operation() {
            return xxxNotUsed -> Map.of(
                    "INIT", LocalDate.now()
                            .minusDays(1L)
                            .with(TemporalAdjusters.firstDayOfMonth())
                            .minusMonths(1L),
                    "END", LocalDate.now()
                            .minusDays(1L)
                            .minusMonths(1L)
                            .with(TemporalAdjusters.lastDayOfMonth())
            );
        }
    };
    TestEnum(String x) { value = x;}
    private final String value;

    public abstract <T,R> Function<T, R> operation();
}

请注意,我已经使用 Void 作为 PERIOD 所需的 "Supplier" 风格调用的虚拟参数类型,并且您可以使用 Function<XYZClass, Void> 作为 "Consumer" 风格的操作。

英文:

A version using a general Function might allow you to declare other arbitrary operations:

public enum TestEnum {
    PERIOD(&quot;PERIOD&quot;) {
        @Override
        public Function&lt;Void,Map&lt;String,LocalDate&gt;&gt; operation() {
            return xxxNotUsed -&gt; Map.of(
                    &quot;INIT&quot;, LocalDate.now()
                            .minusDays(1L)
                            .with(TemporalAdjusters.firstDayOfMonth())
                            .minusMonths(1L),
                    &quot;END&quot;, LocalDate.now()
                            .minusDays(1L)
                            .minusMonths(1L)
                            .with(TemporalAdjusters.lastDayOfMonth())
            );
        }
    };
    TestEnum(String x) { value = x;}
    private final String value;

    public abstract &lt;T,R&gt; Function&lt;T,R&gt; operation();
}

Note that I've used Void as a dummy parameter type for the "Supplier" style of call you wanted for PERIOD, and you could use Function&lt;XYZClass,Void&gt; for "Consumer" style of operation.

huangapple
  • 本文由 发表于 2023年6月21日 23:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定