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.xaniox.heavyspleef.core.event.GlobalEventBus.java

@Override
public void registerListener(SpleefListener listener, boolean registerSuper) {
    Validate.isTrue(!globalListeners.contains(listener), "Global listener already registered");
    for (EventBus bus : singleInstanceBusMap) {
        if (bus.isRegistered(listener)) {
            throw new IllegalArgumentException("This listener has already been registered on a child EventBus");
        }/*w  w w .  j  a va  2  s.co m*/
    }

    globalListeners.add(listener);
    for (EventBus bus : singleInstanceBusMap) {
        bus.registerListener(listener, registerSuper);
    }
}

From source file:com.opengamma.analytics.financial.instrument.swap.SwapIborIborDefinition.java

/**
 * Constructor of the ibor-ibor swap from its two legs.
 * @param firstLeg The first Ibor leg.//from  w w  w.  j a v a  2  s.c  o  m
 * @param secondLeg The second Ibor leg.
 */
public SwapIborIborDefinition(final AnnuityCouponIborSpreadDefinition firstLeg,
        final AnnuityCouponIborSpreadDefinition secondLeg) {
    super(firstLeg, secondLeg);
    Validate.isTrue(firstLeg.getCurrency() == secondLeg.getCurrency(), "legs should have the same currency");
}

From source file:net.jodah.failsafe.internal.actions.TimesAction.java

/**
 * Constructs a new action which will repeat the last recorded action from the specified
 * expectation the specified number of times.
 *
 * @param expectation the expectation where to get the last recorded action to repeat
 * @param name the name for this action//from w  ww .ja v a2s.  c  om
 * @param count the number of times to repeat the action
 * @throws IllegalArgumentException if <code>count</code> is negative
 */
TimesAction(ActionRegistry<R>.Expectation expectation, String name, int count) {
    super(expectation, name);
    Validate.isTrue(count >= 0, "count must be greater or equal than 0");
    this.count = count;
}

From source file:com.opengamma.analytics.financial.model.finitedifference.PDEGrid1D.java

/**
 * Create a uniform grid with numTimeNodes between 0 and tMax, and numSpaceNodes between xMin and xMax
 * @param numTimeNodes The number of time nodes. Note, the number of time steps is numTimeNodes - 1, therefore need  numTimeNodes >= 2
 * @param numSpaceNodes The number of space nodes. Note, this includes the boundaries, so the number of internal nodes is numSpaceNodes - 2, therefore need numSpaceNodes >=3
 * @param tMax maximum time/*from ww w  . j a va2s. c  o m*/
 * @param xMin minimum x
 * @param xMax maximum x
 */
public PDEGrid1D(final int numTimeNodes, final int numSpaceNodes, final double tMax, final double xMin,
        final double xMax) {

    Validate.isTrue(numTimeNodes > 1, "need at least 2 time nodes");
    Validate.isTrue(numSpaceNodes > 2, "need at least 3 space nodes");
    Validate.isTrue(tMax > 0, "need tMax > 0");
    Validate.isTrue(xMax > xMin, "need xMax > xMin");

    _nSpaceNodes = numSpaceNodes;
    _tNodes = new double[numTimeNodes];
    _xNodes = new double[numSpaceNodes];
    _dt = new double[numTimeNodes - 1];
    _dx = new double[numSpaceNodes - 1];

    final double dt = tMax / (numTimeNodes - 1);
    _tNodes[numTimeNodes - 1] = tMax;
    for (int i = 0; i < numTimeNodes - 1; i++) {
        _tNodes[i] = i * dt;
        _dt[i] = dt;
    }

    final double dx = (xMax - xMin) / (numSpaceNodes - 1);
    _xNodes[numSpaceNodes - 1] = xMax;
    for (int i = 0; i < numSpaceNodes - 1; i++) {
        _xNodes[i] = xMin + i * dx;
        _dx[i] = dx;
    }

    _x1st = new double[numSpaceNodes - 2][3];
    _x2nd = new double[numSpaceNodes - 2][3];
    for (int i = 0; i < numSpaceNodes - 2; i++) {
        _x1st[i][0] = -1. / 2. / dx;
        _x1st[i][1] = 0.0;
        _x1st[i][2] = 1. / 2. / dx;
        _x2nd[i][0] = 1. / dx / dx;
        _x2nd[i][1] = -2. / dx / dx;
        _x2nd[i][2] = 1. / dx / dx;
    }

    _x1stFwd = new double[numSpaceNodes - 1][2];
    _x1stBkd = new double[numSpaceNodes - 1][2];
    for (int i = 0; i < numSpaceNodes - 1; i++) {
        _x1stFwd[i][0] = -1 / dx;
        _x1stFwd[i][1] = 1 / dx;
        _x1stBkd[i][0] = -1 / dx;
        _x1stBkd[i][1] = 1 / dx;
    }
}

From source file:com.opengamma.analytics.math.function.special.IncompleteBetaFunction.java

/**
 * /*from w w w.ja  v a2  s . co m*/
 * @param a a, $a > 0$
 * @param b b, $b > 0$
 * @param eps Approximation accuracy, $\epsilon \geq 0$
 * @param maxIter Maximum number of iterations, $\iter \geq 1$
 */
public IncompleteBetaFunction(final double a, final double b, final double eps, final int maxIter) {
    Validate.isTrue(a > 0, "a must be > 0");
    Validate.isTrue(b > 0, "b must be > 0");
    Validate.isTrue(eps >= 0, "eps must not be negative");
    Validate.isTrue(maxIter >= 1, "maximum number of iterations must be greater than zero");
    _a = a;
    _b = b;
    _eps = eps;
    _maxIter = maxIter;
}

