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.puppycrawl.tools.checkstyle.TreeWalker.java

/**
 * Register a check for a given configuration.
 * @param check the check to register/*from   w  w  w  .  jav  a 2 s . co  m*/
 * @throws CheckstyleException if an error occurs
 */
private void registerCheck(Check check) throws CheckstyleException {
    final int[] tokens;
    final Set<String> checkTokens = check.getTokenNames();
    if (!checkTokens.isEmpty()) {
        tokens = check.getRequiredTokens();

        //register configured tokens
        final int[] acceptableTokens = check.getAcceptableTokens();
        Arrays.sort(acceptableTokens);
        for (String token : checkTokens) {
            try {
                final int tokenId = TokenTypes.getTokenId(token);
                if (Arrays.binarySearch(acceptableTokens, tokenId) >= 0) {
                    registerCheck(token, check);
                }
                // TODO: else log warning
            } catch (final IllegalArgumentException ex) {
                throw new CheckstyleException("illegal token \"" + token + "\" in check " + check, ex);
            }
        }
    } else {
        tokens = check.getDefaultTokens();
    }
    for (int element : tokens) {
        registerCheck(element, check);
    }
    if (check.isCommentNodesRequired()) {
        commentChecks.add(check);
    } else {
        ordinaryChecks.add(check);
    }
}

From source file:dk.netarkivet.wayback.batch.copycode.NetarchiveSuiteUURIFactory.java

/**
 * Test of whether passed String has an allowed URI scheme.
 * First tests if likely scheme suffix.  If so, we then test if its one of
 * the supported schemes./* w w  w.jav a 2 s  .c o m*/
 * @param possibleUrl URL string to examine.
 * @return True if passed string looks like it could be an URL.
 */
public static boolean hasSupportedScheme(String possibleUrl) {
    boolean hasScheme = UURI.hasScheme(possibleUrl);
    if (!hasScheme || NetarchiveSuiteUURIFactory.factory.schemes == null) {
        return hasScheme;
    }
    String tmpStr = possibleUrl.substring(0, possibleUrl.indexOf(':'));
    return Arrays.binarySearch(NetarchiveSuiteUURIFactory.factory.schemes, tmpStr) >= 0;
}

From source file:bin.spider.frame.uri.UURIFactory.java

/**
 * Test of whether passed String has an allowed URI scheme.
 * First tests if likely scheme suffix.  If so, we then test if its one of
 * the supported schemes./* www . java2s.  c o  m*/
 * @param possibleUrl URL string to examine.
 * @return True if passed string looks like it could be an URL.
 */
public static boolean hasSupportedScheme(String possibleUrl) {
    boolean hasScheme = UURI.hasScheme(possibleUrl);
    if (!hasScheme || UURIFactory.factory.schemes == null) {
        return hasScheme;
    }
    String tmpStr = possibleUrl.substring(0, possibleUrl.indexOf(':'));
    return Arrays.binarySearch(UURIFactory.factory.schemes, tmpStr) >= 0;
}

From source file:com.cloudera.sqoop.manager.NetezzaImportManualTest.java

@Test
public void testListTables() throws IOException {
    SqoopOptions options = new SqoopOptions(NetezzaTestUtils.getNZConnectString(), getTableName());
    options.setUsername(NetezzaTestUtils.getNZUser());
    options.setPassword(NetezzaTestUtils.getNZPassword());

    ConnManager mgr = new NetezzaManager(options);
    String[] tables = mgr.listTables();
    Arrays.sort(tables);// w  w  w.ja  v  a  2 s.c om
    assertTrue(getTableName() + " is not found!", Arrays.binarySearch(tables, getTableName()) >= 0);
}

From source file:gda.device.detector.mythen.data.MythenDataFileUtils.java

protected static int getExclusiveIndexForMaxIncludedAngle(double[] angles, double maxAngle) {
    int maxPos = Arrays.binarySearch(angles, maxAngle);
    return (maxPos < 0) ? -(maxPos + 1) : maxPos + 1;
}

From source file:org.apache.lucene.index.collocations.CollocationExtractor.java

private int recordAllPositionsOfTheTermInCurrentDocumentBitset(int docSeq, Term term, BitSet termPos, Terms tv,
        String[] terms) throws IOException {
    // first record all of the positions of the term in a bitset which represents terms in the current doc.
    int index = Arrays.binarySearch(terms, term.text());
    if (index >= 0) { // found
        // Bits liveDocs = MultiFields.getLiveDocs(this.reader);
        // int[] pos = tpv.getTermPositions(index);
        DocsAndPositionsEnum dpe = MultiFields.getTermPositionsEnum(this.reader, null, this.fieldName,
                new BytesRef(terms[index]));
        dpe.advance(docSeq);//from   ww  w.j  a v  a2  s . c o m
        // remember all positions of the term in this doc
        for (int j = 0; j < dpe.freq(); j++) {
            termPos.set(dpe.nextPosition());
        }
    }
    return index;
}

