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.music.service.ManagementService.java

@ManagedOperation
@Async//w ww  . j a  v a 2  s  .c  o m
@Transactional
public void fillElectronic() {
    List<Piece> pieces = dao.listOrdered(Piece.class, "id");
    for (Piece piece : pieces) {
        piece.getIntermediateDecisions()
                .setElectronic(piece.getParts().contains(PartType.PAD1.toString())
                        || Arrays.binarySearch(InstrumentGroups.ELECTRONIC_MAIN_PART_ONLY_INSTRUMENTS,
                                piece.getMainInstrument()) > -1);
        dao.persist(piece);
    }
}

From source file:org.apache.taverna.activities.wsdl.xmlsplitter.XMLSplitterConfigurationBeanBuilder.java

public static JsonNode buildBeanForInput(Element element) throws JDOMException, IOException {
    ObjectNode bean = JSON_NODE_FACTORY.objectNode();
    ArrayNode inputDefinitions = bean.arrayNode();
    bean.put("inputPorts", inputDefinitions);
    ArrayNode outputDefinitions = bean.arrayNode();
    bean.put("outputPorts", outputDefinitions);

    TypeDescriptor descriptor = XMLSplitterSerialisationHelper.extensionXMLToTypeDescriptor(element);
    ObjectNode outBean = outputDefinitions.addObject();
    outBean.put("name", "output");
    outBean.put("mimeType", "'text/xml'");
    outBean.put("depth", 0);
    outBean.put("granularDepth", 0);

    if (descriptor instanceof ComplexTypeDescriptor) {
        List<TypeDescriptor> elements = ((ComplexTypeDescriptor) descriptor).getElements();
        String[] names = new String[elements.size()];
        Class<?>[] types = new Class<?>[elements.size()];
        TypeDescriptor.retrieveSignature(elements, names, types);
        for (int i = 0; i < names.length; i++) {
            ObjectNode portBean = inputDefinitions.addObject();
            portBean.put("name", names[i]);
            portBean.put("mimeType", TypeDescriptor.translateJavaType(types[i]));
            portBean.put("depth", depthForDescriptor(elements.get(i)));
        }/*from   w  w  w.j a va  2 s . c  o  m*/

        List<TypeDescriptor> attributes = ((ComplexTypeDescriptor) descriptor).getAttributes();
        String[] elementNames = Arrays.copyOf(names, names.length);
        Arrays.sort(elementNames);
        String[] attributeNames = new String[attributes.size()];
        Class<?>[] attributeTypes = new Class<?>[attributes.size()];
        TypeDescriptor.retrieveSignature(attributes, attributeNames, attributeTypes);
        for (int i = 0; i < attributeNames.length; i++) {
            ObjectNode portBean = inputDefinitions.addObject();
            if (Arrays.binarySearch(elementNames, attributeNames[i]) < 0) {
                portBean.put("name", attributeNames[i]);
            } else {
                portBean.put("name", "1" + attributeNames[i]);
            }
            portBean.put("mimeType", TypeDescriptor.translateJavaType(attributeTypes[i]));
            portBean.put("depth", depthForDescriptor(attributes.get(i)));
        }
    } else if (descriptor instanceof ArrayTypeDescriptor) {
        ObjectNode portBean = inputDefinitions.addObject();
        portBean.put("name", descriptor.getName());

        if (((ArrayTypeDescriptor) descriptor).getElementType() instanceof BaseTypeDescriptor) {
            portBean.put("mimeType", "l('text/plain')");
        } else {
            portBean.put("mimeType", "l('text/xml')");
        }
        portBean.put("depth", 1);
    }

    String wrappedType = new XMLOutputter().outputString(element);
    bean.put("wrappedType", wrappedType);

    return bean;
}

From source file:com.slytechs.capture.file.indexer.SoftTable.java

public long search(long position) throws IOException {
    if (position < first || position > last) {
        return -1;
    }//from  ww w. ja va  2  s  . c om

    ;

    return Arrays.binarySearch(fetchIndexes(), position);
}

From source file:com.epam.ta.reportportal.auth.UserRoleHierarchy.java

private Set<GrantedAuthority> findReachableRoles(UserRole authority) {
    Set<GrantedAuthority> reachableRoles = new HashSet<>();
    UserRole[] roles = UserRole.values();
    int startIndex = Arrays.binarySearch(UserRole.values(), authority);
    for (int i = 0; i <= startIndex; i++) {
        reachableRoles.add(asAuthority(roles[i]));
    }//from w  ww .  j a  v  a  2s.  co  m
    return reachableRoles;
}

