For my android application, I want to read the temperature of my raspberry pi. From the server, the variable is returned as a double. From the XML, it is returned as a String. Therefore I thought why wouldn't I make a generic setter that can set the value if it is a String or double.
@Bindable
public String getCPUTemperature(){
return Double.toString(mCPUTemp);
}
/**
* Sets the CPU temperature for the device view model.
* @param <T> The generic parameter T accepts a String as wel as a Double to set the available memory.
*/
public <T> void setCPUTemperature(T CPUTemp){
if(CPUTemp.getClass().equals(String.class))
this.mCPUTemp = Double.parseDouble((String)CPUTemp);
else if(CPUTemp.getClass().equals(Double.class))
this.mCPUTemp = (double)CPUTemp;
notifyPropertyChanged(BR.cPUTemperature);
}
But after I created this setter I found out that:
Note: Arrays and a generic type, such as the Observable class, might display errors when there are no errors.
So I started to wonder. Is it possible to solve it with a generic variable and if so, is that the best way of solving it?