Example usage for java.util Collections binarySearch

List of usage examples for java.util Collections binarySearch

Introduction

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

Prototype

public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) 

Source Link

Document

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

Usage

From source file:org.usergrid.persistence.cassandra.SimpleIndexBucketLocatorImpl.java

/**
 * Get the next token in the ring for this big int.
 * //from   w  w  w  .  j ava  2  s.c  o  m
 * @param entityId
 * @return
 */
private String getClosestToken(UUID entityId) {
    BigInteger location = new BigInteger(md5(bytes(entityId)));
    location = location.abs();

    int index = Collections.binarySearch(buckets, location);

    if (index < 0) {
        index = (index + 1) * -1;
    }

    // mod if we need to wrap
    index = index % size;

    return bucketsString.get(index);

}

From source file:edu.ku.brc.specify.tasks.subpane.PickListProcessor.java

public boolean doEvaluate() {
    String nameStr = name.getText();
    return isNotEmpty(nameStr) && Collections.binarySearch(titlesList, nameStr) < 0;
}

From source file:com.hp.alm.ali.idea.entity.tree.EntityNode.java

private void mergeChildren(List<EntityNode> children) {
    for (int i = 0; i < this.filtered.size(); i++) {
        EntityNode child = this.filtered.get(i);
        if (!children.contains(child) || (!child.matchesFilter() && !child.isChildMatching())) {
            this.filtered.remove(i);
            model.nodesWereRemoved(this, new int[] { i }, new EntityNode[] { child });
            --i;/*from w ww .j  a  v a2 s  .  com*/
        }
    }
    for (EntityNode child : children) {
        if (!filtered.contains(child)) {
            if (child.matchesFilter() || child.isChildMatching()) {
                int i = Collections.binarySearch(this.filtered, child);
                this.filtered.add(-(i + 1), child);
                model.nodesWereInserted(this, new int[] { -(i + 1) });
            }
        }
    }
}

From source file:org.jboss.dashboard.ui.panel.dataSourceManagement.DataSourceManagementTableListFormatter.java

public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws FormatterException {
    try {/*from   ww w . j ava  2s. c o m*/
        //get all tables of the datasource that has been edited
        DataSourceManagementHandler handler = getDataSourceManagementHandler();
        if (handler.getINTROSPECT_MODE()) {
            String dsName = handler.getDS_EDIT();
            setAttribute("dsName", dsName);
            renderFragment("outputTitleForm");
            if (handler.getINTROSPECT_RESULT().equals(handler.RESULT_OK)) {
                renderFragment("outputTitleTableTD");
                List existingTableEntries = handler.getIntrospectedTables(dsName);
                List selectedTablesList = DataSourceTableManager.lookup().getSelectedTablesName(dsName);
                Collections.sort(selectedTablesList);
                int i, index;
                String checked, trClass, currentTrClass;
                for (i = 0; i < existingTableEntries.size(); i++) {
                    checked = "";
                    DataSourceTableEntry entry = (DataSourceTableEntry) existingTableEntries.get(i);

                    if ((i % 2) == 0)
                        trClass = TR_ALT_CLASS;
                    else
                        trClass = "";

                    currentTrClass = trClass;
                    //search current entrie in selected list
                    if (selectedTablesList.size() > 0) {
                        index = Collections.binarySearch(selectedTablesList, entry.getName());
                        if (index >= 0) {
                            checked = "checked";
                            selectedTablesList.remove(index);
                            currentTrClass = TR_ON_CLASS;
                        }
                    }
                    setAttribute("checked", checked);
                    setAttribute("tableName", entry.getName());
                    setAttribute("tableIndex", i);
                    setAttribute("currentTrClass", currentTrClass);
                    setAttribute("trClass", trClass);

                    renderFragment("outputRow");
                }
                renderFragment("outputEndRow");
                renderFragment("outputBeginButtonsTR");
                renderFragment("outputSaveButton");
                renderFragment("outputCancelButton");
                renderFragment("outputEndButtonsTR");
                setAttribute("numberOfTables", i);
                renderFragment("outputEnd");
            } else {
                setAttribute("errorDescription", handler.getINTROSPECT_RESULT());
                renderFragment("outputDSError");
                renderFragment("outputBeginButtonsTR");
                renderFragment("outputCancelButton");
                renderFragment("outputEndButtonsTR");
                renderFragment("outputEnd");
            }
        }
    } catch (Exception e) {
        log.error("Error: ", e);
    }
}

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxDocument.java

