[英]Can a Java argument be type-bound by a Class contained in another object?
NOTE: This question is primarily theoretical; 注意:这个问题主要是理论上的; I've since given up on using this immediately , as everything I've thought up overcomplicates my code and smells a little like an antipattern. 从那以后,我就放弃了立即使用它,因为我想过的所有事情都使我的代码过于复杂,并且闻起来有点像反模式。 However, I find it interesting in theory and would love the community's help with it. 但是,我认为它在理论上很有趣,并且希望社区对此提供帮助。
I'm refurbing some code written around the turn of the century for image manipulation; 我正在整理一些世纪之交编写的用于图像处理的代码; beginning by chopping apart a single class with a lot of redundancies into a number of BiFunction filters. 首先将具有很多冗余的单个类切分成多个BiFunction过滤器。 They each accept a BufferedImage and an intensity (Double), and return a new BufferedImage. 它们每个接受一个BufferedImage和一个强度(Double),然后返回一个新的BufferedImage。 Of course, a lot of these image filters have additional concerns, like radius-of-effect or another intensity, and if I'm going to chain them efficiently, I would like to be able to set these as "uniforms" (or quasi-constants) before use. 当然,这些图像滤镜中的许多还有其他问题,例如效应半径或其他强度,如果我要高效地链接它们,我希望能够将它们设置为“均匀”(或准)。 -常数)。
My current method is simply to extend my ImageFilter class further, and set the uniforms as Bean-style properties. 我当前的方法只是简单地进一步扩展ImageFilter类,并将统一设置为Bean样式的属性。 What I initially thought of doing was constraining them to a generic class that was held in an interface, and having something along the lines of: 我最初想做的是将它们约束为一个通用类,该类保存在一个接口中,并且具有以下特点:
public void addProperty(Uniform<T> property, T type)
in there, where T is specified entirely by the Uniform. 在这里,T完全由Uniform指定。
The original class kept all of these uniforms in a HashMap by String name, but that's the beginning of a lot of bad habits and I clean it up every time I see it. 原始的类将所有这些制服按String名称保留在HashMap中,但这是许多不良习惯的开始,每次我看到它时,我都会对其进行清理。 If a misspelling causes code to misfire, it's irresponsible code. 如果拼写错误导致代码失误,那是不负责任的代码。 I would prefer to just use a Singleton there, like a grown up. 我宁愿像长大成人一样只在那儿使用Singleton。
So, in summary, is it possible in Java to bind a parameter type to the generic of another parameter? 因此,总而言之,在Java中是否可以将参数类型绑定到另一个参数的泛型? If so, how? 如果是这样,怎么办? If not, does anyone know of any plans to extend Java Generics in the future, in such a manner? 如果不是,是否有人知道将来有任何以这种方式扩展Java泛型的计划?
Thanks for any input! 感谢您的输入!
Yes it's possible all we have to do is define the type parameter on the method itself. 是的,我们可能要做的就是在方法本身上定义type参数。 Check the example below. 检查下面的示例。
public class Uniform { 公共类制服{
}
public class ImageFilter {
//store in a list which can store any type of Uniform
List<Uniform<?>> uniformList = new ArrayList<Uniform<?>>();
//store in a map which can store any type of uniform and uses type as key
Map<Class<?>, Uniform<?>> uniformMap = new HashMap<>();
//Type parameter 'T' on the method itself
public <T> void addProperty(Uniform<T> property, T type) {
uniformList.add(property);
uniformMap.put(type.getClass(), property);
}
}
{
//sample usage
new ImageFilter().addProperty(new Uniform<>(), "test");
new ImageFilter().addProperty(new Uniform<>(), new Double(2.0));
}
Following is sample usage 以下是示例用法
new ImageFilter().addProperty(new Uniform<>(), "test");
//or
new ImageFilter().addProperty(new Uniform<String>(), "test");
//if for some reason JVM can't infer the type itself, it can be set manually
new ImageFilter().<String>addProperty(new Uniform<String>(), "test");
See this link for more information. 有关更多信息,请参见此链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.