List of usage examples for org.apache.commons.lang Validate isTrue
public static void isTrue(boolean expression, String message)
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.
From source file:com.opengamma.analytics.financial.model.volatility.surface.LogMoneyness.java
public LogMoneyness(final double strike, final double forward) { Validate.isTrue(strike > 0, "negative or zero strike"); Validate.isTrue(forward > 0, "negative or zero forward"); _value = Math.log(strike / forward); }
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/*www . ja v a 2 s . co m*/ * @param t the number of differences to be taken (t positive). * @return the numerical difference between adjacent elements in v */ public static double[] values(final double[] 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."); double[] tmp; if (t == 0) { // no differencing done tmp = new double[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:fr.ritaly.dungeonmaster.Utils.java
/** * Returns a random value within the specified range [min, max]. * * @param min// w w w. j a v a 2s . co m * the range's lower bound. Can't be negative. * @param max * the range's upper bound. Can't be negative. * @return a random integer within the specified range. */ public static int random(int min, int max) { Validate.isTrue(min >= 0, String.format("The given min %d must be positive", min)); Validate.isTrue(max >= 0, String.format("The given max %d must be positive", max)); Validate.isTrue(min < max, String.format("The given min %d must be lesser than the max %d", min, max)); return min + RandomUtils.nextInt(max + 1 - min); }
From source file:com.opengamma.analytics.financial.interestrate.swap.derivative.FloatingRateNote.java
private static Annuity<PaymentFixed> setUpFixedLeg(final Annuity<CouponIborSpread> annuity, final PaymentFixed initalPayment, final PaymentFixed finalPayment) { final String curveName = annuity.getDiscountCurve(); //consistency checks on the inputs Validate.isTrue(initalPayment.getCurrency() == finalPayment.getCurrency(), "initial and final payments in different currencies"); Validate.isTrue(initalPayment.getCurrency() == annuity.getCurrency(), "flaoting and fixed payments in different currencies"); Validate.isTrue(initalPayment.getPaymentTime() < finalPayment.getPaymentTime(), "initial payment after final payment"); Validate.isTrue(initalPayment.getPaymentTime() <= annuity.getNthPayment(0).getPaymentTime(), "initial payment after first floating payments"); Validate.isTrue(curveName == initalPayment.getFundingCurveName(), "inital payment discounted off different curve to floating payments"); Validate.isTrue(curveName == finalPayment.getFundingCurveName(), "final payment discounted off different curve to floating payments"); Validate.isTrue(initalPayment.getAmount() * finalPayment.getAmount() < 0, "inital payment should be oposite sign to final"); Validate.isTrue(/*w ww . j a v a 2s .com*/ (annuity.isPayer() && initalPayment.getAmount() > 0.0) || (!annuity.isPayer() && initalPayment.getAmount() < 0.0), "initial payment should be oposite sign to Ibor coupons"); final PaymentFixed[] fixedPayments = new PaymentFixed[2]; fixedPayments[0] = initalPayment; fixedPayments[1] = finalPayment; return new Annuity<PaymentFixed>(fixedPayments); }
From source file:in.voidma.lemming.LemmingLibrary.java
protected static void init(Plugin plugin, LemmingConfig config, LemmingManager manager) { Validate.isTrue(!initialized, "The Lemming Library has already been initialized."); LemmingLibrary.plugin = plugin;/*from w w w.ja v a 2s. c o m*/ LemmingLibrary.config = config; initialized = true; }
From source file:com.opengamma.analytics.financial.model.volatility.surface.Moneyness.java
public Moneyness(final double strike, final double forward) { Validate.isTrue(strike >= 0, "negative strike"); Validate.isTrue(forward > 0, "negative or zero forward"); _value = strike / forward;// w w w .j av a 2 s .c om }
From source file:com.opengamma.analytics.financial.model.finitedifference.PDETerminalResults1D.java
public PDETerminalResults1D(final PDEGrid1D grid, final double[] finalTimeStep) { Validate.isTrue(grid.getNumSpaceNodes() == finalTimeStep.length, "space steps in grid not equal to that in data"); _f = finalTimeStep;/*from w w w.ja v a 2 s .c o m*/ _grid = grid; }
From source file:com.opengamma.analytics.financial.model.volatility.smile.function.SVIFormulaData.java
public SVIFormulaData(final double[] parameters) { Validate.notNull(parameters, "parameters are null"); Validate.isTrue(parameters.length == NUM_PARAMETERS, "must be " + NUM_PARAMETERS + " parameters"); Validate.isTrue(parameters[0] >= 0, "Need a >= 0"); Validate.isTrue(parameters[1] >= 0, "Need b >= 0"); Validate.isTrue(parameters[2] >= -1 && parameters[2] <= 1, "Need -1 <= rho <= 1"); Validate.isTrue(parameters[3] >= 0, "Need nu >= 0"); _parameters = new double[NUM_PARAMETERS]; System.arraycopy(parameters, 0, _parameters, 0, NUM_PARAMETERS); }
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 a va2 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 double[] byRange(double[] 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."); double[] tmp = new double[high - low + 1]; // fence post int ptr = 0; for (int i = low; i <= high; i++) { tmp[ptr] = v[i]; ptr++; } return tmp; }
From source file:de.diemex.keepxp.ScrollOfKeeping.java
public static ItemStack makeScroll(int lvl, int percentage) { Validate.isTrue(lvl > 0, "Cannot have a scroll with a negative value"); Validate.isTrue(percentage <= 100 && percentage >= 0, "percentage has to be < 100 & > 0 was " + percentage); ItemStack scroll = new ItemStack(Material.ENCHANTED_BOOK); ItemMeta meat = scroll.getItemMeta(); meat.setDisplayName("Scroll of Keeping " + StringUtils.repeat("I", lvl)); //only 1-4 List<String> lore = new ArrayList<String>(); lore.add("This Scroll lets you keep " + percentage + "%"); lore.add("of your experience on death."); lore.add(""); lore.add("But if you die the Scroll is"); lore.add("lost forever!"); lore.add(""); lore.add("SOC Lvl " + lvl); meat.setLore(lore);/*from w w w .jav a 2 s .co m*/ scroll.setItemMeta(meat); return scroll; }