From source file:com.opengamma.analytics.financial.instrument.annuity.AnnuityCouponInflationYearOnYearMonthlyDefinition.java

/**
 * Year on year annuity (or Year on year coupon leg) constructor from standard description. The coupon are fixing in advance and payment in arrears. 
 * @param priceIndex The price index./*ww  w  .j  a  va  2  s  .c om*/
 * @param settlementDate The settlement date.
 * @param notional The notional.
 * @param totalPeriod The  period of the annuity.
 * @param paymentPeriod The payment period of the coupons.
 * @param businessDayConvention the business day convention. 
 * @param calendar the calendar.
 * @param endOfMonth The end-of-month flag.
 * @param conventionalMonthLag TODO
 * @param monthLag The day count of the coupons.
 * @param payNotional Payer (true) / receiver (false) flag.
 * @return The Year on year coupon leg.
 */
public static AnnuityCouponInflationYearOnYearMonthlyDefinition from(final IndexPrice priceIndex,
        final ZonedDateTime settlementDate, final double notional, final Period totalPeriod,
        final Period paymentPeriod, final BusinessDayConvention businessDayConvention, final Calendar calendar,
        final boolean endOfMonth, int conventionalMonthLag, final int monthLag, final boolean payNotional) {
    Validate.notNull(settlementDate, "settlement date");
    Validate.isTrue(notional > 0, "notional <= 0");
    Validate.notNull(paymentPeriod, "Payment period");
    ZonedDateTime[] paymentDates = ScheduleCalculator.getAdjustedDateSchedule(settlementDate, paymentPeriod,
            totalPeriod, true, false, businessDayConvention, calendar, endOfMonth);

    final CouponInflationYearOnYearMonthlyDefinition[] coupons = new CouponInflationYearOnYearMonthlyDefinition[paymentDates.length];
    coupons[0] = CouponInflationYearOnYearMonthlyDefinition.from(settlementDate, paymentDates[0], notional,
            priceIndex, monthLag, conventionalMonthLag, payNotional);
    for (int loopcpn = 1; loopcpn < paymentDates.length; loopcpn++) {
        coupons[loopcpn] = CouponInflationYearOnYearMonthlyDefinition.from(paymentDates[loopcpn - 1],
                paymentDates[loopcpn], notional, priceIndex, monthLag, conventionalMonthLag, payNotional);
    }
    return new AnnuityCouponInflationYearOnYearMonthlyDefinition(coupons);
}

From source file:fr.ritaly.dungeonmaster.champion.SkillMapBuilder.java

private void validateLevel(final String name, final int level) {
    Validate.isTrue((0 <= level) && (level <= 15),
            String.format("The given %s level (%d) must be in [0,15]", name, level));
}

From source file:com.hp.autonomy.aci.content.identifier.reference.Reference.java

/**
 * Creates a new {@code Reference} instance for the specified string reference and section number.
 *
 * @param reference The document reference
 * @param section The section number//from  w  w  w. java 2 s.  c o m
 */
public Reference(final String reference, final int section) {
    Validate.isTrue(StringUtils.isNotBlank(reference), "Reference must not be blank");
    Validate.isTrue(section >= 0, "Section number must not be less than zero");

    this.reference = reference;
    this.section = section;
}

From source file:com.relicum.ipsum.Utils.StringStyles.java

/**
 * Centered heading.//from  w w  w .j ava2 s.  com
 * <p>Centres the text to be exactly in the middle of the line with white space used for padding either side.
 * The max length of the heading string is 26 characters.
 *
 * @param color   the color
 * @param style   the style
 * @param heading the heading text
 * @return the centered heading string
 */
public static String centeredHeading(ChatColor color, ChatColor style, String heading) {

    Validate.notNull(color);
    Validate.notNull(style);
    Validate.isTrue(heading.length() < 27, "The maximum length of the heading is 26 Characters");

    int left = 26 - heading.length();
    int offSet = 0;
    int outSet = 0;

    if (!((Integer) left < 1)) {

        if (left == 1) {
            offSet = 1;
        } else if (left == 2) {
            offSet = 1;
            outSet = 1;
        } else if (left > 0 && left % 2 == 1) {
            offSet = (left / 2) + 1;
            outSet = left / 2;
        } else {
            offSet = left / 2;
            outSet = offSet;
        }
    }
    StrBuilder sb = new StrBuilder(58);

    sb.append(" ").appendPadding(19 + offSet, ' ').append(style).append("").append(color).append(heading)
            .appendPadding(8 + offSet, ' ');

    return sb.toString();
}

From source file:com.vmware.identity.performanceSupport.PerfBucketMetrics.java

/**
 * Add measurement to the metrics. Thread safe.
 *
 * @param value New data entry, >=0//from  ww w  .  j  a v a2s . c om
 */
public synchronized void addMeasurement(long value) {
    Validate.isTrue(value >= 0, Long.toString(value));
    if (Long.MAX_VALUE - totalMs < value) {
        reset(); //overflow
    }
    ++hits;
    totalMs += value;
    if (value > ceilingMs) {
        effectiveCeilingMs = ceilingMs;
        ceilingMs = value;
    } else if (value > effectiveCeilingMs) {
        effectiveCeilingMs = value;
    }
    if (value < floorMs) {
        floorMs = value;
    }
}