Example usage for org.apache.commons.lang Validate isTrue

List of usage examples for org.apache.commons.lang Validate isTrue

Introduction

In this page you can find the example usage for org.apache.commons.lang Validate isTrue.

Prototype

public static void isTrue(boolean expression, String message) 

Source Link

Document

Validate an argument, throwing IllegalArgumentException if the test result is false.

This is used when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.

 Validate.isTrue( (i > 0), "The value must be greater than zero"); Validate.isTrue( myObject.isOk(), "The object is not OK"); 

For performance reasons, the message string should not involve a string append, instead use the #isTrue(boolean,String,Object) method.

Usage

From source file:de.matzefratze123.heavyspleef.core.player.SpleefPlayer.java

public void sendMessage(String message) {
    Validate.isTrue(isOnline(), "Player is not online");
    getBukkitPlayer().sendMessage(HeavySpleef.PREFIX + message);
}

From source file:com.hp.autonomy.aci.content.sort.SortBy.java

/**
 * Constructor for creating a {@code SortBy} that only requires a specifier name.
 * <p>//  w ww .jav  a  2  s  . c om
 * This constructor is {@code protected} to discourage its use, not because a subclass is strictly required. All of
 * the current sort specifiers of this form exist as immutable, static fields of this class.
 *
 * @param sort The specifier name
 */
protected SortBy(final String sort) {
    Validate.isTrue(StringUtils.isNotBlank(sort), "Sort must not be blank");
    this.sort = sort;
}

From source file:com.extrahardmode.module.UtilityModule.java

/**
 * >>>>>>> dev Damage an item based on the amount of Blocks it can mine
 *
 * @param item   Item to damage//from   ww w . j a v  a  2  s. com
 * @param blocks amount of blocks the item can break
 *
 * @return the damaged Item, can be completely broken
 */
public static ItemStack damage(ItemStack item, short blocks) {
    short maxDurability = item.getType().getMaxDurability();
    Validate.isTrue(maxDurability > 1, "This item is not damageable");
    if (blocks <= 0)
        return item;

    short damagePerBlock = (short) (maxDurability / blocks);
    //Because tooldmg is an int we have to sometimes break the tool twice, I couldn't find the flaw in the first formula so oh well....
    double percent = damagePerBlock > 1 ? (maxDurability % blocks) / (double) maxDurability
            : ((double) maxDurability / blocks) - damagePerBlock;

    if (damagePerBlock > 0 || percent > 0.0D) {
        int durability = item.getDurability();
        durability += damagePerBlock;

        if (OurRandom.nextDouble() < percent)
            durability += (damagePerBlock > 0 ? damagePerBlock : 1);

        item.setDurability((short) durability);
    }
    return item;
}

From source file:com.opengamma.analytics.math.utilities.Diff.java

/**
 * Finds the t^{th} numerical difference between value at position (i+1) and (i)
 * (effectively recurses {@link Diff.values} "t" times);
 * @param v the vector/*from   ww w  .  jav a 2s  .co m*/
 * @param t the number of differences to be taken (t positive).
 * @return the numerical difference between adjacent elements in v
 */
public static float[] values(final float[] v, final int t) {
    Validate.notNull(v);
    Validate.isTrue((t > -1),
            "Invalid number of differences requested, t must be positive or 0. The given t was: " + t);
    Validate.isTrue((t < v.length),
            "Invalid number of differences requested, 't' is greater than the number of elements in 'v'. The given 't' was: "
                    + t + " and 'v' contains " + v.length + " elements.");
    float[] tmp;
    if (t == 0) { // no differencing done
        tmp = new float[v.length];
        System.arraycopy(v, 0, tmp, 0, v.length);
    } else {
        tmp = values(v);
        for (int i = 0; i < t - 1; i++) {
            tmp = values(tmp);
        }
    }
    return tmp;
}

From source file:com.opengamma.analytics.financial.interestrate.payments.market.CouponIborGearingDiscountingMarketMethod.java

@Override
public CurrencyAmount presentValue(final InstrumentDerivative instrument, final MarketBundle market) {
    Validate.isTrue(instrument instanceof CouponIborGearing, "Coupon Ibor Gearing");
    return presentValue((CouponIborGearing) instrument, market);
}

From source file:com.publicuhc.uhcaddons.teammanager.commands.RandomTeamsCommand.java

/**
 * Splits the list into even sized lists
 *
 * @param list  the list to split/* w  w  w.j a v  a 2 s  . c o  m*/
 * @param count the number of lists to make
 * @param <T>   type
 * @return a list of the split lists
 */
public static <T> List<List<T>> split(List<T> list, int count) {
    Validate.isTrue(list.size() >= count, "List must be >= to the amount of requested lists");

    int amountPerList = (int) Math.ceil((double) list.size() / (double) count);

    return Lists.partition(list, amountPerList);
}

From source file:de.xaniox.heavyspleef.core.game.GameManager.java

public void renameGame(final Game game, final String to, FutureCallback<Void> callback) {
    String oldName;//from  ww  w .j  a v  a 2  s. c  om

    synchronized (games) {
        Validate.isTrue(!hasGame(to), "A game with the name '" + to + "' already exists");

        oldName = game.getName();
        game.setName(to);

        games.remove(oldName);
        games.put(to, game);
    }

    heavySpleef.getDatabaseHandler().renameGame(game, oldName, to, callback);
}

From source file:net.daboross.bukkitdev.skywars.api.location.SkyBlockLocationRange.java

/**
 * Creates a SkyBlockLocationRange./*w w  w  .  ja v a 2s. c o m*/
 *
 * @param min   minimum position. min.world is expected to be equal to world, and will be changed if not world
 *              already.
 * @param max   maximum position. max.world is expected to be equal to world, and will be changed if not world
 *              already.
 * @param world world of the range.
 * @throws IllegalArgumentException if min == null || max == null || min.x &gt; max.x || min.y &gt; max.y || min.z
 *                                  &gt; max.z
 * @throws NullPointerException     if min or max is null
 */
public SkyBlockLocationRange(SkyBlockLocation min, SkyBlockLocation max, String world) {
    Validate.notNull(min, "Min cannot be null");
    Validate.notNull(max, "Max cannot be null");
    Validate.isTrue(min.x <= max.x && min.y <= max.y && min.z <= max.z,
            "Min position cannot be bigger than max position in any dimension");
    if (min.world == null ? world != null : !min.world.equals(world)) {
        min = min.changeWorld(world);
    }
    if (max.world == null ? world != null : !max.world.equals(world)) {
        max = max.changeWorld(world);
    }
    this.world = min.world;
    this.min = min;
    this.max = max;
}

From source file:com.opengamma.analytics.financial.credit.cds.ISDAExtrapolator1D.java

@Override
public double firstDerivative(Interpolator1DDataBundle data, Double value) {

    Validate.notNull(value, "Value to be interpolated must not be null");
    Validate.notNull(data, "Data bundle must not be null");

    double[] xValues = data.getKeys();
    double[] yValues = data.getValues();

    Validate.isTrue(xValues.length == yValues.length, "Invalid data in curve object");
    Validate.isTrue(xValues.length > 1, "At least two data points are required for extrapolation");
    Validate.isTrue(value > xValues[xValues.length - 1], "Value must lie beyond curve data for extrapolation");

    // Avoid divide by zero errors (offset factors out of the final result if it is used)
    final double offset = value == 0.0 ? 1.0 : 0.0;

    final double x1 = xValues[xValues.length - 2];
    final double y1 = yValues[yValues.length - 2];
    final double y1x1 = y1 * (x1 + offset);

    final double x2 = xValues[xValues.length - 1];
    final double y2 = yValues[yValues.length - 1];
    final double y2x2 = y2 * (x2 + offset);

    final double valueWithOffset = value + offset;
    return (y2x2 - y1x1) / (x2 - x1) / valueWithOffset
            - (y1x1 + (value - x1) / (x2 - x1) * (y2x2 - y1x1)) / valueWithOffset / valueWithOffset;
}

From source file:com.opengamma.maths.lowlevelapi.functions.utilities.View.java

/**
 * Looks up the values in v at positions given by index and returns them!
 * @param v the vector/*w w  w . j  ava 2 s . c  o  m*/
 * @param low the lowest element of the range
 * @param high the highest element of the range
 * @return tmp the values looked up by range. i.e. values[low:high];
 */
public static float[] byRange(float[] v, int low, int high) {
    Validate.notNull(v);
    final int n = v.length;
    Validate.isTrue(high < n,
            "Input \"high\" index out of range. " + high + " whereas vector to dereference is length " + n);
    Validate.isTrue(low > -1, "Negative index dereference requested on vector");
    Validate.isTrue(high >= low, "high value is lower than low value, negative range not supported.");
    float[] tmp = new float[high - low + 1]; // fence post
    int ptr = 0;
    for (int i = low; i <= high; i++) {
        tmp[ptr] = v[i];
        ptr++;
    }
    return tmp;
}