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.NormalImpliedVolatilityFormula.java
/** * Computes the implied volatility from the price in a normally distributed asset price world. * @param data The model data. The data volatility, if not zero, is used as a starting point for the volatility search. * @param option The option./*from w w w. j av a 2 s .co m*/ * @param optionPrice The option price. * @return The implied volatility. */ public double getImpliedVolatility(final NormalFunctionData data, final EuropeanVanillaOption option, final double optionPrice) { final double numeraire = data.getNumeraire(); final boolean isCall = option.isCall(); final double f = data.getForward(); final double k = option.getStrike(); final double intrinsicPrice = numeraire * Math.max(0, (isCall ? 1 : -1) * (f - k)); Validate.isTrue(optionPrice > intrinsicPrice || CompareUtils.closeEquals(optionPrice, intrinsicPrice, 1e-6), "option price (" + optionPrice + ") less than intrinsic value (" + intrinsicPrice + ")"); if (Double.doubleToLongBits(optionPrice) == Double.doubleToLongBits(intrinsicPrice)) { return 0.0; } double sigma = (Math.abs(data.getNormalVolatility()) < 1E-10 ? 0.3 * f : data.getNormalVolatility()); NormalFunctionData newData = new NormalFunctionData(f, numeraire, sigma); final double maxChange = 0.5 * f; double[] priceDerivative = new double[3]; double price = NORMAL_PRICE_FUNCTION.getPriceAdjoint(option, newData, priceDerivative); double vega = priceDerivative[1]; double change = (price - optionPrice) / vega; double sign = Math.signum(change); change = sign * Math.min(maxChange, Math.abs(change)); if (change > 0 && change > sigma) { change = sigma; } int count = 0; while (Math.abs(change) > EPS) { sigma -= change; newData = new NormalFunctionData(f, numeraire, sigma); price = NORMAL_PRICE_FUNCTION.getPriceAdjoint(option, newData, priceDerivative); vega = priceDerivative[1]; change = (price - optionPrice) / vega; sign = Math.signum(change); change = sign * Math.min(maxChange, Math.abs(change)); if (change > 0 && change > sigma) { change = sigma; } if (count++ > MAX_ITERATIONS) { final BracketRoot bracketer = new BracketRoot(); final BisectionSingleRootFinder rootFinder = new BisectionSingleRootFinder(EPS); final Function1D<Double, Double> func = new Function1D<Double, Double>() { @SuppressWarnings({ "synthetic-access" }) @Override public Double evaluate(final Double volatility) { final NormalFunctionData myData = new NormalFunctionData(data.getForward(), data.getNumeraire(), volatility); return NORMAL_PRICE_FUNCTION.getPriceFunction(option).evaluate(myData) - optionPrice; } }; final double[] range = bracketer.getBracketedPoints(func, 0.0, 10.0); return rootFinder.getRoot(func, range[0], range[1]); } } return sigma; }
From source file:de.xaniox.heavyspleef.core.event.EventListenerMethod.java
public void invoke(Event event) { Validate.isTrue(eventClass.isAssignableFrom(event.getClass()), "event must be either " + eventClass.getName() + " or a subtype"); try {//from w w w. j a va2s.com method.invoke(instance, event); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } }
From source file:fr.ritaly.dungeonmaster.Utils.java
/** * Tells whether the given value is inside the specified range [min, max]. * * @param value/*from w ww.jav a 2 s . c o m*/ * an integer representing the value to test. * @param min * the range's lower bound. * @param max * the range's upper bound. * @return whether the given value is inside the specified range. */ public static boolean inside(int value, int min, int max) { Validate.isTrue(min < max, String.format("The given min %d must be lesser than the given max %d", min, max)); return ((min <= value) && (value <= max)); }
From source file:com.opengamma.analytics.financial.instrument.cds.ISDACDSCouponDefinition.java
@Override public ISDACDSCoupon toDerivative(final ZonedDateTime date, final String... yieldCurveNames) { Validate.notNull(date, "date"); Validate.notNull(yieldCurveNames, "yield curve names"); Validate.isTrue(yieldCurveNames.length > 0, "at least one curve required"); Validate.isTrue(!date.isAfter(getPaymentDate()), "date is after payment date"); // Required: reference date <= payment date final String fundingCurveName = yieldCurveNames[0]; return new ISDACDSCoupon(getCurrency(), getTimeBetween(date, getPaymentDate()), fundingCurveName, getPaymentYearFraction(), getNotional(), getRate(), getAccrualStartDate(), getAccrualEndDate(), getTimeBetween(date, getAccrualStartDate()), getTimeBetween(date, getAccrualEndDate())); }
From source file:net.jadler.mocking.Verifying.java
/** * Checks the number of requests described in this verifying object. * @param count expected number of requests described in this verifying object. * @throws VerificationException if the number of requests described in this verifying object doesn't equal to * the given parameter/*from ww w . j a v a 2 s. c om*/ */ public void receivedTimes(final int count) { Validate.isTrue(count >= 0, "count cannot be negative"); this.receivedTimes(equalTo(count)); }
From source file:com.opengamma.analytics.financial.model.volatility.surface.SurfaceConverter.java
Surface<Double, Double, Double> deltaToLogMoneyness(final Surface<Double, Double, Double> deltaSurf) { final Function<Double, Double> surFunc = new Function<Double, Double>() { @SuppressWarnings("synthetic-access") @Override/*from w ww . j a v a 2 s. c om*/ public Double evaluate(final Double... tx) { final double t = tx[0]; final double x = tx[1]; Validate.isTrue(t >= 0, "Must have t >= 0.0"); //find the delta that gives the required log-moneyness (x) at time t final double delta = getDeltaForLogMoneyness(x, deltaSurf, t); return deltaSurf.getZValue(t, delta); } }; return FunctionalDoublesSurface.from(surFunc); }
From source file:net.daboross.bukkitdev.skywars.api.arenaconfig.SkyArenaChestConfig.java
public static SkyArenaChestConfig deserialize(ConfigurationSection configurationSection) { Validate.isTrue(configurationSection.isBoolean("randomize"), "chest missing valid randomize"); Validate.isTrue(configurationSection.isInt("total-level"), "chest missing valid total-level"); Validate.isTrue(configurationSection.isInt("min-item-value"), "chest missing valid min-item-value"); Validate.isTrue(configurationSection.isInt("max-item-value"), "chest missing valid max-item-value"); Validate.isTrue(configurationSection.isConfigurationSection("location"), "chest missing valid location"); int level = configurationSection.getInt("total-level"); int minItemValue = configurationSection.getInt("min-item-value"); int maxItemValue = configurationSection.getInt("max-item-value"); boolean randomize = configurationSection.getBoolean("randomize"); SkyBlockLocation location = SkyBlockLocation .deserialize(configurationSection.getConfigurationSection("location")); return new SkyArenaChestConfig(randomize, level, minItemValue, maxItemValue, location); }
From source file:com.prowidesoftware.swift.model.SwiftBlock3.java
/** * Sets the block number. Will cause an exception unless setting block number to 3. * @param blockNumber the block number to set * @throws IllegalArgumentException if parameter blockName is not the integer 3 * @since 5.0//from w w w.j a v a 2 s . c o m */ protected void setBlockNumber(final Integer blockNumber) { // sanity check Validate.notNull(blockNumber, "parameter 'blockNumber' cannot be null"); Validate.isTrue(blockNumber.intValue() == 3, "blockNumber must be 3"); }
From source file:net.daboross.bukkitdev.skywars.api.kits.impl.SkyKitConfig.java
public SkyKitConfig(List<SkyKitItem> inventoryContents, List<SkyKitItem> armorContents, String name, final String description, final Material totem, int cost, String permissionNode) { Validate.notNull(inventoryContents, "Inventory cannot be null"); Validate.notNull(armorContents, "Armor contents cannot be null"); Validate.notNull(name, "Name cannot be null"); Validate.notNull(description, "Description cannot be null"); Validate.notNull(totem, "Totem cannot be null"); Validate.isTrue(armorContents.size() == 4, "Armor contents size must be 4"); Validate.isTrue(cost >= 0);/* www . j a v a 2 s.co m*/ this.inventoryContents = inventoryContents; this.armorContents = armorContents; this.name = name; this.description = description.replace(ChatColor.COLOR_CHAR, '&'); this.displayDescription = Arrays .asList(ChatColor.translateAlternateColorCodes('&', description).split("\n")); this.totem = totem; this.cost = cost; this.permission = permissionNode; }
From source file:com.opengamma.analytics.financial.interestrate.swaption.derivative.SwaptionBermudaFixedIbor.java
/** * Constructor for the Bermuda swaption. * @param underlyingSwap The swaps underlying the swaption. There is one swap for each expiration date. * @param isLong Flag indicating if the option is long (true) or short (false). * @param expiryTime The swaption expiration times. * @param settlementTime The times (in year) to the swaps settlement. *///from w w w .j a v a 2 s. com public SwaptionBermudaFixedIbor(SwapFixedCoupon<? extends Coupon>[] underlyingSwap, boolean isLong, double[] expiryTime, double[] settlementTime) { Validate.notNull(expiryTime, "Expiry time"); Validate.notNull(underlyingSwap, "Underlying swap"); Validate.notNull(settlementTime, "Settlement time"); Validate.isTrue(underlyingSwap.length == expiryTime.length, "Number of swaps not in line with number of expiry times"); Validate.isTrue(underlyingSwap.length == settlementTime.length, "Number of swaps not in line with number of settlement times"); this._underlyingSwap = underlyingSwap; this._isLong = isLong; this._expiryTime = expiryTime; this._settlementTime = settlementTime; }