简体   繁体   English

在Java 8中创建方法引用数组的简写方法?

[英]Shorthand method for creating array of method references in Java 8?

I'm using Wicket 6/Java 8 and am adding some simple classes that make use of the lambda functionality from Java 8 (I know the later versions of Wicket have lambda support but we can't upgrade right now). 我正在使用Wicket 6 / Java 8并且正在添加一些使用Java 8中的lambda功能的简单类(我知道Wicket的更高版本具有lambda支持但我们现在无法升级)。 I'm creating a LambdaModel that is a bit like the PropertyModel, which I'm hoping will remove the need to hardcode Strings representing the nested path to the property. 我正在创建一个有点像PropertyModel的LambdaModel,我希望它不需要硬编码表示属性的嵌套路径的字符串。

To start, I'm making a simple readonly version. 首先,我正在制作一个简单的只读版本。 Ive made Serializable versions of the Function interface to create the following: 我已经使用Serial接口版本的Function接口来创建以下内容:

public class LambdaModelUtils {
  public static <X,R> IModel<R> ofNested( IModel<X> target, SerializableFunction<?,?>... path ) {
     // creates a model that works through each function in the path in turn
  }
}

My implementation works well, but the only problem is that calling this method the 'efficient' way causes compile errors: 我的实现很好,但唯一的问题是调用此方法是“有效”的方式会导致编译错误:

IModel<Parent> parentModel = ...
IModel<String> model = LambdaModelUtils.ofNested( parentModel,
                            Parent::getChild, Child::getName );  // Compile time error

The only way I can find to call the method is by the following: 我可以找到调用该方法的唯一方法是:

SerializableFunction<Parent,Child> path0 = Parent::getChild;
SerializableFunction<Child,String> path1 = Child::getName;
IModel<String> model = LambdaModelUtils.ofNested( parentModel,
                            path0, path1 );  // works

This is a bit clumsy - is there a better way? 这有点笨拙 - 有更好的方法吗?

Ive looked here but this doesnt seem to work either: 我看过这里,但这似乎也不起作用:

List<SerializableFunction> path = Arrays.asList( Parent::getChild, Child::getName );

Thanks 谢谢

If you're using these functions to get to a nested property, but don't really use the intermediate results, I'd advice you to just use a lambda expression: 如果您使用这些函数来获取嵌套属性,但实际上没有使用中间结果,我建议您只使用lambda表达式:

public static <X,R> IModel<R> ofNested(IModel<X> target, SerializableFunction<X, R> path)

IModel<Parent> parentModel = ...
IModel<String> model = LambdaModelUtils.ofNested(parentModel, p -> p.getChild().getName());

This works since the target type of the lambda is now known, instead of the generic SerializedFunction<?, ?> , you get SerialiedFunction<X, R> where X = Parent and R = String . 这是有效的,因为lambda的目标类型现在已知,而不是通用的SerializedFunction<?, ?> ,你得到SerialiedFunction<X, R> ,其中X = ParentR = String

I tried something similar to your code. 我尝试了类似于你的代码的东西。 Casting the method references to the functional interface type solves the compilation error: 将方法引用强制转换为函数接口类型可以解决编译错误:

IModel<String> model =
    LambdaModelUtils.ofNested(parentModel,
                             (SerializableFunction<Parent,Child>) Parent::getChild, 
                             (SerializableFunction<Child,String>) Child::getName);

Not the prettiest solution, but at least it saves you the need to declare the path0 and path1 variables. 不是最漂亮的解决方案,但至少它可以节省您声明path0path1变量的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM