1

I have this scenario...

public enum ParametrosAplicacion{
    BOOLEAN("BOOLEANO", "Valor verdadero o falso", Boolean.class, false),
    FILE("PROPIEDAD", "DESCRIPCION", File.class, new File("ruta"));
    private final String propiedad, descripcion;
    private final Class<?> clase;
    private Object valor;
    private ParametrosAplicacion(String p, String d, Class<?> c, Object v){
        this.propiedad=p;
        this.descripcion=d;
        this.clase=c;
        this.valor=v;
    }
    public Class<?> getClase(){
        return this.clase;
    }
    public <V> V getValor(Class<V> clase){
        return clase.cast(valor);
    }
    public static void main(String[] args){
        Boolean sw=ParametrosAplicacion.BOOLEAN.getValor(Boolean.class);
        if(sw){
            System.out.println(sw.getClass().getSimpleName());
        }else{
            File file=ParametrosAplicacion.FILE.getValor(File.class);
            System.out.println(file.getClass().getSimpleName());
        }
    }
}

I am finding for some way to get values a this way...

Boolean sw=ParametrosAplicacion.BOOLEAN.getValor();

Some thing like...

public <V> V getValor(){
    return this.clase.cast(this.valor); // not compile!!!
}

Of course I need to skip any unchecked warning, so....

public <V> V getValor(){
    return (V)(valor);
}

Is not a good solution...

Thanks a lot...

Tilens
  • 109
  • 7
  • Do you actually need the "enum"-ness of this enum as from the non-instantiability (as in, the serializability, the resistance to reflective instantiation, `getValues()` etc), or could you just have some non-instantiable values? – Andy Turner Sep 08 '17 at 14:07
  • I think its not exactly the same thus i have the `Class` ready to parse, so i think not `Inteface`is necesary, only I dont know how to correctly implement the solution... If it is posible... – Tilens Sep 08 '17 at 14:19
  • Do you meant instantiability outside `ParametrosAplicacion`... – Tilens Sep 08 '17 at 14:26
  • I mean like with enums: you've got the values `BOOLEAN`, `FILE`, but you can't create any more at runtime. – Andy Turner Sep 08 '17 at 14:28
  • Yes correct! I create `ParametrosAplicacion` as `Constants` on development... Only a `setValue(V valor)` is planed, to load values from `Properties` object – Tilens Sep 08 '17 at 14:30

0 Answers0