/**
 * Find the token at a given position.  May return null if no token is
 * found (whitespace skipped) or if the position is out of range.
 * /*from   ww w  .  jav  a 2 s . c  om*/
 * @param pos
 * @return
 */
public Token getTokenAt(int pos) {
    if (tokens == null || tokens.isEmpty() || pos > getLength()) {
        return null;
    }

    Token token = null;
    Token tKey = new Token(TokenType.DEFAULT, pos, 1);
    @SuppressWarnings("unchecked")
    int ndx = Collections.binarySearch((List) tokens, tKey);
    if (ndx < 0) {
        // so, start from one before the token where we should be...
        // -1 to get the location, and another -1 to go back..
        ndx = (-ndx - 1 - 1 < 0) ? 0 : (-ndx - 1 - 1);
        Token t = tokens.get(ndx);
        if ((t.start <= pos) && (pos <= (t.start + t.length))) {
            token = t;
        }
    } else {
        token = tokens.get(ndx);
    }

    return token;
}

From source file:com.feedzai.fos.api.CategoricalAttribute.java

@Override
protected double parse(Object original) throws FOSException {
    String value = original.toString();
    int index = Collections.binarySearch(categoricalInstances, value);

    if (index < 0) {
        throw new FOSException(String.format("Failed to parse %s", original));
    }/* ww  w.  jav  a 2s  .c  o  m*/

    return index;
}

From source file:ome.services.db.SelfCorrectingDataSource.java

/**
 * First removes all entries in {@link #errorTimes} that are older than some
 * given time and then uses the remaining number of errors to determine the
 * backoff : (#^1/2)*1000 milliseconds./*w  ww  .ja  v  a2  s . c  o  m*/
 */
protected long markAndSweep() {
    final long timeAgo = System.currentTimeMillis() - errorTimeout;
    synchronized (errorTimes) {
        int location = Collections.binarySearch(errorTimes, timeAgo);
        log.info("Found location in errorTimes: " + location);
        if (location < 0) {
            location = -location - 1; // search returns -insertion-1
        }

        List<Long> subList = new ArrayList<Long>(errorTimes.subList(location, errorTimes.size()));

        int eSize = errorTimes.size();
        int sSize = subList.size();

        log.info("Removing " + (eSize - sSize) + " from errorTimes");
        errorTimes.clear();
        errorTimes.addAll(subList);
        log.warn("Registering error with list: Current size: " + sSize);
        errorTimes.add(System.currentTimeMillis());
        return calculateBackOff(sSize);
    }
}

From source file:org.drugis.addis.presentation.wizard.SelectableOptionsModel.java

public ModifiableHolder<Boolean> getSelectedModel(E option) {
    int idx = Collections.binarySearch(d_options, new Option<E>(option, false));
    if (idx >= 0) {
        return d_options.get(idx).toggle;
    }//  w  w w .j  a  v  a 2  s .  c om
    return null;
}

From source file:org.janusgraph.diskstorage.keycolumnvalue.SliceQuery.java

public EntryList getSubset(final SliceQuery otherQuery, final EntryList otherResult) {
    assert otherQuery.subsumes(this);
    int pos = Collections.binarySearch(otherResult, sliceStart);
    if (pos < 0)
        pos = -pos - 1;// w w  w.ja v a  2 s .c o  m

    List<Entry> result = new ArrayList<Entry>();
    for (; pos < otherResult.size() && result.size() < getLimit(); pos++) {
        Entry e = otherResult.get(pos);
        if (e.getColumnAs(StaticBuffer.STATIC_FACTORY).compareTo(sliceEnd) < 0)
            result.add(e);
        else
            break;
    }
    return StaticArrayEntryList.of(result);
}

From source file:org.opendatakit.utilities.NameUtil.java

/**
 * Determines whether or not the given name is valid for a user-defined
 * entity in the database. Valid names are determined to not begin with a
 * single underscore, not to begin with a digit, and to contain only unicode
 * appropriate word characters./*from www. ja  v  a2  s . co  m*/
 *
 * @param name The string to be looked up in the list of definitely not allowed words
 * @return true if valid else false
 */
public static boolean isValidUserDefinedDatabaseName(String name) {
    boolean matchHit = letterFirstPattern.matcher(name).matches();
    // TODO: uppercase is bad...
    boolean reserveHit = Collections.binarySearch(reservedNamesSortedList, name.toUpperCase(Locale.US)) >= 0;
    return !reserveHit && matchHit;
}