From source file:org.ligoj.app.plugin.vm.aws.VmAwsPluginResource.java

/**
 * Build a described {@link AwsVm} bean from a XML VMRecord entry.
 *//*  w w  w.j  a  va  2  s . c  om*/
private AwsVm toVm(final Element record) {
    final AwsVm result = new AwsVm();
    result.setId(xml.getTagText(record, "instanceId"));
    result.setName(Objects.toString(getName(record), result.getId()));
    result.setDescription(getResourceTag(record, "description"));
    final int state = getEc2State(record);
    result.setStatus(CODE_TO_STATUS.get(state));
    result.setBusy(Arrays.binarySearch(BUSY_CODES, state) >= 0);
    result.setVpc(xml.getTagText(record, "vpcId"));
    result.setAz(
            xml.getTagText((Element) record.getElementsByTagName("placement").item(0), "availabilityZone"));

    final InstanceType type = instanceTypes.get(xml.getTagText(record, "instanceType"));
    // Instance type details
    result.setRam(Optional.ofNullable(type).map(InstanceType::getRam).map(m -> (int) (m * 1024d)).orElse(0));
    result.setCpu(Optional.ofNullable(type).map(InstanceType::getCpu).orElse(0));
    result.setDeployed(result.getStatus() == VmStatus.POWERED_ON);
    return result;
}

From source file:com.music.util.music.ChordUtils.java

private static boolean isScaleChord(int[] chordType, Scale scale, int root) {
    int[] supposedChord = new int[3];
    fillChord(supposedChord, chordType, root);
    for (int note : supposedChord) {
        if (Arrays.binarySearch(scale.getDefinition(), note) < 0) {
            return false;
        }/*w  ww . ja  v a 2 s  .  c o  m*/
    }
    return true;
}

From source file:acromusashi.stream.ml.clustering.kmeans.KmeansCalculator.java

/**
 * KMeans++?????/*  w w  w.  ja v  a2  s .co  m*/
 * 
 * @param basePoints ??
 * @param clusterNum 
 * @return 
 */
public static List<KmeansPoint> createInitialCentroids(List<KmeansPoint> basePoints, int clusterNum) {
    Random random = new Random();
    List<KmeansPoint> resultList = Lists.newArrayList();
    // ??????????
    List<KmeansPoint> pointList = Lists.newArrayList(basePoints);
    KmeansPoint firstCentroid = pointList.remove(random.nextInt(pointList.size()));
    resultList.add(firstCentroid);

    double[] dxs;
    // KMeans++??????
    // ??1????????1????
    for (int centroidIndex = 1; centroidIndex < clusterNum; centroidIndex++) {
        // ?????????????
        dxs = computeDxs(pointList, resultList);

        // ??????????
        double r = random.nextDouble() * dxs[dxs.length - 1];
        int next = Arrays.binarySearch(dxs, r);
        int index = 0;
        if (next > 0) {
            index = next - 1;
        } else if (next < 0) {
            index = COMPENSATE_INDEX - next;
        }

        while (index > 0 && resultList.contains(pointList.get(index))) {
            index = index - 1;
        }

        resultList.add(pointList.get(index));
    }

    return resultList;
}

From source file:org.apache.cayenne.unit.di.server.SchemaBuilder.java

private void dbEntitiesFilter(List<DbEntity> entities) {
    // filter various unsupported tests...

    // LOBs/*from  w  w  w  . java  2s . co  m*/
    boolean excludeLOB = !unitDbAdapter.supportsLobs();
    boolean excludeBinPK = !unitDbAdapter.supportsBinaryPK();
    if (excludeLOB || excludeBinPK) {

        List<DbEntity> filtered = new ArrayList<DbEntity>();

        for (DbEntity ent : entities) {

            // check for LOB attributes
            if (excludeLOB) {
                if (Arrays.binarySearch(EXTRA_EXCLUDED_FOR_NO_LOB, ent.getName()) >= 0) {
                    continue;
                }

                boolean hasLob = false;
                for (final DbAttribute attr : ent.getAttributes()) {
                    if (attr.getType() == Types.BLOB || attr.getType() == Types.CLOB) {
                        hasLob = true;
                        break;
                    }
                }

                if (hasLob) {
                    continue;
                }
            }

            // check for BIN PK
            if (excludeBinPK) {
                boolean skip = false;
                for (final DbAttribute attr : ent.getAttributes()) {
                    // check for BIN PK or FK to BIN Pk
                    if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY
                            || attr.getType() == Types.LONGVARBINARY) {

                        if (attr.isPrimaryKey() || attr.isForeignKey()) {
                            skip = true;
                            break;
                        }
                    }
                }

                if (skip) {
                    continue;
                }
            }

            filtered.add(ent);
        }

        entities = filtered;
    }
}