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

@SuppressWarnings("unchecked")
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) 

Source Link

Document

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

Usage

From source file:net.testdriven.psiprobe.beans.LogResolverBean.java

public List<LogDestination> getLogDestinations(boolean all) {
    List<LogDestination> allAppenders = getAllLogDestinations();

    if (allAppenders != null) {
        ////from w  ww  . j  a  v a  2 s . co m
        // this list has to guarantee the order in which elements are added
        //
        List<LogDestination> uniqueList = new LinkedList<>();
        LogComparator cmp = new LogDestinationComparator(all);

        Collections.sort(allAppenders, cmp);
        for (LogDestination dest : allAppenders) {
            if (Collections.binarySearch(uniqueList, dest, cmp) < 0) {
                if (all || dest.getFile() == null || dest.getFile().exists()) {
                    uniqueList.add(new DisconnectedLogDestination(dest));
                }
            }
        }
        return uniqueList;
    }
    return null;
}

From source file:com.epam.dlab.mongo.ResourceItemList.java

/** Find and return the resource by resource id.
 * @param index index of the resource.//  w  w  w .  j av  a  2s  .c om
 */
public ResourceItem getById(String resourceId) {
    findItemById.resourceId = resourceId;
    int index = Collections.binarySearch(list, findItemById, compareByName);

    return (index < 0 ? null : list.get(index));
}

From source file:org.libreplan.business.util.ListSorter.java

private void insert(T element) {
    int position = Collections.binarySearch(list, element, comparator);
    assert position < 0 : "the object must not be in the list";
    position = (-position) - 1;/*from  ww w  .ja v  a2  s  .  c  om*/
    list.add(position, element);
}

From source file:org.apache.metron.profiler.client.stellar.IntervalPredicate.java

/**
 * Determine if x is in the set of intervals in O(log*n) time.
 * @param x//ww  w.  j  a v a2s  .c  o m
 * @return true if in the set of intervals and false otherwise.
 */
@Override
public boolean test(T x) {
    long ts = timestampTransformer.apply(x);
    int pos = Collections.binarySearch(intervals, Range.is(ts), INTERVAL_COMPARATOR);
    if (pos < 0) {
        pos = -pos - 1;
    }

    Optional<Range<Long>> right = pos >= 0 && pos < intervals.size() ? Optional.of(intervals.get(pos))
            : Optional.empty();
    Optional<Range<Long>> left = pos - 1 >= 0 && pos - 1 < intervals.size()
            ? Optional.of(intervals.get(pos - 1))
            : Optional.empty();
    return (right.isPresent() ? containsInclusive(right.get(), ts) : false)
            || (left.isPresent() ? containsInclusive(left.get(), ts) : false);
}

From source file:com.googlecode.psiprobe.beans.LogResolverBean.java

public List getLogDestinations(boolean all) {
    List allAppenders = getAllLogDestinations();

    if (allAppenders != null) {
        ////from   www .java  2  s.c  o m
        // this list has to guarantee the order in which elements are added
        //
        List uniqueList = new LinkedList();
        LogComparator cmp = new LogDestinationComparator(all);

        Collections.sort(allAppenders, cmp);
        for (int i = 0; i < allAppenders.size(); i++) {
            LogDestination dest = (LogDestination) allAppenders.get(i);
            if (Collections.binarySearch(uniqueList, dest, cmp) < 0) {
                if (all || dest.getFile() == null || dest.getFile().exists()) {
                    uniqueList.add(new DisconnectedLogDestination(dest));
                }
            }
        }
        return uniqueList;
    }
    return null;
}

From source file:org.oscarehr.common.dao.InboxResultsDao.java

