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.epam.reportportal.apache.http.conn.ssl.AbstractVerifier.java

boolean validCountryWildcard(final String cn) {
    final String parts[] = cn.split("\\.");
    if (parts.length != 3 || parts[2].length() != 2) {
        return true; // it's not an attempt to wildcard a 2TLD within a country code
    }/*from  w w  w  .jav a 2  s.c  o  m*/
    return Arrays.binarySearch(BAD_COUNTRY_2LDS, parts[1]) < 0;
}

From source file:org.jenkinsci.plugins.GithubSecurityRealm.java

/**
 * Checks the security realm for a GitHub OAuth scope.
 * @param scope A scope to check for in the security realm.
 * @return true if security realm has the scope or false if it does not.
 *//*from w  w w .  jav a2  s  .com*/
public boolean hasScope(String scope) {
    if (this.myScopes == null) {
        this.myScopes = this.oauthScopes.split(",");
        Arrays.sort(this.myScopes);
    }
    return Arrays.binarySearch(this.myScopes, scope) >= 0;
}

From source file:com.puppycrawl.tools.checkstyle.api.FileText.java

/**
 * Determine line and column numbers in full text.
 * @param pos the character position in the full text
 * @return the line and column numbers of this character
 *///  w  ww . j  a v a 2  s.  c  o  m
public LineColumn lineColumn(int pos) {
    final int[] lineBreakPositions = findLineBreaks();
    int lineNo = Arrays.binarySearch(lineBreakPositions, pos);
    if (lineNo < 0) {
        // we have: lineNo = -(insertion point) - 1
        // we want: lineNo =  (insertion point) - 1
        lineNo = -lineNo - 2;
    }
    final int startOfLine = lineBreakPositions[lineNo];
    final int columnNo = pos - startOfLine;
    // now we have lineNo and columnNo, both starting at zero.
    return new LineColumn(lineNo + 1, columnNo);
}

From source file:it.infn.ct.browseDigitalRepos.Browse.java

