Example usage for java.util Arrays binarySearch

List of usage examples for java.util Arrays binarySearch

Introduction

In this page you can find the example usage for java.util Arrays binarySearch.

Prototype

public static int binarySearch(Object[] a, Object key) 

Source Link

Document

Searches the specified array for the specified object using the binary search algorithm.

Usage

From source file:com.krawler.common.util.SchedulingUtilities.java

public static String getNextWorkingDay(String currDate, int[] NWD, String[] CH) {
    Calendar c1 = Calendar.getInstance();
    Date stdate = c1.getTime();/*from w  ww . java2s  .  co  m*/
    java.text.SimpleDateFormat sdfLong = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
    int n = 1;
    try {
        c1.setTime(sdfLong.parse(currDate));
        while (n > 0) {
            c1.add(Calendar.DATE, 1);
            if (Arrays.binarySearch(NWD, (c1.get(Calendar.DAY_OF_WEEK) - 1)) > -1
                    || Arrays.binarySearch(CH, sdf.format(c1.getTime())) > -1) {
                n++;
            } else {
                n = 0;
            }
            stdate = c1.getTime();
        }
    } catch (Exception e) {
        throw ServiceException.FAILURE("Exception while calculating next working day : " + e.getMessage(), e);
    } finally {
        return sdfLong.format(stdate);
    }
}

From source file:forge.deck.generation.DeckGenerator2Color.java

public DeckGenerator2Color(IDeckGenPool pool0, DeckFormat format0, final String clr1, final String clr2) {
    super(pool0, format0);
    int c1 = MagicColor.fromName(clr1);
    int c2 = MagicColor.fromName(clr2);

    format0.adjustCMCLevels(cmcLevels);/* w  w  w. ja  v a2 s . co  m*/

    if (c1 == 0 && c2 == 0) {
        int color1 = r.nextInt(5);
        int color2 = (color1 + 1 + r.nextInt(4)) % 5;
        colors = ColorSet.fromMask(MagicColor.WHITE << color1 | MagicColor.WHITE << color2);
    } else if (c1 == 0 || c2 == 0) {
        byte knownColor = (byte) (c1 | c2);
        int color1 = Arrays.binarySearch(MagicColor.WUBRG, knownColor);
        int color2 = (color1 + 1 + r.nextInt(4)) % 5;
        colors = ColorSet.fromMask(MagicColor.WHITE << color1 | MagicColor.WHITE << color2);
    } else {
        colors = ColorSet.fromMask(c1 | c2);
    }
}

From source file:org.apache.tika.module.command.internal.Activator.java

@Override
public void start(BundleContext context) throws Exception {

    configureLogging(context);/*  ww  w .  jav a2s .  co m*/

    boolean batchMode = Boolean.parseBoolean(context.getProperty("org.apache.tika.batch.mode"));

    String commandString = context.getProperty("org.apache.tika.command.launchArgs");

    String[] command = new String[0];
    if (StringUtils.isNotBlank(commandString)) {
        command = commandString.split("\\n");
    }

    boolean fork = Arrays.binarySearch(command, "-f") >= 0 || Arrays.binarySearch(command, "--fork") >= 0;
    ServiceReference<TikaServiceFactory> tikaServiceFactoryRef = context
            .getServiceReference(TikaServiceFactory.class);

    if (fork) {
        forkProcess(command);
        stopFramework(context);
    } else if (batchMode) {
        runBatch(context, command);
        stopFramework(context);
    } else {
        runCLI(context, command, tikaServiceFactoryRef);

    }
}

From source file:org.jblogcms.core.common.service.ItemServiceToolImpl.java

@Override
public List<I> addItemRelationsToItemList(List<I> items, Long accountId) {
    if (items != null && !items.isEmpty() && accountId != null) {
        List<IREL> itemRelations = itemRelationRepository.getItemRelations(items, accountId);

        if (!itemRelations.isEmpty()) {
            long[] sortedItemRelations = new long[itemRelations.size()];
            int i = 0;
            for (ItemRelation itemRelation : itemRelations) {
                sortedItemRelations[i++] = itemRelation.getItem().getId();
            }//from   ww w  .ja va2 s.  com
            Arrays.sort(sortedItemRelations);

            for (I item : items) {
                int k = Arrays.binarySearch(sortedItemRelations, item.getId());
                if (k >= 0) {
                    item.setCurrentItemRelation(itemRelations.get(k));
                }
            }
        }
    }
    return items;
}

From source file:com.opengamma.analytics.math.FunctionUtils.java

/**
 * Find the index of a <b>sorted</b> set that is less than or equal to a given value. If the given value is lower than the lowest member (i.e. the first)
 *  of the set, zero is returned.  This uses Arrays.binarySearch 
 * @param set a <b>sorted</b> array of numbers. 
 * @param value The value to search for//  w w w.  jav a2s  .co  m
 * @return index in the array
 */
public static int getLowerBoundIndex(final double[] set, final double value) {
    final int n = set.length;
    if (value < set[0]) {
        return 0;
    }
    if (value > set[n - 1]) {
        return n - 1;
    }
    int index = Arrays.binarySearch(set, value);
    if (index >= 0) {
        // Fast break out if it's an exact match.
        return index;
    }
    if (index < 0) {
        index = -(index + 1);
        index--;
    }
    return index;
}

