Example usage for com.google.common.math DoubleMath isMathematicalInteger

List of usage examples for com.google.common.math DoubleMath isMathematicalInteger

Introduction

In this page you can find the example usage for com.google.common.math DoubleMath isMathematicalInteger.

Prototype

@GwtIncompatible("java.lang.Math.getExponent, com.google.common.math.DoubleUtils")
public static boolean isMathematicalInteger(double x) 

Source Link

Document

Returns true if x represents a mathematical integer.

Usage

From source file:common.TakeBuilder.java

private static String jsonDouble(double val) {
    if (DoubleMath.isMathematicalInteger(val)) {
        return Integer.toString((int) Math.round(val));
    } else {//from   ww w  .j av a 2  s .  c  o  m
        return Double.toString(val);
    }
}

From source file:org.jpmml.converter.ValueUtil.java

static public String formatValue(Number number) {
    double value = number.doubleValue();

    if (DoubleMath.isMathematicalInteger(value)) {
        return Long.toString((long) value);
    }//  w  w w  .  java 2s  .c om

    return number.toString();
}

From source file:org.jpmml.converter.ValueUtil.java

static public int asInt(Number number) {

    if (number instanceof Integer) {
        return (Integer) number;
    }/*from   www  .  j a va2 s.c o  m*/

    double value = number.doubleValue();

    if (DoubleMath.isMathematicalInteger(value)) {
        return Ints.checkedCast((long) value);
    }

    throw new IllegalArgumentException();
}

From source file:com.analog.lyric.dimple.factorfunctions.core.FactorFunctionUtilities.java

public static final int toInteger(@Nullable Object value) {
    int out = 0;//from www . j  a va2  s .co  m
    if (value instanceof Number) {
        double d = ((Number) value).doubleValue();
        out = DoubleMath.isMathematicalInteger(d) ? (int) d : (int) Math.round(d);
    } else if (value instanceof Boolean) {
        out = (Boolean) value ? 1 : 0;
    } else
        throw new DimpleException("Invalid value type");
    return out;
}

From source file:org.mymedialite.eval.ItemRecommendationEvaluationResults.java

public String getPrettyPrintedValue(Object k) {
    Double v = this.get(k);
    if (DoubleMath.isMathematicalInteger(v)) {
        return integerFormat.format(v);
    } else {/*ww  w  .  j av  a  2s . c  om*/
        return decimalFormat.format(v);
    }
}

From source file:fr.ens.transcriptome.aozan.io.CasavaDesignXLSReader.java

/**
 * Parse the content of a cell.//www.  ja v a  2 s. c om
 * @param cell cell to parse
 * @return a String with the cell content
 */
private static String parseCell(final HSSFCell cell) {

    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        final double doubleValue = cell.getNumericCellValue();

        if (DoubleMath.isMathematicalInteger(doubleValue)) {
            return Long.toString((long) doubleValue);
        }
    }

    return cell.toString();
}

From source file:com.linkedin.pinot.core.query.aggregation.function.AggregationFunctionUtils.java

@Nonnull
public static String formatValue(@Nonnull Object value) {
    if (value instanceof Double) {
        Double doubleValue = (Double) value;

        // String.format is very expensive, so avoid it for whole numbers that can fit in Long.
        // We simply append ".00000" to long, in order to keep the existing behavior.
        if (doubleValue <= Long.MAX_VALUE && DoubleMath.isMathematicalInteger(doubleValue)) {
            return Long.toString(doubleValue.longValue()) + ".00000";
        } else {//from  ww  w  . jav  a  2 s .co m
            return String.format(Locale.US, "%1.5f", doubleValue);
        }
    } else {
        return value.toString();
    }
}

From source file:org.jpmml.evaluator.TypeUtil.java

static private Integer parseInteger(String value) {

    try {/*from  w  ww. j ava 2 s .  com*/
        long result = Long.parseLong(value);

        return toInt(result);
    } catch (NumberFormatException nfeInteger) {

        try {
            double result = Double.parseDouble(value);

            if (DoubleMath.isMathematicalInteger(result)) {
                return toInt((long) result);
            }
        } catch (NumberFormatException nfeDouble) {
            // Ignored
        }

        throw nfeInteger;
    }
}

From source file:com.economic.persistgson.internal.bind.ObjectTypeAdapter.java

@Override
public Object read(JsonReader in) throws IOException {
    JsonToken token = in.peek();// ww  w .j a v a2s .  co  m
    switch (token) {
    case BEGIN_ARRAY:
        List<Object> list = new ArrayList<Object>();
        in.beginArray();
        while (in.hasNext()) {
            list.add(read(in));
        }
        in.endArray();
        return list;

    case BEGIN_OBJECT:
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        in.beginObject();
        while (in.hasNext()) {
            map.put(in.nextName(), read(in));
        }
        in.endObject();
        return map;

    case STRING:
        return in.nextString();

    case NUMBER:
        JsonElement element = jsonParser.parse(in);
        String elementString = element.getAsString();
        double asDouble = Double.parseDouble(elementString); // don't catch this NumberFormatException.
        if ((Double.isNaN(asDouble) || Double.isInfinite(asDouble))) {
            throw new com.economic.persistgson.stream.MalformedJsonException(
                    "JSON forbids NaN and infinities: " + asDouble);
        }
        if (DoubleMath.isMathematicalInteger(asDouble)) {
            return (long) asDouble;
        } else {
            return asDouble;
        }
    case BOOLEAN:
        return in.nextBoolean();

    case NULL:
        in.nextNull();
        return null;

    default:
        throw new IllegalStateException();
    }
}

From source file:org.jclouds.rackspace.autoscale.v1.functions.ParseScalingPolicyResponse.java

/**
 * Parse a single scaling policy response
 *///from  w  ww  .j  a va  2  s .  c  o  m
@SuppressWarnings("unchecked")
public ScalingPolicy apply(HttpResponse from) {
    // This needs to be refactored when the service is in a more final state and changing less often
    // A lot of the complexity is expected to go away

    Map<String, Object> singleMap = json.apply(from);
    Map<String, Object> scalingPolicyMap = (Map<String, Object>) singleMap.get("policy");

    ScalingPolicyTargetType targetType = null;
    for (String key : scalingPolicyMap.keySet()) {
        if (ScalingPolicyTargetType.getByValue(key).isPresent()) {
            targetType = ScalingPolicyTargetType.getByValue(key).get();
            break;
        }
    }

    ImmutableList.Builder<Link> links = ImmutableList.builder();
    for (Map<String, String> linkMap : (List<Map<String, String>>) scalingPolicyMap.get("links")) {
        Link link = Link.builder().href(URI.create(linkMap.get("href")))
                .relation(Relation.fromValue(linkMap.get("rel"))).build();
        links.add(link);
    }

    Double d = (Double) scalingPolicyMap.get(targetType.toString()); // GSON only knows double now
    ScalingPolicy scalingPolicyResponse = new ScalingPolicy((String) scalingPolicyMap.get("name"),
            ScalingPolicyType.getByValue((String) scalingPolicyMap.get("type")).get(),
            ((Double) scalingPolicyMap.get("cooldown")).intValue(),
            DoubleMath.isMathematicalInteger(d) ? Integer.toString(d.intValue()) : Double.toString(d),
            targetType, (Map<String, String>) scalingPolicyMap.get("args"), ImmutableList.copyOf(links.build()),
            (String) scalingPolicyMap.get("id"));

    return scalingPolicyResponse;
}