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.hyperic.hq.plugin.rabbitmq.manage.RabbitTransientResourceManager.java

public void syncServices(List<ServiceResource> rabbitResources) throws Exception {

    if (rabbitResources == null) {
        return;// w  w w . j  a  v  a 2s .c o m
    }

    int numResourcesDeleted = 0;

    try {
        Resource rabbitMQ = getRabbitMQServer();

        if (rabbitMQ == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not find a RabbitMQ server");
            }
            return;
        }

        List<String> resources = new ArrayList();
        for (ServiceResource s : rabbitResources) {
            logger.info(s.getType() + s.getName());
            resources.add(s.getType() + s.getName());
            if (logger.isDebugEnabled()) {
                logger.debug("HQ RabbitMQ service={" + "type=" + s.getName() + ", name=" + s.getName() + "}");
            }
        }

        Collections.sort(resources);
        for (Resource service : rabbitMQ.getResource()) {
            String sname = service.getResourcePrototype().getName() + service.getName();
            if (Collections.binarySearch(resources, sname) < 0) {
                commandsClient.deleteResource(service);
                logger.debug("HQ RabbitMQ service deleted={" + "type="
                        + service.getResourcePrototype().getName() + ", name=" + service.getName() + "}");
                numResourcesDeleted++;
            }
        }
    } catch (Exception e) {
        // TODO: log here?
        throw e;
    } finally {
        if (numResourcesDeleted > 0) {
            logger.info(numResourcesDeleted + " transient RabbitMQ services deleted");
        }
    }
}

From source file:edu.cornell.med.icb.goby.alignments.perms.PermutationWriter.java

/**
 * Append a piece of the permutation./*  w  w w  .  ja v  a2 s . c om*/
 *
 * @param permPiece map from query indices to small indices.
 * @throws IOException   If an error occurs writing the data to disk.
 */
public void append(final Int2IntMap permPiece) throws IOException {

    if (permPiece.isEmpty()) {
        return;
    }
    //final Int2IntSortedMap reverse=new Int2IntAVLTreeMap();

    final IntArrayList smallIndices = new IntArrayList();
    smallIndices.addAll(permPiece.values());
    Collections.sort(smallIndices);
    final int size = smallIndices.size();
    final int minSmallIndexInChunk = smallIndices.get(0);
    final int maxSmallIndexInChunk = smallIndices.get(size - 1);
    final IntArrayList largeIndices = new IntArrayList();
    largeIndices.size(permPiece.size());
    for (final int queryIndex : permPiece.keySet()) {
        final int smallIndex = permPiece.get(queryIndex);
        final int indexOf = Collections.binarySearch(smallIndices, smallIndex);
        largeIndices.set(indexOf, queryIndex);
    }
    chunk(smallIndices, minSmallIndexInChunk, maxSmallIndexInChunk, largeIndices);
    output.flush();
}

