database - Java: Reading data with type that isn't known in advance -
i'm working on project in have read bunch of features (properties) database (which in form of csv file).
this how file looks (formatted bit easier readability):
isfast; speed; isred; r; g; b t; 123.4; f; 0.0; 0.0; 1.0 f; 21.3; t; 1.0; 0.0; 0.0 ...
as can see, t/f represent boolean values, while numbers floating point values. properties may later added or removed.
in code need read these values each instance (row) , pass them other code processes data further.
the way i'm dealing have following classes:
public abstract class feature<t> { public final string name; public final t value; public feature(string name, t value) { this.name = name; this.value = value; } public abstract boolean test(t value); } public class boolfeature extends feature<boolean> { public boolfeature(string name, boolean value) { super(name, value); } @override public boolean test(boolean value) { return this.value; } } public class doublefeature extends feature<double> { public doublefeature(string name, double value) { super(name, value); } @override public boolean test(double value) { return this.value < value; } }
i'm parsing csv file , creating boolfeature
objects input that's t/f , doublefeature
s others, saving in list<feature>
or feature[]
, , passing collection around.
this results in lot of
feature raw type. references generic type feature should parameterized
and
type safety: method test(object) belongs raw type feature. references generic type feature should parameterized
in code like:
public abstract class metric { protected list<feature> features; // line gives warning #1 above public metric(list<feature> features) { this.features = features; } public double getsplitquality(split split) { return getimpurity(split.yes) + getimpurity(split.no); } public abstract double getimpurity(list<instance> instances); } public class split { public final list<instance> yes; public final list<instance> no; public split(list<instance> instances, feature feature) { yes = new arraylist<>(); no = new arraylist<>(); (instance inst : instances) if (inst.features.get(feature.name).test(feature.value)) // line gives warning #2 above yes.add(inst); else no.add(inst); } } public class instance { public hashmap<string, feature> features = new hashmap<>(); public string output = null; public instance(string[] featurenames, string[] csvrow) { /* parse csv row, creating boolfeatures , doublefeatures , adding them hashmap, example: */ if (csvrow[0].equals("t") || csvrow[0].equals("f")) features.put(featurenames[0], new boolfeature(featurenames[0], csvrow[0].equals("t"))); } }
is there cleaner way deal problem this?
edit
after adding <?>
references feature
, per answer, line gave warning #2 gives compile time error:
the method test(capture#2-of ?) in type feature not applicable arguments (capture#3-of ?)
your list<feature>
uses raw-type feature
(as warning says). if know subtype feature
has can use generic type e.g list<feature<boolean>>
.
however if want list<feature
contain boolfeature
, doublefeature
can make use of wildcard-type list<feature<?>>
Comments
Post a Comment