List of usage examples for java.util Arrays binarySearch
public static int binarySearch(Object[] a, Object key)
From source file:com.opengamma.financial.convention.daycount.AccruedInterestCalculator.java
/** * Calculates the accrued interest for a {@code ZonedDateTime}. * //from ww w . j ava 2s .c om * @param dayCount the day count convention, not null * @param settlementDate the settlement date, not null * @param nominalDates the nominalDates, not null, no null elements * @param coupon the coupon value * @param paymentsPerYear the number of payments per year, one, two, three, four, six or twelve * @param isEndOfMonthConvention whether to use end of month rules * @param exDividendDays the number of ex-dividend days * @param calendar The working day calendar to be used in calculating ex-dividend dates, not null * @return the accrued interest */ public static double getAccruedInterest(final DayCount dayCount, final ZonedDateTime settlementDate, final ZonedDateTime[] nominalDates, final double coupon, final int paymentsPerYear, final boolean isEndOfMonthConvention, final int exDividendDays, final Calendar calendar) { Validate.notNull(dayCount, "day-count"); Validate.notNull(settlementDate, "date"); Validate.noNullElements(nominalDates, "nominalDates"); Validate.notNull(calendar, "calendar"); Validate.isTrue(paymentsPerYear > 0); Validate.isTrue(exDividendDays >= 0); final int i = Arrays.binarySearch(nominalDates, settlementDate); if (i > 0) { return 0; } final int index = -i - 2; final int length = nominalDates.length; Validate.isTrue(index >= 0, "Settlement date is before first accrual date"); Validate.isTrue(index < length, "Settlement date is after maturity date"); final double accruedInterest = getAccruedInterest(dayCount, index, length, nominalDates[index], settlementDate, nominalDates[index + 1], coupon, paymentsPerYear, isEndOfMonthConvention); ZonedDateTime exDividendDate = nominalDates[index + 1]; for (int j = 0; j < exDividendDays; j++) { while (!calendar.isWorkingDay(exDividendDate.toLocalDate())) { exDividendDate = exDividendDate.minusDays(1); } exDividendDate = exDividendDate.minusDays(1); } if (exDividendDays != 0 && exDividendDate.isBefore(settlementDate)) { return accruedInterest - coupon; } return accruedInterest; }
From source file:com.mycompany.monroelabsm.B58.java
public static String encode(byte[] toEncode, byte prefix) { //TODO:validate toEncode for correct size //TODO:implement an enum for this parameter instead. //validate prefix against known prefixes byte[] validPrefixes = { 0, //Bitcoin pubkey hash 5, //Bitcoin script hash 21, //Bitcoin (compact) public key (proposed) 52, //Namecoin pubkey hash -128, //Private key 111, //Bitcoin testnet pubkey hash -60, //Bitcoin testnet script hash -17 //Testnet Private key };// w ww. java 2 s .com Arrays.sort(validPrefixes); if (Arrays.binarySearch(validPrefixes, prefix) < 0) {//not found. see https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html return "Base58 error: Invalid address prefix byte specified."; } int size = 21; if (prefix == (byte) -128) { size = 33; } byte[] step1 = new byte[size]; step1[0] = prefix; for (int i = 1; i < size; i++) { step1[i] = toEncode[i - 1]; } byte[] step2 = new byte[4]; byte[] step2a = DigestUtils.sha256(DigestUtils.sha256(step1)); step2[0] = step2a[0]; step2[1] = step2a[1]; step2[2] = step2a[2]; step2[3] = step2a[3]; byte[] step3 = new byte[size + 4]; for (int i = 0; i < size; i++) { step3[i] = step1[i]; } step3[size] = step2[0]; step3[size + 1] = step2[1]; step3[size + 2] = step2[2]; step3[size + 3] = step2[3]; String output = Base58.encode(step3); return output; }
From source file:com.cloudera.oryx.kmeans.common.WeightedSampler.java
public T sample() { double offset = random.nextDouble() * cumulativeSum[cumulativeSum.length - 1]; int next = Arrays.binarySearch(cumulativeSum, offset); Weighted<T> item = (next >= 0) ? things.get(next - 1) : things.get(-2 - next); return item.thing(); }
From source file:Util.java
public static final double[] interpLinear(double[] x, double[] y, double[] xi) throws IllegalArgumentException { if (x.length != y.length) { throw new IllegalArgumentException("X and Y must be the same length"); }/*from w w w . j ava2 s. co m*/ if (x.length == 1) { throw new IllegalArgumentException("X must contain more than one value"); } double[] dx = new double[x.length - 1]; double[] dy = new double[x.length - 1]; double[] slope = new double[x.length - 1]; double[] intercept = new double[x.length - 1]; // Calculate the line equation (i.e. slope and intercept) between each point for (int i = 0; i < x.length - 1; i++) { dx[i] = x[i + 1] - x[i]; if (dx[i] == 0) { throw new IllegalArgumentException("X must be montotonic. A duplicate " + "x-value was found"); } if (dx[i] < 0) { throw new IllegalArgumentException("X must be sorted"); } dy[i] = y[i + 1] - y[i]; slope[i] = dy[i] / dx[i]; intercept[i] = y[i] - x[i] * slope[i]; } // Perform the interpolation here double[] yi = new double[xi.length]; for (int i = 0; i < xi.length; i++) { if ((xi[i] > x[x.length - 1]) || (xi[i] < x[0])) { yi[i] = Double.NaN; } else { int loc = Arrays.binarySearch(x, xi[i]); if (loc < -1) { loc = -loc - 2; yi[i] = slope[loc] * xi[i] + intercept[loc]; } else { yi[i] = y[loc]; } } } return yi; }
From source file:offstage.cleanse.Hist.java
public void add(double d) { int ix = Arrays.binarySearch(cat, d); if (ix >= 0) ++count[ix];//from w ww. j a va 2 s . co m else ++count[-(ix + 2)]; ++tot; }
From source file:com.gordoni.opal.BiInterpolator.java
public double value(double[] p) { double x = p[0]; x = Math.max(x, mp.floor[0]); x = Math.min(x, mp.ceiling[0]); double y = p[1]; y = Math.max(y, mp.floor[1]); y = Math.min(y, mp.ceiling[1]); double v = f.value(x, y); int xindex = Arrays.binarySearch(xval, x); if (xindex < 0) xindex = -xindex - 2;//w w w .j a v a 2s .c o m int yindex = Arrays.binarySearch(yval, y); if (yindex < 0) yindex = -yindex - 2; double fmin = fval[xindex][yindex]; double fmax = fval[xindex][yindex]; if (xindex + 1 < xval.length) { fmin = Math.min(fmin, fval[xindex + 1][yindex]); fmax = Math.max(fmax, fval[xindex + 1][yindex]); } if (yindex + 1 < yval.length) { fmin = Math.min(fmin, fval[xindex][yindex + 1]); fmax = Math.max(fmax, fval[xindex][yindex + 1]); } if (xindex + 1 < xval.length && yindex + 1 < yval.length) { fmin = Math.min(fmin, fval[xindex + 1][yindex + 1]); fmax = Math.max(fmax, fval[xindex + 1][yindex + 1]); } v = Math.max(v, fmin); v = Math.min(v, fmax); return v; }
From source file:com.gordoni.opal.UniInterpolator.java
public double value(double[] p) { double x = p[0]; x = Math.max(x, mp.floor[0]); x = Math.min(x, mp.ceiling[0]); double v = f.value(x); // Bound value by surrounding knot values. Otherwise get bad results if metric_sm is non-monotone in p. int xindex = Arrays.binarySearch(xval, x); if (xindex < 0) xindex = -xindex - 2;// w w w . jav a2 s .c om double fmin = fval[xindex]; double fmax = fval[xindex]; if (xindex + 1 < xval.length) { fmin = Math.min(fmin, fval[xindex + 1]); fmax = Math.max(fmax, fval[xindex + 1]); } v = Math.max(v, fmin); v = Math.min(v, fmax); return v; }
From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionPlayerHasClanHall.java
@Override public boolean testImpl(Env env) { if (!(env.player instanceof L2Player)) return false; L2Clan clan = ((L2Player) env.player).getClan(); if (clan == null) return _clanHall.length == 1 && _clanHall[0] == 0; // All Clan Hall if (_clanHall.length == 1 && _clanHall[0] == -1) return clan.getHasHideout() > 0; return Arrays.binarySearch(_clanHall, clan.getHasHideout()) >= 0; }
From source file:com.gordoni.opal.LSInterpolator.java
public double value(double[] p) { double x = p[0]; x = Math.max(x, mp.floor[0]); x = Math.min(x, mp.ceiling[0]); double y = p[1]; y = Math.max(y, mp.floor[1]); y = Math.min(y, mp.ceiling[1]); if (!linear_spline) { // spline-linear. double tmp = x; x = y;/* www .j av a 2s . com*/ y = tmp; } int xindex = Arrays.binarySearch(xval, x); if (xindex < 0) xindex = -xindex - 2; int x1; int x2; if (xindex + 1 > xval.length - 1) { x1 = xindex; x2 = xindex - 1; } else { x1 = xindex; x2 = xindex + 1; } double v1 = f[x1].value(y); double v2 = f[x2].value(y); double v = v1 + (x - xval[x1]) / (xval[x2] - xval[x1]) * (v2 - v1); int yindex = Arrays.binarySearch(yval, y); if (yindex < 0) yindex = -yindex - 2; double fmin = Math.min(fval[x1][yindex], fval[x2][yindex]); double fmax = Math.max(fval[x1][yindex], fval[x2][yindex]); if (yindex + 1 < yval.length) { fmin = Math.min(fmin, fval[x1][yindex + 1]); fmin = Math.min(fmin, fval[x2][yindex + 1]); fmax = Math.max(fmax, fval[x1][yindex + 1]); fmax = Math.max(fmax, fval[x2][yindex + 1]); } v = Math.max(v, fmin); v = Math.min(v, fmax); return v; }
From source file:eu.trentorise.game.sample.DemosInitializer.java
@PostConstruct private void initDemos() { Game g = null;//from w w w.jav a 2 s. c om boolean secProfileActive = Arrays.binarySearch(env.getActiveProfiles(), "sec") >= 0; if (secProfileActive) { LogHub.info(null, logger, "sec profile active..create sample game for every user"); for (AuthUser user : usersProvider.getUsers()) { g = gameFactory.createGame(null, null, null, user.getUsername()); if (g != null) { gameSrv.startupTasks(g.getId()); } } } else { LogHub.info(null, logger, "no-sec profile active..create sample game for default user"); // initialize demo-game for default user in no-sec env g = gameFactory.createGame(null, null, null, DefaultIdentityLookup.DEFAULT_USER); if (g != null) { gameSrv.startupTasks(g.getId()); } } }