From source file:org.apache.asterix.om.typecomputer.impl.RecordMergeTypeComputer.java

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
        IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    IAType t0 = (IAType) env.getType(f.getArguments().get(0).getValue());
    IAType t1 = (IAType) env.getType(f.getArguments().get(1).getValue());
    boolean unknownable = TypeHelper.canBeUnknown(t0) || TypeHelper.canBeUnknown(t1);
    ARecordType recType0 = TypeComputeUtils.extractRecordType(t0);
    ARecordType recType1 = TypeComputeUtils.extractRecordType(t1);

    if (recType0 == null || recType1 == null) {
        throw new AlgebricksException(
                "record-merge expects possibly NULL records as arguments, but got (" + t0 + ", " + t1 + ")");
    }//  www  . ja  v  a2  s .co m

    List<String> resultFieldNames = new ArrayList<>();
    for (String fieldName : recType0.getFieldNames()) {
        resultFieldNames.add(fieldName);
    }
    Collections.sort(resultFieldNames);

    List<IAType> resultFieldTypes = new ArrayList<>();
    for (String fieldName : resultFieldNames) {
        if (recType0.getFieldType(fieldName).getTypeTag() == ATypeTag.RECORD) {
            ARecordType nestedType = (ARecordType) recType0.getFieldType(fieldName);
            //Deep Copy prevents altering of input types
            resultFieldTypes.add(nestedType.deepCopy(nestedType));
        } else {
            resultFieldTypes.add(recType0.getFieldType(fieldName));
        }
    }

    List<String> additionalFieldNames = new ArrayList<>();
    List<IAType> additionalFieldTypes = new ArrayList<>();
    String fieldNames[] = recType1.getFieldNames();
    IAType fieldTypes[] = recType1.getFieldTypes();
    for (int i = 0; i < fieldNames.length; ++i) {
        int pos = Collections.binarySearch(resultFieldNames, fieldNames[i]);
        if (pos >= 0) {
            IAType resultFieldType = resultFieldTypes.get(pos);
            if (resultFieldType.getTypeTag() != fieldTypes[i].getTypeTag()) {
                throw new AlgebricksException("Duplicate field " + fieldNames[i] + " encountered");
            }
            try {
                // Assuming fieldTypes[i].getTypeTag() = resultFieldType.getTypeTag()
                if (fieldTypes[i].getTypeTag() == ATypeTag.RECORD) {
                    resultFieldTypes.set(pos, mergedNestedType(fieldTypes[i], resultFieldType));
                }
            } catch (AsterixException e) {
                throw new AlgebricksException(e);
            }

        } else {
            additionalFieldNames.add(fieldNames[i]);
            additionalFieldTypes.add(fieldTypes[i]);
        }
    }

    resultFieldNames.addAll(additionalFieldNames);
    resultFieldTypes.addAll(additionalFieldTypes);
    String resultTypeName = "merged(" + recType0.getTypeName() + ", " + recType1.getTypeName() + ")";
    boolean isOpen = recType0.isOpen() || recType1.isOpen();

    IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[] {}),
            resultFieldTypes.toArray(new IAType[] {}), isOpen);

    if (unknownable) {
        resultType = AUnionType.createUnknownableType(resultType);
    }
    return resultType;
}

From source file:dk.statsbiblioteket.netark.dvenabler.wrapper.SortedSetDocValuesWrapper.java

public long getOrd(String value) {
    int ord = Collections.binarySearch(values, new BytesRef(value));
    if (ord < 0) {
        throw new IllegalStateException("The ord for value '" + value + "' for docID " + docID + " in field '"
                + field + "' could not be located but should always be present");
    }//from  w w  w  .ja v  a 2  s. com
    return ord;
}

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

public static <T extends Comparable<T>> T find(List<T> aList, T key) {
    T res = null;/*from   w w w .  ja  v a 2s .co  m*/

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

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

    return res;
}

From source file:net.tradelib.core.Series.java

public double get(LocalDateTime ts, int colId) {
    int rowId = Collections.binarySearch(index, ts);
    if (rowId < 0) {
        throw new BadIndexException(
                ts.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " is not in the index");
    }//from  w w  w .j  a  va2 s .co m
    return data.get(colId).get(rowId);
}

