peekFlowVariable Double vs Int

Can I peek a numeric flow variable regardless of whether it is a double or an int?

I thought peeking a flow variable for a double would return correctly regardless of whether the flow variable was a double or an int, but it turns out I need to handle both cases independently.

Here's my code:

try {
	priceProduct = this.peekFlowVariableDouble(nameInputColumn + FLOW_VARIABLE_QUALIFIER); //The Product Price could be a Flow Variable Double or a FlowVariable Int - test both
} catch( NullPointerException | NoSuchElementException e ) {
	try {
		priceProduct = (double) this.peekFlowVariableInt(nameInputColumn + FLOW_VARIABLE_QUALIFIER);
	catch( NullPointerException | NoSuchElementException e ) {}
}

I'm trying to match an input column for "product" with a flow variable for "price". The code above works fine, but it would be more elegant if I could do this in one instruction without the double-nested try-catch blocks.

 

Would a cast to float before this java snippet solve this ?

I don't think so. I need to get the Flow Variable, so I need to first call peekFlowVariableDouble then peekFlowVariableInt. So I would need to cast the Flow Variable itself which I don't think I can do.

You can do something like this:

FlowVariable fvar = getAvailableFlowVariables().get(nameInputColumn + FLOW_VARIABLE_QUALIFIER);
double priceProduct;
switch (fvar.getType()) {
	case STRING:
		//Do string operation - raise exception?
		break;
			
	case DOUBLE:
		//Do Double operation e.g.
		priceProduct = fvar.getDoubleValue();

	case INTEGER:
		//Do integer comparison
		priceProduce = (double)fvar.getIntValue();
		break;
	}

Steve

(You missed a break from the DOUBLE case.)

Thanks - good spot - was copy/pasting from a node model trying to miss out the irrelevant code.  Obviously missed out some of the relevant too.

You might be able to shorten further using something like:

Double priceProduct=Double.ParseDouble(fvar.getValueAsString());

(Making sure that you deal with the possibility that fvar could be a String if it is possible for that to happen)

Steve