/**
 * Populates ArrayList with labs attached to a consultation request
 *//*from www  .j a v a  2  s . c om*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public ArrayList populateHL7ResultsData(String demographicNo, String consultationId, boolean attached) {
    String sql = "SELECT hl7.label, hl7.lab_no, hl7.obr_date, hl7.discipline, hl7.accessionNum, hl7.final_result_count, patientLabRouting.id "
            + "FROM hl7TextInfo hl7, patientLabRouting " + "WHERE patientLabRouting.lab_no = hl7.lab_no "
            + "AND patientLabRouting.lab_type = 'HL7' AND patientLabRouting.demographic_no=" + demographicNo
            + " GROUP BY hl7.lab_no";

    String attachQuery = "SELECT consultdocs.document_no FROM consultdocs, patientLabRouting "
            + "WHERE patientLabRouting.id = consultdocs.document_no AND " + "consultdocs.requestId = "
            + consultationId
            + " AND consultdocs.doctype = 'L' AND consultdocs.deleted IS NULL ORDER BY consultdocs.document_no";

    ArrayList labResults = new ArrayList<LabResultData>();
    ArrayList attachedLabs = new ArrayList<LabResultData>();

    try {
        Query q = entityManager.createNativeQuery(attachQuery);

        List<Object[]> result = q.getResultList();
        for (Object[] r : result) {
            LabResultData lbData = new LabResultData(LabResultData.HL7TEXT);
            lbData.labPatientId = (String) r[0];
            attachedLabs.add(lbData);
        }

        LabResultData lbData = new LabResultData(LabResultData.HL7TEXT);
        LabResultData.CompareId c = lbData.getComparatorId();

        q = entityManager.createNativeQuery(sql);
        result = q.getResultList();
        for (Object[] r : result) {
            lbData.segmentID = (String) r[1];
            lbData.labPatientId = (String) r[6];
            lbData.dateTime = (String) r[2];
            lbData.discipline = (String) r[3];
            lbData.accessionNumber = (String) r[4];
            lbData.finalResultsCount = (Integer) r[5];
            lbData.label = (String) r[0];

            if (attached && Collections.binarySearch(attachedLabs, lbData, c) >= 0)
                labResults.add(lbData);
            else if (!attached && Collections.binarySearch(attachedLabs, lbData, c) < 0)
                labResults.add(lbData);

            lbData = new LabResultData(LabResultData.HL7TEXT);
        }
    } catch (Exception e) {
        logger.error("exception in HL7Populate", e);
    }

    return labResults;
}

From source file:org.jets3t.apps.cockpitlite.CLObjectTableModel.java

public String getObjectAclStatus(S3Object objectWithAcl) {
    synchronized (objectList) {
        int updateRow = Collections.binarySearch(objectList, objectWithAcl, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((S3Object) o1).getKey().compareToIgnoreCase(((S3Object) o2).getKey());
            }//ww  w .  j av a  2  s  .c  om
        });
        if (updateRow >= 0) {
            return (String) this.getValueAt(updateRow, 3);
        } else {
            return null;
        }
    }
}

From source file:it.attocchi.utils.ListUtils.java

public static <T> T find(List<T> aList, T key, Comparator<T> c) {
    T res = null;//www  .  j a  va 2 s .  c  o  m

    Collections.sort(aList, c);
    int index = Collections.binarySearch(aList, key, c);

    if (index >= 0) {
        res = aList.get(index);
    }

    return res;
}

From source file:psiprobe.beans.LogResolverBean.java

/**
 * Gets the log destinations.//  www .  j  a  va2 s . co m
 *
 * @param all the all
 * @return the log destinations
 */
public List<LogDestination> getLogDestinations(boolean all) {
    List<LogDestination> allAppenders = getAllLogDestinations();

    if (allAppenders != null) {
        //
        // this list has to guarantee the order in which elements are added
        //
        List<LogDestination> uniqueList = new LinkedList<>();
        LogComparator cmp = new LogDestinationComparator(all);

        Collections.sort(allAppenders, cmp);
        for (LogDestination dest : allAppenders) {
            if (Collections.binarySearch(uniqueList, dest, cmp) < 0) {
                if (all || dest.getFile() == null || dest.getFile().exists()) {
                    uniqueList.add(new DisconnectedLogDestination(dest));
                }
            }
        }
        return uniqueList;
    }
    return null;
}

From source file:com.icesoft.icefaces.tutorial.component.selectInputText.SelectInputTextBean.java

/**
 * Utility method for building the match list given the current value of the
 * SelectInputText component./*  w  w  w .ja  v a 2  s . com*/
 *
 * @param event
 */
private void setMatches(ValueChangeEvent event) {

    Object searchWord = event.getNewValue();
    int maxMatches = ((SelectInputText) event.getComponent()).getRows();
    List matchList = new ArrayList(maxMatches);

    try {

        int insert = Collections.binarySearch(dictionary, searchWord,
                SelectInputTextDictionary.LABEL_COMPARATOR);

        // less then zero if wer have a partial match
        if (insert < 0) {
            insert = Math.abs(insert) - 1;
        }

        for (int i = 0; i < maxMatches; i++) {
            // quit the match list creation if the index is larger then
            // max entries in the dictionary if we have added maxMatches.
            if ((insert + i) >= dictionary.size() || i >= maxMatches) {
                break;
            }
            SelectItem match = (SelectItem) dictionary.get(insert + i);
            if (match.getLabel().toLowerCase().startsWith(searchWord.toString().toLowerCase()))
                matchList.add(match);
        }
    } catch (Throwable e) {
        log.error("Erorr finding selectInputText matches", e);
    }
    // assign new matchList
    if (this.matchesList != null) {
        this.matchesList.clear();
        this.matchesList = null;
    }
    this.matchesList = matchList;
}