Example usage for org.apache.commons.lang.math NumberRange NumberRange

List of usage examples for org.apache.commons.lang.math NumberRange NumberRange

Introduction

In this page you can find the example usage for org.apache.commons.lang.math NumberRange NumberRange.

Prototype

public NumberRange(Number num1, Number num2) 

Source Link

Document

Constructs a new NumberRange with the specified minimum and maximum numbers (both inclusive).

The arguments may be passed in the order (min,max) or (max,min).

Usage

From source file:com.easyvalidation.rules.impl.NumberRangeRule.java

@Override
/**/*w w  w . j  a  v  a2s. com*/
 * Checks if number is in range
 */
public final boolean checkError() {
    if (!Utils.isEmpty(getValue()) && !Utils.isEmpty(getMin()) && !Utils.isEmpty(getMax())) {
        return !(new NumberRange((Number) getMin(), (Number) getMax()).containsNumber((Number) getValue()));
    }
    return false;
}

From source file:io.seldon.prediction.VariationPredictionStrategy.java

public static VariationPredictionStrategy build(List<Variation> variations) {
    Map<Range, SimplePredictionStrategy> strategyMap = new LinkedHashMap<>();
    BigDecimal ratioTotal = BigDecimal.ZERO;
    for (Variation var : variations) {
        ratioTotal = ratioTotal.add(var.ratio);
    }/*from  w ww  .  j a v  a  2 s.  co m*/
    BigDecimal currentMax = BigDecimal.ZERO;
    for (Variation var : variations) {
        NumberRange range = new NumberRange(currentMax,
                currentMax.add(var.ratio.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
        strategyMap.put(range, var.variationStrategy);
        currentMax = currentMax.add(var.ratio);
    }
    return new VariationPredictionStrategy(strategyMap);
}

From source file:au.org.ala.delta.model.NumericRange.java

public Range getFullRange() {
    Number min = _range.getMinimumNumber();
    Number max = _range.getMaximumNumber();
    if (hasExtremeLow()) {
        min = _extremeLow;//from   ww  w.  j av  a  2s  . c om
    }

    if (hasExtremeHigh()) {
        max = _extremeHigh;
    }
    if (min.equals(max)) {
        return createRangeForSingleValue(min);
    }
    return new NumberRange(min, max);
}

From source file:io.seldon.recommendation.VariationTestingClientStrategy.java

public static VariationTestingClientStrategy build(List<Variation> variations) {
    Map<Range, ClientStrategy> strategyMap = new LinkedHashMap<>();
    BigDecimal ratioTotal = BigDecimal.ZERO;
    for (Variation var : variations) {
        ratioTotal = ratioTotal.add(var.ratio);
    }//from  ww w. j  a va  2s .  co  m
    BigDecimal currentMax = BigDecimal.ZERO;
    for (Variation var : variations) {
        NumberRange range = new NumberRange(currentMax,
                currentMax.add(var.ratio.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
        strategyMap.put(range, var.variationStrategy);
        currentMax = currentMax.add(var.ratio);
    }
    return new VariationTestingClientStrategy(strategyMap);
}

From source file:io.seldon.recommendation.VariationTestingClientStrategyTest.java

@Test
public void rangeTest() {
    BigDecimal ratioTotal = new BigDecimal(1.0);
    BigDecimal currentMax = new BigDecimal(0.0);
    BigDecimal r1 = new BigDecimal(0.9);
    BigDecimal r2 = new BigDecimal(0.1);
    NumberRange range1 = new NumberRange(currentMax,
            currentMax.add(r1.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
    currentMax = currentMax.add(r1);//from www  .  j  a  va 2 s  .c  o  m
    NumberRange range2 = new NumberRange(currentMax.add(new BigDecimal(0.0001)),
            currentMax.add(r2.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
    BigDecimal t = new BigDecimal(0.900001);
    Assert.assertTrue(range1.containsNumber(t));
    Assert.assertFalse(range2.containsNumber(t));
    BigDecimal t2 = new BigDecimal(0.901);
    Assert.assertFalse(range1.containsNumber(t2));
    Assert.assertTrue(range2.containsNumber(t2));

}

From source file:org.apache.ambari.common.util.HostUtil.java

private void findRange(String exp) {
    Matcher m = p.matcher(exp);/* ww w.  ja va2 s  .  co  m*/
    while (m.find()) {
        int low = Integer.parseInt(m.group(1));
        int high = Integer.parseInt(m.group(2));
        int padding = checkPadding(m.group(1));
        if (low > high) {
            throw new RuntimeException("Invalid range - low: " + low + " high: " + high);
        }
        NumberRange range = new NumberRange(low, high);
        ranges.add(range);
        rangesPaddingSize.add(padding);
    }
}

From source file:org.cbioportal.genome_nexus.annotation.util.Numerical.java

/**
  * Checks if the given input value overlaps the start and end values.
  * Input value can be a range value too.
  */*w  ww .  j a va 2 s  . c  o  m*/
  * This function assumes that start value is smaller than the end value.
  *
  * @param input input string (a single value or a range value)
  * @param start start value
  * @param end   end value
  * @return      true if there is an overlap between values
  */
public static boolean overlaps(String input, String start, String end) {
    Integer startValue = null;
    Integer endValue = null;
    Integer minPos = null;
    Integer maxPos = null;
    boolean overlap = false;
    List<Integer> positions = extractPositiveIntegers(input);

    if (positions.size() > 0) {
        minPos = Collections.min(positions);
        maxPos = Collections.max(positions);
    }

    if (end != null && end.matches("\\d+")) {
        endValue = Integer.parseInt(end);
    }

    if (start != null && start.matches("\\d+")) {
        startValue = Integer.parseInt(start);
    }

    NumberRange range;

    if (startValue != null) {
        // if end value is not valid use start value as the end value
        if (endValue == null || endValue < startValue) {
            endValue = startValue;
        }

        range = new NumberRange(startValue, endValue);

        // check for an overlap
        if (range.containsNumber(minPos) || range.containsNumber(maxPos)) {
            overlap = true;
        }
    }

    // input can be a range value too!
    if (minPos != null && maxPos != null) {
        range = new NumberRange(minPos, maxPos);

        if (range.containsNumber(startValue) || range.containsNumber(endValue)) {
            overlap = true;
        }
    }

    return overlap;
}

From source file:se.crafted.chrisb.ecoCreature.drops.chances.ChanceTest.java

@Test
public void testAmounts() {
    int samples = 10000000;

    int min = 1;/*from   w  w  w .  j av  a2s. co m*/
    int max = 10;
    int[] bucket = new int[max + 1];
    Chance chance = new TestChance(new NumberRange(min, max), 100);

    for (int i = 0; i < samples; i++) {
        int amount = chance.nextIntAmount();
        bucket[amount]++;
    }

    double idealDist = samples / (max - min + 1);
    int total = 0;
    double maxDelta = 0;
    for (int i = min; i < bucket.length; i++) {
        double delta = Math.abs(idealDist - (double) bucket[i]) / idealDist;
        total += bucket[i];
        if (delta > maxDelta) {
            maxDelta = delta;
        }
        Assert.assertTrue(delta < 0.01);
    }
    Assert.assertEquals(samples, total);
}

From source file:se.crafted.chrisb.ecoCreature.drops.chances.CoinChance.java

public static Collection<CoinChance> parseConfig(String section, ConfigurationSection config) {
    ConfigurationSection dropConfig = config.getConfigurationSection(section);
    Collection<CoinChance> chances = Collections.emptyList();

    if (dropConfig != null && dropConfig.contains("Coin_Maximum") && dropConfig.contains("Coin_Minimum")
            && dropConfig.contains("Coin_Percent")) {
        CoinChance chance = new CoinChance(
                new NumberRange(dropConfig.getDouble("Coin_Minimum", 0),
                        dropConfig.getDouble("Coin_Maximum", 0)),
                dropConfig.getDouble("Coin_Percent", 0.0D), dropConfig.getDouble("Coin_Gain", 1.0D));

        CoinMessageDecorator rewardMessage = new CoinMessageDecorator(new DefaultMessage(
                dropConfig.getString("Reward_Message",
                        config.getString("System.Messages.Reward_Message", COIN_REWARD_MESSAGE)),
                config.getBoolean("System.Messages.Output")));
        rewardMessage.setLoggingEnabled(config.getBoolean("System.Messages.LogCoinRewards", true));
        chance.setCoinRewardMessage(rewardMessage);

        CoinMessageDecorator penaltyMessage = new CoinMessageDecorator(new DefaultMessage(
                dropConfig.getString("Penalty_Message",
                        config.getString("System.Messages.Penalty_Message", COIN_PENALTY_MESSAGE)),
                config.getBoolean("System.Messages.Output")));
        penaltyMessage.setLoggingEnabled(config.getBoolean("System.Messages.LogCoinRewards", true));
        chance.setCoinPenaltyMessage(penaltyMessage);

        NoCoinMessageDecorator noRewardMessage = new NoCoinMessageDecorator(new DefaultMessage(
                dropConfig.getString("NoReward_Message",
                        config.getString("System.Messages.NoReward_Message", NO_COIN_REWARD_MESSAGE)),
                config.getBoolean("System.Messages.Output")));
        noRewardMessage.setNoRewardMessageEnabled(config.getBoolean("System.Messages.NoReward"));
        chance.setNoCoinRewardMessage(noRewardMessage);

        CoinMessageDecorator partyPenaltyMessage = new CoinMessageDecorator(
                new DefaultMessage(PARTY_PENALTY_MESSAGE, config.getBoolean("System.Messages.Output")));
        partyPenaltyMessage.setLoggingEnabled(config.getBoolean("System.Messages.LogCoinRewards", true));
        chance.setPartyRewardMessage(partyPenaltyMessage);

        CoinMessageDecorator partyRewardMessage = new CoinMessageDecorator(
                new DefaultMessage(PARTY_REWARD_MESSAGE, config.getBoolean("System.Messages.Output")));
        partyRewardMessage.setLoggingEnabled(config.getBoolean("System.Messages.LogCoinRewards", true));
        chance.setPartyRewardMessage(partyRewardMessage);

        NoCoinMessageDecorator partyNoRewardMessage = new NoCoinMessageDecorator(
                new DefaultMessage(PARTY_NO_REWARD_MESSAGE, config.getBoolean("System.Messages.Output")));
        partyNoRewardMessage.setNoRewardMessageEnabled(config.getBoolean("System.Messages.NoReward"));
        chance.setPartyNoRewardMessage(partyNoRewardMessage);

        chance.setIntegerCurrency(config.getBoolean("System.Economy.IntegerCurrency"));

        chances = new ArrayList<>();
        chances.add(chance);//from www.j  a  v  a  2s.c o  m
    }

    return chances;
}

From source file:se.crafted.chrisb.ecoCreature.drops.chances.CustomEntityChance.java

protected static NumberRange parseRange(String dropString) {
    String[] dropParts = dropString.split(":");
    String[] amountRange = dropParts[1].split("-");

    int min = 0;/*  ww  w .  j  av a2 s  . c  om*/
    int max;

    if (amountRange.length == 2) {
        min = Integer.parseInt(amountRange[0]);
        max = Integer.parseInt(amountRange[1]);
    } else {
        max = Integer.parseInt(dropParts[1]);
    }

    return new NumberRange(min, max);
}