'Identify variable data type at Runtime java
I have a property class which has some variables(for example, name, age, percentage, freeClasses, etc.). Their type can be boolean, double, integer, string, an id mapping the property class (id is an integer) and I have to store the values of id and its properties with following constraints on property class variables.
- datatype has to be identified at run time.
- The datatype of the property class variables is defined at the first insert.
- Once the datatype of a particular variable is identified, it cannot be changed. For example, freeClasses when defined takes type = boolean, hence, any id when using the property freeClasses must allow only boolean values on subsequent inserts/updates.
Note: I am using an in-memory database
I am not able to find anything related to the above problem.
Edit: I am not sure how using the Object type for each variable will solve the 3rd point requirement,
Below is the implementation that I have done for the above 3 requirements.
I am calling the allArgs constructor of the Attribute class from my service class.
@Getter
public class Attribute<A, B>{
private A name;
private B freeClasses;
private Attribute(){}
public Attribute(A name, B freeClasses) {
if (name != null) {
setName(name);
}
if (freeClasses != null) {
setFreeClasses(freeClasses);
}
}
public void setFreeClasses(B freeClasses) {
if (checkType(freeClasses, freeClassesType)) {
freeClassesType = freeClasses.getClass().getGenericSuperclass();
this.freeClasses = freeClasses;
}else {
this.throwException("freeClasses");
}
}
public void setName(A name) {
if (checkType(name, nameType)){
nameType = name.getClass().getGenericSuperclass();
this.name = name;
} else{
this.throwException("name");
}
}
@SneakyThrows
private void throwException(String attribute) {
throw new InputMismatchException("Incorrect datatype provided for field: " + attribute);
}
private <T> boolean checkType(T attributeField, Type type){
return type == null || Objects.equals(attributeField.getClass().getGenericSuperclass(), type);
}
enter code here
Solution 1:[1]
If I understood you well, I think you can use a builder or factory for entities. Nevertheless, you should think in other approarch different from dynamic typing not using Object if possible.
Something you could try is as following:
class Entity<
A extends Serializable,
B extends Serializable,
C extends Serializable
> {
private A attribute1;
private B attribute2;
private C attribute3;
public Entity(
A attribute1,
B attribute2,
C attribute3
) {
this.attribute1 = attribute1;
this.attribute2 = attribute2;
this.attribute3 = attribute3;
}
...getters...
public String toString() {
return attribute1.toString() + "," + attribute2.toString() + "," + attribute3.toString();
}
}
class EntityBuilder<
A extends Serializable,
B extends Serializable,
C extends Serializable
> {
private Class<A> type1;
private Class<B> type2;
private Class<C> type3;
public EntityBuilder(
Class<A> type1,
Class<B> type2,
Class<C> type3
) {
this.type1 = type1;
this.type2 = type2;
this.type3 = type3;
}
public Entity<A, B, C> buildFrom(
Serializable value1,
Serializable value2,
Serializable value3
) {
return new Entity<A, B, C>(
type1.cast(value1),
type2.cast(value2),
type3.cast(value3)
);
}
}
And then, something like
public class BuildingEntities {
public static void main(String... args) {
Serializable[][] values = new Serializable[][] {
new Serializable[] {1, false, "description1"},
new Serializable[] {2, true, "description2"},
new Serializable[] {3, false, "description3"},
new Serializable[] {4, true, "description4"},
new Serializable[] {5, false, "description5"}
};
EntityBuilder<
? extends Serializable,
? extends Serializable,
? extends Serializable
> builder = new EntityBuilder<>(
values[0][0].getClass(),
values[0][1].getClass(),
values[0][2].getClass()
);
for (Serializable[] entityValues : values) {
Entity<?, ?, ?> entity = builder.buildFrom(
entityValues[0],
entityValues[1],
entityValues[2]
);
System.out.println(entity);
}
}
}
Printing out
1,false,description1
2,true,description2
3,false,description3
4,true,description4
5,false,description5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
