简体   繁体   中英

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. They each accept a BufferedImage and an intensity (Double), and return a new 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. 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.

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. If a misspelling causes code to misfire, it's irresponsible code. I would prefer to just use a Singleton there, like a grown up.

So, in summary, is it possible in Java to bind a parameter type to the generic of another parameter? If so, how? If not, does anyone know of any plans to extend Java Generics in the future, in such a manner?

Thanks for any input!

Yes it's possible all we have to do is define the type parameter on the method itself. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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