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:ome.formats.model.ChannelProcessor.java

/**
 * Returns the name from the wavelength.
 *
 * @param value The value to handle.//from   ww w  .j  av a  2s.  c  o m
 * @return See above.
 */
private String getNameFromWavelength(Length value) {
    if (value == null)
        return null;
    //Check that the value is an int
    if (DoubleMath.isMathematicalInteger(value.getValue())) {
        return "" + value.getValue();
    }
    return value.toString();
}

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

/**
 * Parse a list of scaling policy responses
 */// www  . j  a va  2 s.c  o  m
@SuppressWarnings("unchecked")
public FluentIterable<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, List<Map<String, Object>>> singleMap = (Map<String, List<Map<String, Object>>>) json
            .apply(from);
    List<Map<String, Object>> result = singleMap.get("policies");
    ImmutableList.Builder<ScalingPolicy> scalingPoliciesList = ImmutableList.builder();

    for (Map<String, Object> scalingPolicyMap : result) {
        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"));
        scalingPoliciesList.add(scalingPolicyResponse);
    }

    return FluentIterable.from(scalingPoliciesList.build());
}

From source file:org.opennms.netmgt.newts.NewtsWriter.java

@Inject
public NewtsWriter(@Named("newts.max_batch_size") Integer maxBatchSize,
        @Named("newts.ring_buffer_size") Integer ringBufferSize,
        @Named("newts.writer_threads") Integer numWriterThreads, MetricRegistry registry) {
    Preconditions.checkArgument(maxBatchSize > 0, "maxBatchSize must be strictly positive");
    Preconditions.checkArgument(ringBufferSize > 0, "ringBufferSize must be positive");
    Preconditions.checkArgument(DoubleMath.isMathematicalInteger(Math.log(ringBufferSize) / Math.log(2)),
            "ringBufferSize must be a power of two");
    Preconditions.checkArgument(numWriterThreads > 0, "numWriterThreads must be positive");
    Preconditions.checkNotNull(registry, "metric registry");

    m_maxBatchSize = maxBatchSize;//from   www.j a  v a2  s . c  o m
    m_ringBufferSize = ringBufferSize;
    m_numWriterThreads = numWriterThreads;
    m_numEntriesOnRingBuffer.set(0L);

    registry.register(MetricRegistry.name("ring-buffer", "size"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return m_numEntriesOnRingBuffer.get();
        }
    });
    registry.register(MetricRegistry.name("ring-buffer", "max-size"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return Long.valueOf(m_ringBufferSize);
        }
    });

    m_droppedSamples = registry.meter(MetricRegistry.name("ring-buffer", "dropped-samples"));

    LOG.debug("Using max_batch_size: {} and ring_buffer_size: {}", maxBatchSize, m_ringBufferSize);
    setUpWorkerPool();
}

From source file:com.analog.lyric.dimple.model.domains.Domain.java

public static boolean isIntCompatibleValue(double value) {
    return DoubleMath.isMathematicalInteger(value) && isIntCompatibleValue((long) value);
}

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

/**
 * Parses the Group from the response//from  ww w  . j  a  v  a 2  s  .  c  o  m
 */
@SuppressWarnings("unchecked")
public Group 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> result = json.apply(from);

    Map<String, Object> group = (Map<String, Object>) result.get("group");
    Map<String, Object> groupConfigurationMap = (Map<String, Object>) group.get("groupConfiguration");
    Map<String, Object> launchConfigurationMap = (Map<String, Object>) group.get("launchConfiguration");
    ImmutableList.Builder<ScalingPolicy> scalingPoliciesList = ImmutableList.builder();
    Map<String, Object> args = (Map<String, Object>) launchConfigurationMap.get("args");
    Map<String, Object> server = (Map<String, Object>) args.get("server");

    ImmutableList.Builder<Personality> personalities = ImmutableList.builder();
    ImmutableList.Builder<String> networks = ImmutableList.builder();
    for (Map<String, String> jsonPersonality : (List<Map<String, String>>) server.get("personality")) {
        personalities.add(Personality.builder().path(jsonPersonality.get("path"))
                .contents(jsonPersonality.get("contents")).build());
    }

    for (Map<String, String> jsonNetwork : (List<Map<String, String>>) server.get("networks")) {
        networks.add(jsonNetwork.get("uuid"));
    }

    ImmutableList.Builder<LoadBalancer> loadBalancers = ImmutableList.builder();
    for (Map<String, Double> jsonLoadBalancer : (List<Map<String, Double>>) args.get("loadBalancers")) {
        loadBalancers
                .add(LoadBalancer.builder().id(((Double) jsonLoadBalancer.get("loadBalancerId")).intValue())
                        .port(((Double) jsonLoadBalancer.get("port")).intValue()).build());
    }

    LaunchConfiguration launchConfiguration = LaunchConfiguration.builder().loadBalancers(loadBalancers.build())
            .serverName((String) server.get("name")).serverImageRef((String) server.get("imageRef"))
            .serverFlavorRef((String) server.get("flavorRef"))
            .serverDiskConfig((String) server.get("OS-DCF:diskConfig"))
            .serverMetadata((Map<String, String>) server.get("metadata")).personalities(personalities.build())
            .networks(networks.build())
            .type(LaunchConfigurationType.getByValue((String) launchConfigurationMap.get("type")).get())
            .build();

    GroupConfiguration groupConfiguration = GroupConfiguration.builder()
            .cooldown(((Double) groupConfigurationMap.get("cooldown")).intValue())
            .minEntities(((Double) groupConfigurationMap.get("minEntities")).intValue())
            .maxEntities(((Double) groupConfigurationMap.get("maxEntities")).intValue())
            .name((String) groupConfigurationMap.get("name"))
            .metadata((Map<String, String>) groupConfigurationMap.get("metadata")).build();

    for (Map<String, Object> scalingPolicyMap : (List<Map<String, Object>>) group.get("scalingPolicies")) {
        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"));
        scalingPoliciesList.add(scalingPolicyResponse);
    }

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

    String groupId = (String) group.get("id");
    return Group.builder().id(groupId).scalingPolicy(scalingPoliciesList.build())
            .groupConfiguration(groupConfiguration).launchConfiguration(launchConfiguration)
            .links(links.build()).build();
}