From source file:net.phoenix.nlp.pos.chmm.recognitor.DateTimeRecognitor.java

private Status parseStatus(POSTerm term) {
    if (term.getTermNatures().isNature(Nature.Number))
        return Status.Number;
    String name = term.getName();
    name = name.trim();//from  www .  j a  v a 2s .  c om
    if (name.length() == 0)
        return Status.Other;
    // ?
    if (name.length() == 1) {
        if (Arrays.binarySearch(this.qualifiers, name.charAt(0)) >= 0)
            return Status.Qualifier;
        else if (Arrays.binarySearch(this.numbers, name.charAt(0)) >= 0)
            return Status.Number;
        else
            return Status.Other;
    }

    // + //?
    boolean isNumber = true;
    int pos = 0;
    // 
    while (isNumber && pos < name.length() - 1) {
        isNumber = (Arrays.binarySearch(this.numbers, name.charAt(pos)) >= 0);
        pos++;
    }
    if (isNumber) {
        if (Arrays.binarySearch(this.qualifiers, name.charAt(pos)) >= 0)
            return Status.Date;
        else if (Arrays.binarySearch(this.numbers, name.charAt(pos)) >= 0)
            return Status.Number;
        else
            return Status.Other;
    }

    return Status.Other;
}

From source file:com.nttec.everychan.ui.presentation.Subscriptions.java

/**
 * , ?    ?   ? ?/*w  ww .j a v a2s  .c o  m*/
 * @param page ?
 * @param startPostIndex  ? ( ?)  ?, ? ?  ?? ?
 * @return ? (  ?)    ? ?, ?  ?,   ? -1 
 */
public int checkSubscriptions(SerializablePage page, int startPostIndex) {
    if (!MainApplication.getInstance().settings.isSubscriptionsEnabled())
        return -1;
    if (page.pageModel == null || page.pageModel.type != UrlPageModel.TYPE_THREADPAGE || page.posts == null)
        return -1;
    String[] subscriptions = getSubscriptions(page.pageModel.chanName, page.pageModel.boardName,
            page.pageModel.threadNumber);
    if (subscriptions == null)
        return -1;
    if (startPostIndex < page.posts.length && MainApplication.getInstance().settings.subscribeThreads()
            && Arrays.binarySearch(subscriptions, page.pageModel.threadNumber) >= 0)
        return startPostIndex;
    for (int i = startPostIndex; i < page.posts.length; ++i) {
        String comment = page.posts[i].comment;
        if (comment == null)
            continue;
        Matcher m = PresentationItemModel.REPLY_LINK_FULL_PATTERN.matcher(comment);
        while (m.find())
            if (Arrays.binarySearch(subscriptions, m.group(1)) >= 0)
                return i;
    }
    return -1;
}

From source file:eu.irreality.age.SwingAetheriaGameLoaderInterface.java

public static void loadFont() {

    //cargar configuracin del ini

    String fontName = "Courier New";
    int fontSize = 12;
    try {/*w w w.j  a  v a 2 s .  co m*/
        BufferedReader iniReader = new BufferedReader(
                Utility.getBestInputStreamReader(new FileInputStream("age.cfg")));
        String linea;
        for (int line = 1; line < 100; line++) {
            linea = iniReader.readLine();
            if (linea != null) {
                //System.out.println("Linea " + linea );
                String codigo = StringMethods.getTok(linea, 1, '=').trim().toLowerCase();
                if (codigo.equals("font name")) {
                    //System.out.println("Nombre: " + StringMethods.getTok(linea,2,'=').trim() );
                    fontName = StringMethods.getTok(linea, 2, '=').trim();
                } else if (codigo.equals("font size")) {
                    fontSize = Integer.parseInt(StringMethods.getTok(linea, 2, '=').trim());
                }
            }
        }
    }
    //las excepciones nos la sudan porque hay valores por defecto
    catch (FileNotFoundException fnfe) {
        ;
    } catch (NumberFormatException nfe) {
        ;
    } catch (IOException ioe) {
        ;
    } catch (SecurityException se) //applet mode
    {
        ;
    }
    Font[] fuentes = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    Font fuenteElegida;
    for (int f = 0; f < fuentes.length; f++) {
        if (fuentes[f].getFontName().equalsIgnoreCase(fontName)) {
            SwingAetheriaGameLoaderInterface.font = fuentes[f].deriveFont((float) fontSize);
            break;
        }
        //System.out.println("Fuente: " + fuentes[f]);
    }

    //System.err.println("He seleccionado mi fuente, y es: " + SwingAetheriaGameLoaderInterface.font  );

    //font not selected? be less picky
    if (SwingAetheriaGameLoaderInterface.font == null) {
        String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        Arrays.sort(fonts);
        if (Arrays.binarySearch(fonts, "Courier New") >= 0) {
            SwingAetheriaGameLoaderInterface.font = new Font("Courier New", Font.PLAIN, 12);
        } else if (Arrays.binarySearch(fonts, "Courier") >= 0) {
            SwingAetheriaGameLoaderInterface.font = new Font("Courier", Font.PLAIN, 12);
        } else if (Arrays.binarySearch(fonts, "Monospaced") >= 0) {
            SwingAetheriaGameLoaderInterface.font = new Font("Monospaced", Font.PLAIN, 13);
        }
    }

    //still not selected? well, in that case just default to font 0
    if (SwingAetheriaGameLoaderInterface.font == null)
        SwingAetheriaGameLoaderInterface.font = fuentes[0].deriveFont((float) fontSize);
}