From source file:edu.uci.ics.asterix.om.typecomputer.impl.RecordMergeTypeComputer.java

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
        IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    IAType t0 = (IAType) env.getType(f.getArguments().get(0).getValue());
    IAType t1 = (IAType) env.getType(f.getArguments().get(1).getValue());
    boolean nullable = TypeHelper.canBeNull(t0) || TypeHelper.canBeNull(t1);
    ARecordType recType0 = extractRecordType(t0);
    ARecordType recType1 = extractRecordType(t1);

    if (recType0 == null || recType1 == null) {
        throw new AlgebricksException(
                "record-merge expects possibly NULL records as arguments, but got (" + t0 + ", " + t1 + ")");
    }//from  w  w w .  j av  a2  s.  co m

    List<String> resultFieldNames = new ArrayList<>();
    for (String fieldName : recType0.getFieldNames()) {
        resultFieldNames.add(fieldName);
    }
    Collections.sort(resultFieldNames);
    List<IAType> resultFieldTypes = new ArrayList<>();
    for (String fieldName : resultFieldNames) {
        try {
            if (recType0.getFieldType(fieldName).getTypeTag() == ATypeTag.RECORD) {
                ARecordType nestedType = (ARecordType) recType0.getFieldType(fieldName);
                //Deep Copy prevents altering of input types
                resultFieldTypes.add(nestedType.deepCopy(nestedType));
            } else {
                resultFieldTypes.add(recType0.getFieldType(fieldName));
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    List<String> additionalFieldNames = new ArrayList<>();
    List<IAType> additionalFieldTypes = new ArrayList<>();
    for (int i = 0; i < recType1.getFieldNames().length; ++i) {
        String fieldName = recType1.getFieldNames()[i];
        IAType fieldType = recType1.getFieldTypes()[i];
        int pos = Collections.binarySearch(resultFieldNames, fieldName);
        if (pos >= 0) {
            try {
                resultFieldTypes.set(pos, mergedNestedType(fieldType, resultFieldTypes.get(pos)));
            } catch (AsterixException e) {
                throw new AlgebricksException(e);
            }

        } else {
            additionalFieldNames.add(fieldName);
            additionalFieldTypes.add(fieldType);
        }
    }

    resultFieldNames.addAll(additionalFieldNames);
    resultFieldTypes.addAll(additionalFieldTypes);
    String resultTypeName = "merged(" + recType0.getTypeName() + ", " + recType1.getTypeName() + ")";
    boolean isOpen = recType0.isOpen() || recType1.isOpen();
    IAType resultType = null;
    try {
        resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[] {}),
                resultFieldTypes.toArray(new IAType[] {}), isOpen);
    } catch (AsterixException | HyracksDataException e) {
        throw new AlgebricksException(e);
    }
    ;

    if (nullable) {
        resultType = AUnionType.createNullableType(resultType);
    }
    return resultType;
}

From source file:org.jfree.chart.demo.PerformanceTest1.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main3(String as[]) {
    ArrayList arraylist = new ArrayList();
    Millisecond millisecond = new Millisecond();
    for (int i = 0; i < 200; i++) {
        millisecond = (Millisecond) millisecond.next();
        arraylist.add(millisecond);/*from   ww  w.  j a  v a2 s  .c  om*/
    }

    for (int j = 0; j < 2000; j++) {
        long l = System.currentTimeMillis();
        Collections.binarySearch(arraylist, new Millisecond());
        long l1 = System.currentTimeMillis();
        System.out.println(j + " --> " + (l1 - l) + " (" + Runtime.getRuntime().freeMemory() + " / "
                + Runtime.getRuntime().totalMemory() + ")");
    }

}

From source file:com.trenako.services.WishListsServiceImpl.java

private boolean addDefaultWishList(List<WishList> results, Account owner) {
    if (results.size() == 0) {
        return true;
    }//from ww w.j av a 2  s . c om

    return Collections.binarySearch(results, WishList.defaultList(owner)) < 0;
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.PeakAnnotationReportPanel.java

protected void updateView() {
    // clear//from  w  w  w  .ja va 2s. c o  m
    //for( int i=1; i<theDataset.getSeriesCount(); i++ )
    //theDataset.removeSeries(theDataset.getSeriesKey(i-1));

    for (int l = 0; l < theDocument.getNoStructures(); l++)
        theDataset.removeSeries("series" + l);

    for (int l = 0; l < theDocument.getNoStructures(); l++) {

        double[][] data = new double[2][];
        data[0] = new double[theDocument.getNoPeaks()];
        data[1] = new double[theDocument.getNoPeaks()];

        // get intensities and order them
        LinkedList<Double> sortedList = new LinkedList<Double>();
        for (int i = 0; i < theDocument.getNoPeaks(); i++) {
            double intensity = theDocument.getPeak(i).getIntensity();
            if (theDocument.getAnnotations(i, l).size() == 0)
                intensity = 0.;

            int index = Collections.binarySearch(sortedList, intensity);
            if (index < 0)
                index = -index - 1;
            sortedList.add(index, intensity);
        }

        // set data
        for (int i = 0; i < theDocument.getNoPeaks(); i++) {
            data[0][i] = i;
            data[1][i] = sortedList.get(theDocument.getNoPeaks() - i - 1);
        }
        theDataset.addSeries("series" + l, data);
    }
}