From source file:org.sosy_lab.cpachecker.util.ci.translators.ApronRequirementsTranslator.java

private String getIntegerValFromScalar(final Scalar pCst) {
    double value;
    if (pCst instanceof DoubleScalar) {
        value = ((DoubleScalar) pCst).get();
    } else if (pCst instanceof MpfrScalar) {
        value = ((MpfrScalar) pCst).get().doubleValue(Mpfr.RNDN);
    } else {/*w  w w .j a  v  a2  s .c  om*/
        assert (pCst instanceof MpqScalar);
        value = ((MpqScalar) pCst).get().doubleValue();
    }
    if (DoubleMath.isMathematicalInteger(value)) {
        return String.valueOf((int) value);
    }
    throw new AssertionError("Cannot deal with this non-integer scalar");
}

From source file:com.opengamma.strata.basics.currency.FxRate.java

/**
 * Returns the formatted string version of the currency pair.
 * <p>/*from www.  j  a v  a 2  s . c o m*/
 * The format is '${baseCurrency}/${counterCurrency} ${rate}'.
 * 
 * @return the formatted string
 */
@Override
public String toString() {
    return pair + " "
            + (DoubleMath.isMathematicalInteger(rate) ? Long.toString((long) rate) : Double.toString(rate));
}

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

/**
 * Casts the specified value to Integer data type.
 *
 * @see DataType#INTEGER/*from w  w  w . j a v  a  2s. com*/
 */
static private Integer toInteger(Object value) {

    if (value instanceof Integer) {
        return (Integer) value;
    } else

    if ((value instanceof Double) || (value instanceof Float)) {
        Number number = (Number) value;

        if (DoubleMath.isMathematicalInteger(number.doubleValue())) {
            return toInt(number.longValue());
        }
    } else

    if (value instanceof Long) {
        Long number = (Long) value;

        return toInt(number.longValue());
    } else

    if ((value instanceof Short) || (value instanceof Byte)) {
        Number number = (Number) value;

        return Integer.valueOf(number.intValue());
    } else

    if ((value instanceof DaysSinceDate) || (value instanceof SecondsSinceDate)
            || (value instanceof SecondsSinceMidnight)) {
        Number number = (Number) value;

        return Integer.valueOf(number.intValue());
    }

    throw new TypeCheckException(DataType.INTEGER, value);
}

From source file:com.opengamma.basics.currency.CurrencyAmount.java

/**
 * Gets the amount as a string./*from  w w w  .j a v  a 2 s. com*/
 * <p>
 * The format is the currency code, followed by a space, followed by the
 * amount: '${currency} ${amount}'.
 * 
 * @return the currency amount
 */
@Override
@ToString
public String toString() {
    return currency + " " + (DoubleMath.isMathematicalInteger(amount) ? Long.toString((long) amount)
            : Double.toString(amount));
}

From source file:org.jpmml.rexp.RandomForestConverter.java

static UnsignedLong toUnsignedLong(double value) {

    if (!DoubleMath.isMathematicalInteger(value)) {
        throw new IllegalArgumentException();
    }//from   www  .  ja va2s.c o m

    return UnsignedLong.fromLongBits((long) value);
}