From source file:org.atricore.idbus.idojos.dbsessionstore.test.JdbcSessionStoreTest.java

@Test
public void testKeys() throws Exception {
    String[] keys = db.keys();/*w w  w. ja  v a  2s.  c om*/
    assert Arrays.binarySearch(keys, "1") > -1 : "expected sessionId 1 not found";
    assert Arrays.binarySearch(keys, "2") > -1 : "expected sessionId 2 not found";
}

From source file:Primes.java

/**
 * Returns from a static prime table the least prime that is greater 
 * than or equal to a specified value./*from w ww .  j  a v a 2s.c  om*/
 */
public static int findLeastPrimeNumber(int n) {
    assert (n >= 0) : n;
    int idx = Arrays.binarySearch(PRIMES, n);
    if (idx < 0) {
        idx = -idx - 1;
        if (idx >= PRIMES.length) {
            idx = PRIMES.length - 1;
        }
        return PRIMES[idx]; // if not found, use max prime (in this case 9973).
    }
    return PRIMES[idx];
}

From source file:Util.java

public static final BigDecimal[] interpLinear(BigDecimal[] x, BigDecimal[] y, BigDecimal[] xi) {
    if (x.length != y.length) {
        throw new IllegalArgumentException("X and Y must be the same length");
    }//from   ww  w.j a va2s.  c o  m
    if (x.length == 1) {
        throw new IllegalArgumentException("X must contain more than one value");
    }
    BigDecimal[] dx = new BigDecimal[x.length - 1];
    BigDecimal[] dy = new BigDecimal[x.length - 1];
    BigDecimal[] slope = new BigDecimal[x.length - 1];
    BigDecimal[] intercept = new BigDecimal[x.length - 1];

    // Calculate the line equation (i.e. slope and intercept) between each point
    BigInteger zero = new BigInteger("0");
    BigDecimal minusOne = new BigDecimal(-1);

    for (int i = 0; i < x.length - 1; i++) {
        //dx[i] = x[i + 1] - x[i];
        dx[i] = x[i + 1].subtract(x[i]);
        if (dx[i].equals(new BigDecimal(zero, dx[i].scale()))) {
            throw new IllegalArgumentException("X must be montotonic. A duplicate " + "x-value was found");
        }
        if (dx[i].signum() < 0) {
            throw new IllegalArgumentException("X must be sorted");
        }
        //dy[i] = y[i + 1] - y[i];
        dy[i] = y[i + 1].subtract(y[i]);
        //slope[i] = dy[i] / dx[i];
        slope[i] = dy[i].divide(dx[i]);
        //intercept[i] = y[i] - x[i] * slope[i];
        intercept[i] = x[i].multiply(slope[i]).subtract(y[i]).multiply(minusOne);
        //intercept[i] = y[i].subtract(x[i]).multiply(slope[i]);
    }

    // Perform the interpolation here
    BigDecimal[] yi = new BigDecimal[xi.length];
    for (int i = 0; i < xi.length; i++) {
        //if ((xi[i] > x[x.length - 1]) || (xi[i] < x[0])) {
        if (xi[i].compareTo(x[x.length - 1]) > 0 || xi[i].compareTo(x[0]) < 0) {
            yi[i] = null; // same as NaN
        } else {
            int loc = Arrays.binarySearch(x, xi[i]);
            if (loc < -1) {
                loc = -loc - 2;
                //yi[i] = slope[loc] * xi[i] + intercept[loc];
                yi[i] = slope[loc].multiply(xi[i]).add(intercept[loc]);
            } else {
                yi[i] = y[loc];
            }
        }
    }

    return yi;
}