generics - Can a Java argument be type-bound by a Class contained in another object? -


note: question theoretical; i've since given on using immediately, i've thought overcomplicates code , smells little antipattern. however, find interesting in theory , love community's it.

i'm refurbing code written around turn of century image manipulation; beginning chopping apart single class lot of redundancies number of bifunction filters. each accept bufferedimage , intensity (double), , return new bufferedimage. of course, lot of these image filters have additional concerns, radius-of-effect or intensity, , if i'm going chain them efficiently, able set these "uniforms" (or quasi-constants) before use.

my current method extend imagefilter class further, , set uniforms bean-style properties. thought of doing constraining them generic class held in interface, , having along lines of:

public void addproperty(uniform<t> property, t type) 

in there, t specified entirely uniform.

the original class kept of these uniforms in hashmap string name, that's beginning of lot of bad habits , clean every time see it. if misspelling causes code misfire, it's irresponsible code. prefer use singleton there, grown up.

so, in summary, possible in java bind parameter type generic of parameter? if so, how? if not, know of plans extend java generics in future, in such manner?

thanks input!

yes it's possible have define type parameter on method itself. check example below.

public class uniform {

} public class imagefilter {     //store in list can store type of uniform     list<uniform<?>> uniformlist = new arraylist<uniform<?>>();      //store in map can store type of uniform , uses type key     map<class<?>, uniform<?>> uniformmap = new hashmap<>();      //type parameter 't' on method     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 sample usage

new imagefilter().addproperty(new uniform<>(), "test");  //or new imagefilter().addproperty(new uniform<string>(), "test"); //if reason jvm can't infer type itself, can set manually new imagefilter().<string>addproperty(new uniform<string>(), "test"); 

see link more information.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -