/** * Get an optional {@link Number} value associated with a key, or <code>null</code> * if there is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number ({@link BigDecimal}). This method * would be used in cases where type coercion of the number value is unwanted. * * @param index * The index must be between 0 and length() - 1. * @return An object which is the value. */ public Number optNumber(int index) { return this.optNumber(index, null); }
/** * Get an optional {@link Number} value associated with a key, or <code>null</code> * if there is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number ({@link BigDecimal}). This method * would be used in cases where type coercion of the number value is unwanted. * * @param index * The index must be between 0 and length() - 1. * @return An object which is the value. */ public Number optNumber(int index) { return this.optNumber(index, null); }
/** * Get the optional int value associated with an index. The defaultValue is * returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default value. * @return The value. */ public int optInt(int index, int defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } return val.intValue(); }
/** * Get the optional long value associated with an index. The defaultValue is * returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default value. * @return The value. */ public long optLong(int index, long defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } return val.longValue(); }
/** * Get the optional double value associated with an index. The defaultValue * is returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * subscript * @param defaultValue * The default value. * @return The value. */ public double optDouble(int index, double defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } final double doubleValue = val.doubleValue(); // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { // return defaultValue; // } return doubleValue; }
/** * Get the optional float value associated with an index. The defaultValue * is returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * subscript * @param defaultValue * The default value. * @return The value. */ public float optFloat(int index, float defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } final float floatValue = val.floatValue(); // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { // return floatValue; // } return floatValue; }
/** * Get an optional {@link Number} value associated with a key, or <code>null</code> * if there is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number ({@link BigDecimal}). This method * would be used in cases where type coercion of the number value is unwanted. * * @param index * The index must be between 0 and length() - 1. * @return An object which is the value. */ public Number optNumber(int index) { return this.optNumber(index, null); }
/** * Get the optional int value associated with an index. The defaultValue is * returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default value. * @return The value. */ public int optInt(int index, int defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } return val.intValue(); }
/** * Get the optional long value associated with an index. The defaultValue is * returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default value. * @return The value. */ public long optLong(int index, long defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } return val.longValue(); }
/** * Get the optional double value associated with an index. The defaultValue * is returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * subscript * @param defaultValue * The default value. * @return The value. */ public double optDouble(int index, double defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } final double doubleValue = val.doubleValue(); // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { // return defaultValue; // } return doubleValue; }
/** * Get the optional float value associated with an index. The defaultValue * is returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * subscript * @param defaultValue * The default value. * @return The value. */ public float optFloat(int index, float defaultValue) { final Number val = this.optNumber(index, null); if (val == null) { return defaultValue; } final float floatValue = val.floatValue(); // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { // return floatValue; // } return floatValue; }
private BitfinexExecutedTrade jsonToExecutedTrade(final JSONArray jsonArray) { final BitfinexExecutedTrade executedTrade = new BitfinexExecutedTrade(); final long id = jsonArray.getNumber(0).longValue(); executedTrade.setTradeId(id); final long timestamp = jsonArray.getNumber(1).longValue(); executedTrade.setTimestamp(timestamp); final BigDecimal amount = jsonArray.getBigDecimal(2); executedTrade.setAmount(amount); // Funding or Currency if (jsonArray.optNumber(4) != null) { final BigDecimal rate = jsonArray.getBigDecimal(3); executedTrade.setRate(rate); final Long period = jsonArray.getLong(4); executedTrade.setPeriod(period); } else { final BigDecimal price = jsonArray.getBigDecimal(3); executedTrade.setPrice(price); } return executedTrade; }