@Override
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
    try {/*from   www.  j  a va 2s .c  o  m*/
        String action = "";

        // Getting the action to be processed from the request
        action = request.getParameter("ActionEvent");

        Company company = PortalUtil.getCompany(request);
        String portal = company.getName();

        PortletPreferences portletPreferences = (PortletPreferences) request.getPreferences();

        if (action.equals("CONFIG_BROWSE_PORTLET")) {
            log.info("\nPROCESS ACTION => " + action);

            String browse_METADATA_HOST = request.getParameter("browse_METADATA_HOST");
            String repository = request.getParameter("repository");
            String[] infras = new String[2];

            String eumed_browse_ENABLEINFRASTRUCTURE = "unchecked";
            String[] browse_INFRASTRUCTURES = request.getParameterValues("browse_ENABLEINFRASTRUCTURES");

            if (browse_INFRASTRUCTURES != null) {
                Arrays.sort(browse_INFRASTRUCTURES);
                eumed_browse_ENABLEINFRASTRUCTURE = Arrays.binarySearch(browse_INFRASTRUCTURES, "eumed") >= 0
                        ? "checked"
                        : "unchecked";
            }

            if (eumed_browse_ENABLEINFRASTRUCTURE.equals("checked")) {
                infras[0] = "eumed";
                String eumed_browse_INFRASTRUCTURE = request.getParameter("eumed_browse_INFRASTRUCTURE");
                String eumed_browse_VONAME = request.getParameter("eumed_browse_VONAME");
                String eumed_browse_LAT = request.getParameter("eumed_browse_LAT");
                String eumed_browse_ETOKENSERVER = request.getParameter("eumed_browse_ETOKENSERVER");
                String eumed_browse_LONG = request.getParameter("eumed_browse_LONG");
                String eumed_browse_PORT = request.getParameter("eumed_browse_PORT");
                String eumed_browse_ROBOTID = request.getParameter("eumed_browse_ROBOTID");
                String eumed_browse_ROLE = request.getParameter("eumed_browse_ROLE");

                // Set the portlet preferences
                portletPreferences.setValue("eumed_browse_INFRASTRUCTURE", eumed_browse_INFRASTRUCTURE.trim());
                portletPreferences.setValue("eumed_browse_VONAME", eumed_browse_VONAME.trim());
                portletPreferences.setValue("eumed_browse_LAT", eumed_browse_LAT.trim());
                portletPreferences.setValue("eumed_browse_ETOKENSERVER", eumed_browse_ETOKENSERVER.trim());
                portletPreferences.setValue("eumed_browse_LONG", eumed_browse_LONG.trim());
                portletPreferences.setValue("eumed_browse_PORT", eumed_browse_PORT.trim());
                portletPreferences.setValue("eumed_browse_ROBOTID", eumed_browse_ROBOTID.trim());
                portletPreferences.setValue("eumed_browse_ROLE", eumed_browse_ROLE.trim());

                portletPreferences.setValue("browse_METADATA_HOST", browse_METADATA_HOST.trim());
                portletPreferences.setValue("repository", repository.trim());

                log.info("\n\nPROCESS ACTION => " + action + "\n- Storing the BROWSE portlet preferences ..."
                        + "\n\neumed_browse_INFRASTRUCTURE: " + eumed_browse_INFRASTRUCTURE
                        + "\neumed_browse_VONAME: " + eumed_browse_VONAME + "\neumed_browse_LAT: "
                        + eumed_browse_LAT + "\neumed_browse_LONG: " + eumed_browse_LONG
                        + "\neumed_browse_ETOKENSERVER: " + eumed_browse_ETOKENSERVER + "\neumed_browse_PORT: "
                        + eumed_browse_PORT + "\neumed_browse_ROBOTID: " + eumed_browse_ROBOTID
                        + "\neumed_browse_ROLE: " + eumed_browse_ROLE

                        + "\n\nbrowse_ENABLEINFRASTRUCTURE: " + "eumed" + "\nbrowse_METADATA_HOST: "
                        + browse_METADATA_HOST + "\nRepository: " + repository);
            }

            for (int i = 0; i < infras.length; i++)
                log.info("\n - Infrastructure Enabled = " + infras[i]);
            portletPreferences.setValues("browse_ENABLEINFRASTRUCTURE", infras);
            portletPreferences.setValue("eumed_browse_ENABLEINFRASTRUCTURE", infras[0]);

            portletPreferences.store();
            response.setPortletMode(PortletMode.VIEW);
        } // end PROCESS ACTION [ CONFIG_BROWSE_PORTLET ]

    } catch (PortalException ex) {
        Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SystemException ex) {
        Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.tino1b2be.dtmfdecoder.DTMFUtil.java

/**
 * Check if the characters are valid and set the characters
 * /* w ww .j a  v  a  2 s  .c  om*/
 * @param chars
 *            Characters to be generated
 * @throws DTMFDecoderException
 */
private void setChars(char[] chars) throws DTMFDecoderException {
    outChars = new char[chars.length];
    char[] cc = Arrays.copyOf(DTMF_CHARACTERS, DTMF_CHARACTERS.length);
    Arrays.sort(cc);
    for (int c = 0; c < chars.length; c++) {
        if (Arrays.binarySearch(cc, chars[c]) < 0)
            throw new DTMFDecoderException("The character \"" + chars[c] + "\" is not a DTMF character.");
        else
            outChars[c] = chars[c];
    }
}

From source file:com.linkedin.pinot.core.segment.index.readers.ImmutableDictionaryReaderTest.java

@Test
public void testDoubleDictionary() throws Exception {
    try (DoubleDictionary doubleDictionary = new DoubleDictionary(
            PinotDataBuffer.fromFile(new File(TEMP_DIR, DOUBLE_COLUMN_NAME + V1Constants.Dict.FILE_EXTENSION),
                    ReadMode.mmap, FileChannel.MapMode.READ_ONLY, DOUBLE_COLUMN_NAME),
            NUM_VALUES)) {//from  w w  w  .ja  va2  s .  co  m
        for (int i = 0; i < NUM_VALUES; i++) {
            Assert.assertEquals(doubleDictionary.get(i), _doubleValues[i], 0.0);
            Assert.assertEquals(doubleDictionary.getIntValue(i), (int) _doubleValues[i]);
            Assert.assertEquals(doubleDictionary.getLongValue(i), (long) _doubleValues[i]);
            Assert.assertEquals(doubleDictionary.getFloatValue(i), (float) _doubleValues[i], 0.0f);
            Assert.assertEquals(doubleDictionary.getDoubleValue(i), _doubleValues[i], 0.0);
            Assert.assertEquals(Double.parseDouble(doubleDictionary.getStringValue(i)), _doubleValues[i], 0.0);

            Assert.assertEquals(doubleDictionary.indexOf(_doubleValues[i]), i);

            double randomDouble = RANDOM.nextDouble();
            Assert.assertEquals(doubleDictionary.insertionIndexOf(randomDouble),
                    Arrays.binarySearch(_doubleValues, randomDouble));
        }
    }
}

From source file:com.opengamma.analytics.financial.credit.creditdefaultswap.pricing.vanilla.isdanew.ISDACompliantCurve.java

/**
 * get the sensitivity of the interpolated rate at time t to the curve node. Note, since the interpolator is highly local, most
 * of the returned values will be zero, so it maybe more efficient to call getSingleNodeSensitivity
 * @param t The time//from ww w . j av  a 2s . c om
 * @return sensitivity to the nodes
 */
public double[] getNodeSensitivity(final double t) {

    final double[] res = new double[_n];

    // short-cut doing binary search
    if (t <= _t[0]) {
        res[0] = 1.0;
        return res;
    }
    if (t >= _t[_n - 1]) {
        final int insertionPoint = _n - 1;
        final double t1 = _t[insertionPoint - 1];
        final double t2 = _t[insertionPoint];
        final double dt = t2 - t1;
        res[insertionPoint - 1] = t1 * (t2 - t) / dt / t;
        res[insertionPoint] = t2 * (t - t1) / dt / t;
        return res;
    }

    final int index = Arrays.binarySearch(_t, t);
    if (index >= 0) {
        res[index] = 1.0;
        return res;
    }

    final int insertionPoint = -(1 + index);
    final double t1 = _t[insertionPoint - 1];
    final double t2 = _t[insertionPoint];
    final double dt = t2 - t1;
    res[insertionPoint - 1] = t1 * (t2 - t) / dt / t;
    res[insertionPoint] = t2 * (t - t1) / dt / t;
    return res;
}

From source file:com.asakusafw.runtime.stage.input.StageInputDriver.java

private static void writeEncoded(DataOutput output, String[] dictionary, String value) throws IOException {
    assert output != null;
    assert dictionary != null;
    assert value != null;
    int index = Arrays.binarySearch(dictionary, value);
    if (index < 0) {
        throw new IllegalStateException(MessageFormat.format("Value is not in dictionary: value={0}, dict={1}",
                value, Arrays.toString(dictionary)));
    }//from w ww  . j a  va  2  s.  co m
    WritableUtils.writeVInt(output, index);
}

From source file:jsat.distributions.empirical.MyKernelDensityEstimator.java

@Override
public double invCdf(double p) {
    int index;/*from   w w  w. j a  v  a  2 s .com*/
    double kd0;

    if (weights.length == 0) {
        final double r = p * X.length;
        index = (int) r;
        final double pd0 = r - index, pd1 = 1 - pd0;
        kd0 = k.intK(pd1);
    } else// CDF can be found from the weights summings
    {
        final double XEstimate = p * sumOFWeights;
        index = Arrays.binarySearch(weights, XEstimate);
        index = index < 0 ? -index - 1 : index;
        if (X[index] != 0) {
            kd0 = 1.0;// -Math.abs((XEstimate-X[index])/X[index]);
        } else {
            kd0 = 1.0;
        }
    }

    if (index == X.length - 1) {
        return X[index] * kd0;
    }

    final double x = X[index] * kd0 + X[index + 1] * (1 - kd0);

    return x;
}

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

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