From source file:org.catrobat.catroid.pocketmusic.ui.TrackView.java

private void initializeRows() {
    if (!trackRowViews.isEmpty()) {
        removeAllViews();//w  ww  . j a v  a 2s . co  m
        trackRowViews.clear();
    }
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1.0f);
    for (int i = 0; i < ROW_COUNT; i++) {
        boolean isBlackRow = Arrays.binarySearch(BLACK_KEY_INDICES, i) > -1;
        NoteName noteName = NoteName.getNoteNameFromMidiValue(NoteName.C1.getMidi() + i);
        trackRowViews.add(new TrackRowView(getContext(), trackGrid.getBeat(), isBlackRow, noteName,
                trackGrid.getGridRowForNoteName(noteName), this));
        addView(trackRowViews.get(i), params);
    }
}

From source file:org.apache.sysml.runtime.transform.encode.Encoder.java

/**
 * Indicates if this encoder is applicable for the given column ID,
 * i.e., if it is subject to this transformation.
 * /* ww w .  j av a  2 s.c  o  m*/
 * @param colID column ID
 * @return true if encoder is applicable for given column
 */
public int isApplicable(int colID) {
    if (_colList == null)
        return -1;
    int idx = Arrays.binarySearch(_colList, colID);
    return (idx >= 0 ? idx : -1);
}

From source file:com.rover12421.shaka.smali.baksmali.baksmaliMainAj.java

@Around("execution(* org.jf.baksmali.main.main(..))" + "&& args(args)")
public void main(ProceedingJoinPoint joinPoint, String[] args) throws Throwable {
    try {/*from  w w  w .ja v a  2 s .c om*/
        CommandLineParser parser = new PosixParser();
        CommandLine cli = parser.parse(options(), args);
        if (CommandLineArgEnum.InlieMethodResolverFromFile.hasMatch(cli)) {
            ShakaBaksmaliOption.setInlineMethodResolverFile(
                    cli.getOptionValue(CommandLineArgEnum.InlieMethodResolverFromFile.getOpt()));
            int index = Arrays.binarySearch(args, CommandLineArgEnum.InlieMethodResolverFromFile.getOpt());
            if (index < 0) {
                index = Arrays.binarySearch(args, CommandLineArgEnum.InlieMethodResolverFromFile.getLongOpt());
            }

            if (index >= 0) {
                String[] nArgs = new String[args.length - 2];
                if (index > 0) {
                    System.arraycopy(args, 0, nArgs, 0, index);
                }
                System.arraycopy(args, index + 2, nArgs, index, nArgs.length - index);
                joinPoint.proceed(nArgs);
            } else {
                LogHelper.warning("args error!!!");
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

    joinPoint.proceed(joinPoint.getArgs());
}

From source file:com.akop.bach.service.XboxLiveServiceClient.java

private boolean areArraysEquivalent(long[] arrayOne, long[] arrayTwo) {
    if (arrayOne == null || arrayTwo == null)
        return arrayOne == arrayTwo;

    if (arrayOne.length != arrayTwo.length)
        return false;

    Arrays.sort(arrayTwo);/*ww w.jav a2s .co  m*/
    for (int i = 0, n = arrayOne.length; i < n; i++)
        if (Arrays.binarySearch(arrayTwo, arrayOne[i]) < 0)
            return false;

    return true;
}

From source file:de.tud.inf.db.sparqlytics.model.Measure.java

/**
 * Creates a new measure with the given name, seed pattern, numeric
 * expression and aggregation function./*from  w  w  w.  ja  v a2s . co  m*/
 *
 * @param name                  the name of the measure
 * @param seedPattern           the seed pattern connecting the numeric
 *                              expression with the facts
 * @param expression            the expression to derive numeric values from
 *                              the facts
 * @param aggregationFunction   the aggregation function to apply to the
 *                              numeric values
 *
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if the numeric expression references a
 *                                  variable not present in the seed pattern
 *                                  or if the aggregation function is
 *                                  unsupported
 */
public Measure(final String name, final Element seedPattern, final Expr expression,
        final String aggregationFunction) {
    super(name);
    if (expression == null || aggregationFunction == null) {
        throw new NullPointerException();
    } else {
        this.aggregationFunction = aggregationFunction.toUpperCase();
        int index = Arrays.binarySearch(AGGREGATION_FUNCTIONS, this.aggregationFunction);
        if (index < 0) {
            throw new IllegalArgumentException("Unsupported aggregation function: " + aggregationFunction);
        }
    }
    List<Var> vars = new LinkedList<>();
    ExprVars.varsMentioned(vars, expression);
    vars.removeAll(PatternVars.vars(seedPattern));
    if (!vars.isEmpty()) {
        throw new IllegalArgumentException(
                "Expression references unknown variable(s): " + StringUtils.join(vars, ", "));
    }
    this.seedPattern = seedPattern;
    this.expression = expression;
}