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:org.tonguetied.datatransfer.ExportServiceTest.java

public final void testCreateArchiveWithFiles() throws Exception {
    archiveTestDirectory = new File(USER_DIR, "testCreateArchiveWithFiles");
    FileUtils.forceMkdir(archiveTestDirectory);
    FileUtils.writeStringToFile(new File(archiveTestDirectory, "temp"), "test.value=value");
    FileUtils.writeStringToFile(new File(archiveTestDirectory, "temp_en"), "test.value=valyoo");
    assertEquals(2, archiveTestDirectory.listFiles().length);
    assertTrue(archiveTestDirectory.isDirectory());
    dataService.createArchive(archiveTestDirectory);
    assertEquals(3, archiveTestDirectory.listFiles().length);
    // examine zip file
    File[] files = archiveTestDirectory.listFiles(new FileExtensionFilter(".zip"));
    assertEquals(1, files.length);// www  .  j a  v a  2 s . c om
    ZipInputStream zis = null;
    final String[] fileNames = { "temp", "temp_en" };
    try {
        zis = new ZipInputStream(new FileInputStream(files[0]));
        ZipEntry zipEntry = zis.getNextEntry();
        Arrays.binarySearch(fileNames, zipEntry.getName());
        zis.closeEntry();
        zipEntry = zis.getNextEntry();
        Arrays.binarySearch(fileNames, zipEntry.getName());
        zis.closeEntry();

        // confirm that there are no more files in the archive
        assertEquals(0, zis.available());
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:com.panet.imeta.core.plugins.PluginLoader.java

private String[] getLibs(FileObject pluginLocation) throws IOException {
    File[] jars = new File(pluginLocation.getURL().getFile()).listFiles(new JarNameFilter());

    String[] libs = new String[jars.length];
    for (int i = 0; i < jars.length; i++)
        libs[i] = jars[i].getPath();//from  w w w .  j a  v a  2s.  c  o  m

    Arrays.sort(libs);
    int idx = Arrays.binarySearch(libs, DEFAULT_LIB);

    String[] retVal = null;

    if (idx < 0) // does not contain
    {
        String[] completeLib = new String[libs.length + 1];
        System.arraycopy(libs, 0, completeLib, 0, libs.length);
        completeLib[libs.length] = pluginLocation.resolveFile(DEFAULT_LIB).getURL().getFile();
        retVal = completeLib;
    } else
        retVal = libs;

    return retVal;
}

From source file:org.commonvox.hbase_column_manager.ChangeEventMonitor.java

/**
 * Get the {@link ChangeEvent}s pertaining to a specified Entity (e.g., a specific <i>Table</i>),
 * in timestamp order. Note that some parameters will not apply to some {@link SchemaEntityType}s
 * (e.g. columnFamily does not apply when entityType is {@code TABLE}), in which case
 * {@code null}s may be submitted for inapplicable parameters.
 *
 * @param entityType type of Entity for which {@code ChangeEvent} list is to be produced
 * @param namespaceName Name of <i>Namespace</i> which pertains to the Entity
 * @param tableName Name of <i>Table</i> which pertains to the Entity (if applicable)
 * @param columnFamily Name of <i>Column Family</i> which pertains to the Entity (if applicable)
 * @param columnQualifier <i>Column Qualifier</i> which pertains to the Entity (if applicable)
 * @param includeChildEntities//from  ww  w . j av  a 2 s .  com
 * @return Set of ChangeEvents pertaining to the specified Entity, in timestamp order
 */
private Set<ChangeEvent> getChangeEventsForEntity(SchemaEntityType entityType, byte[] namespaceName,
        byte[] tableName, byte[] columnFamily, byte[] columnQualifier, boolean includeChildEntities) {

    ChangeEvent.Entity[] entityArray = entitySet.toArray(new ChangeEvent.Entity[entitySet.size()]);

    ChangeEvent.Entity namespaceEntity = new ChangeEvent.Entity(SchemaEntityType.NAMESPACE.getRecordType(),
            Repository.NAMESPACE_PARENT_FOREIGN_KEY, namespaceName);
    int namespaceIndex = Arrays.binarySearch(entityArray, namespaceEntity);
    if (namespaceIndex < 0) {
        return null;
    }
    if (entityType.equals(SchemaEntityType.NAMESPACE)) {
        Set<ChangeEvent> returnedChangeEvents = getChangeEventsForEntity(namespaceEntity);
        if (includeChildEntities) {
            for (ChangeEvent.Entity entity : entityArray) {
                if (entity.getEntityRecordType().getByte() == SchemaEntityType.TABLE.getRecordType()
                        && Bytes.equals(entity.getNamespace(), namespaceName)) {
                    returnedChangeEvents.addAll(getChangeEventsForEntity(SchemaEntityType.TABLE, namespaceName,
                            entity.getEntityName().getBytes(), null, null, includeChildEntities));
                }
            }
        }
        return returnedChangeEvents;
    }

    byte[] namespaceForeignKey = entityArray[namespaceIndex].getEntityForeignKey().getBytes();
    ChangeEvent.Entity tableEntity = new ChangeEvent.Entity(SchemaEntityType.TABLE.getRecordType(),
            namespaceForeignKey, tableName);
    int tableIndex = Arrays.binarySearch(entityArray, tableEntity);
    if (tableIndex < 0) {
        return null;
    }
    if (entityType.equals(SchemaEntityType.TABLE)) {
        Set<ChangeEvent> returnedChangeEvents = getChangeEventsForEntity(tableEntity);
        if (includeChildEntities) {
            for (ChangeEvent.Entity entity : entityArray) {
                if (entity.getEntityRecordType().getByte() == SchemaEntityType.COLUMN_FAMILY.getRecordType()
                        && Bytes.equals(entity.getNamespace(), namespaceName)
                        && Bytes.equals(entity.getTableName(), tableName)) {
                    returnedChangeEvents
                            .addAll(getChangeEventsForEntity(SchemaEntityType.COLUMN_FAMILY, namespaceName,
                                    tableName, entity.getEntityName().getBytes(), null, includeChildEntities));
                }
            }
        }
        return returnedChangeEvents;
    }

    byte[] tableForeignKey = entityArray[tableIndex].getEntityForeignKey().getBytes();
    ChangeEvent.Entity colFamilyEntity = new ChangeEvent.Entity(SchemaEntityType.COLUMN_FAMILY.getRecordType(),
            tableForeignKey, columnFamily);
    int colFamilyIndex = Arrays.binarySearch(entityArray, colFamilyEntity);
    if (colFamilyIndex < 0) {
        return null;
    }
    if (entityType.equals(SchemaEntityType.COLUMN_FAMILY)) {
        Set<ChangeEvent> returnedChangeEvents = getChangeEventsForEntity(colFamilyEntity);
        if (includeChildEntities) {
            for (ChangeEvent.Entity entity : entityArray) {
                if (entity.getEntityRecordType().getByte() == SchemaEntityType.COLUMN_AUDITOR.getRecordType()
                        && Bytes.equals(entity.getNamespace(), namespaceName)
                        && Bytes.equals(entity.getTableName(), tableName)
                        && Bytes.equals(entity.getColumnFamily(), columnFamily)) {
                    returnedChangeEvents
                            .addAll(getChangeEventsForEntity(SchemaEntityType.COLUMN_AUDITOR, namespaceName,
                                    tableName, entity.getEntityName().getBytes(), null, includeChildEntities));
                }
                if (entity.getEntityRecordType().getByte() == SchemaEntityType.COLUMN_DEFINITION.getRecordType()
                        && Bytes.equals(entity.getNamespace(), namespaceName)
                        && Bytes.equals(entity.getTableName(), tableName)
                        && Bytes.equals(entity.getColumnFamily(), columnFamily)) {
                    returnedChangeEvents
                            .addAll(getChangeEventsForEntity(SchemaEntityType.COLUMN_DEFINITION, namespaceName,
                                    tableName, entity.getEntityName().getBytes(), null, includeChildEntities));
                }
            }
        }
        return returnedChangeEvents;
    }

    byte[] colFamilyForeignKey = entityArray[colFamilyIndex].getEntityForeignKey().getBytes();
    ChangeEvent.Entity colQualifierEntity = new ChangeEvent.Entity(entityType.getRecordType(),
            colFamilyForeignKey, columnQualifier);
    int colQualifierIndex = Arrays.binarySearch(entityArray, colQualifierEntity);
    if (colQualifierIndex < 0) {
        return null;
    }
    if (entityType.equals(SchemaEntityType.COLUMN_AUDITOR)
            || entityType.equals(SchemaEntityType.COLUMN_DEFINITION)) {
        return getChangeEventsForEntity(colQualifierEntity);
    }
    return null;
}

From source file:net.sf.firemox.clickable.target.card.MCard.java

/**
 * indicates if this card has this idType.
 * //from  w ww . java  2  s . co m
 * @param idType
 *          is the type required
 * @param creator
 *          the card that has created the modifier to ignore.
 * @return true if this card has the required type
 */
public boolean hasPropertyNotFromCreator(int idType, MCard creator) {
    boolean found;
    int[] properties = getCardModel().getProperties();
    found = properties != null && Arrays.binarySearch(properties, idType) >= 0;
    if (creator == this) {
        found = false;
    }
    if (propertyModifier != null) {
        return propertyModifier.hasPropertyNotFromCreator(idType, found, creator);
    }
    return found;
}

From source file:fastcall.FastCallSNP.java

private void fillDepthAndBase(ConcurrentHashMap<String, List<List<String>>> bamPileupResultMap,
        StringBuilder[][] baseSb, int[][] depth, int startPos) {
    Set<Map.Entry<String, String[]>> entries = this.taxaBamPathMap.entrySet();
    entries.parallelStream().forEach(e -> {
        String taxa = e.getKey();
        int taxaIndex = Arrays.binarySearch(this.taxaNames, taxa);
        String[] bams = e.getValue();

        int count = 0;
        String b = null;/*from w ww. j  a  v a  2s  . c  om*/
        try {
            for (int i = 0; i < bams.length; i++) {
                List<List<String>> lines = bamPileupResultMap.get(bams[i]);
                count = lines.size();
                b = bams[i];
                for (int j = 0; j < lines.size(); j++) {
                    List<String> split = lines.get(j);
                    if (split.get(2).startsWith("N") || split.get(2).startsWith("n"))
                        continue;
                    int siteIndex = Integer.valueOf(split.get(1)) - startPos;
                    depth[siteIndex][taxaIndex] += Integer.valueOf(split.get(3));
                    baseSb[siteIndex][taxaIndex].append(split.get(4));
                }
            }
        } catch (Exception ee) {
            System.out.println(b);
            System.out.println(count);
            ee.printStackTrace();
            System.exit(1);
        }
    });
}

From source file:com.jaspersoft.jasperserver.remote.services.impl.BatchRepositoryServicePeganatedSearchTest.java

private List<ResourceLookup> createPage(final int givenOffset, int... removeIndexes) {
    int maxPage = givenOffset + ITEMS_PER_PAGE;
    maxPage = maxPage > this.lookups.size() ? this.lookups.size() : maxPage;

    List<ResourceLookup> page = new ArrayList<ResourceLookup>();
    List<ResourceLookup> subList = this.lookups.subList(givenOffset, maxPage);

    for (int i = 0; i < subList.size(); i++) {
        if (Arrays.binarySearch(removeIndexes, i) < 0) {
            page.add(subList.get(i));//www .  jav  a 2  s .  co m
        }
    }

    return page;
}

From source file:org.commonjava.maven.atlas.spi.neo4j.io.Conversions.java

public static boolean idListingContains(final String property, final Relationship relationship,
        final long... targets) {
    if (!relationship.hasProperty(property)) {
        return false;
    }/*from w ww  .jav  a  2s.c  o  m*/

    final long[] ids = (long[]) relationship.getProperty(property, new long[] {});
    //        LOGGER.info( "Relationship: %s\nValue of property: %s is: %s", relationship, property,
    //                     relationship.getProperty( property ) );

    Arrays.sort(ids);
    for (final long target : targets) {
        if (Arrays.binarySearch(ids, target) > -1) {
            return true;
        }
    }

    return false;
}

From source file:com.bdaum.zoom.gps.internal.operations.GeotagOperation.java

private boolean tag(AssetImpl asset, Meta meta, int i) {
    if (!gpsConfiguration.overwrite
            && (!Double.isNaN(asset.getGPSLongitude()) || !Double.isNaN(asset.getGPSLatitude()))) {
        monitor.worked(1);/*  w  w  w .ja  v  a 2  s . c  om*/
        return false;
    }
    createBackup(asset, i, false);
    long refTime = asset.getDateTimeOriginal().getTime() + gpsConfiguration.timeshift * ONEMINUTE;
    refDate.setTime(refTime);
    int index = Arrays.binarySearch(trackpoints, refDate);
    Trackpoint lower = null;
    Trackpoint upper = null;
    long lowerdiff = 0;
    long upperdiff = 0;
    if (index >= 0)
        upper = lower = trackpoints[index];
    else {
        index = -index - 1;
        lower = trackpoints[index <= 0 ? 0 : index - 1];
        upper = trackpoints[index > trackpoints.length - 1 ? trackpoints.length - 1 : index];
        lowerdiff = lower.getTime() < 0 ? 0 : Math.abs(lower.getTime() - refTime);
        upperdiff = upper.getTime() < 0 ? 0 : Math.abs(upper.getTime() - refTime);
        if (lower.getMinTime() >= 0 && upper.getMinTime() >= 0) {
            if (refTime > upper.getMaxTime() || refTime < lower.getMinTime()) {
                monitor.worked(1);
                return false;
            }
            if (refTime > upper.getMaxTime())
                upper = lower;
            else if (refTime < lower.getMinTime())
                lower = upper;
        } else {
            long tolerance = gpsConfiguration.tolerance * ONEMINUTE;
            if (upperdiff > tolerance) {
                if (lowerdiff > tolerance) {
                    monitor.worked(1);
                    return false;
                }
                upper = lower;
            } else if (lowerdiff > tolerance)
                lower = upper;
        }
    }
    double longitude = Double.NaN;
    double latitude = Double.NaN;
    double altitude = Double.NaN;
    double speed = Double.NaN;

    if (lower == upper) {
        longitude = lower.getLongitude();
        latitude = lower.getLatitude();
        altitude = lower.getAltitude();
        speed = lower.getSpeed();
    } else {
        long diff = lowerdiff + upperdiff;
        if (diff == 0) {
            monitor.worked(1);
            return false;
        }
        double fac1 = (double) upperdiff / diff;
        double fac2 = (double) lowerdiff / diff;
        if (!Double.isNaN(lower.getLongitude()) && !Double.isNaN(upper.getLongitude())) {
            double lowerLongitude = lower.getLongitude();
            double upperLongitude = upper.getLongitude();
            if (Math.abs(lowerLongitude - upperLongitude) > 180) {
                if (lowerLongitude > upperLongitude)
                    lowerLongitude -= 360;
                else
                    upperLongitude -= 360;
            }
            longitude = lowerLongitude * fac1 + upperLongitude * fac2;
            if (longitude < 0)
                longitude += 360;
        }
        if (!Double.isNaN(lower.getLatitude()) && !Double.isNaN(upper.getLatitude()))
            latitude = lower.getLatitude() * fac1 + upper.getLatitude() * fac2;
        if (!Double.isNaN(lower.getAltitude()) && !Double.isNaN(upper.getAltitude()))
            altitude = lower.getAltitude() * fac1 + upper.getAltitude() * fac2;
        if (!Double.isNaN(lower.getSpeed()) && !Double.isNaN(upper.getSpeed()))
            speed = lower.getSpeed() * fac1 + upper.getSpeed() * fac2;
    }
    if (noGoAreas != null)
        for (GeoArea area : noGoAreas)
            if (Core.distance(latitude, longitude, area.getLatitude(), area.getLongitude(), 'k') <= area
                    .getKm())
                return !updateMonitor(1);
    if (!Double.isNaN(longitude))
        asset.setGPSLongitude(longitude);
    if (!Double.isNaN(latitude))
        asset.setGPSLatitude(latitude);
    if (!Double.isNaN(altitude))
        asset.setGPSAltitude(altitude);
    if (!Double.isNaN(speed))
        asset.setGPSSpeed(speed);
    if (gpsConfiguration.updateAltitude && !Double.isNaN(asset.getGPSLatitude())
            && !Double.isNaN(asset.getGPSLongitude())) {
        double elevation = getElevation(asset.getGPSLatitude(), asset.getGPSLongitude(), monitor);
        if (!Double.isNaN(elevation))
            asset.setGPSAltitude(elevation);
    }
    if (gpsConfiguration.includeCoordinates)
        return addCoordinateKeywords(asset, asset.getGPSLatitude(), asset.getGPSLongitude(), meta);
    return storeSafely(null, 1, asset);
}

From source file:aldenjava.opticalmapping.data.data.DataNode.java

public void insertSignal(long pos) {
    int index = Arrays.binarySearch(refp, pos);
    if (index < 0) {
        long[] newrefp = new long[refp.length + 1];
        int insertionpoint = (index + 1) * -1;
        System.arraycopy(refp, 0, newrefp, 0, insertionpoint);
        newrefp[insertionpoint] = pos;//from   w w w .j a  v a 2 s  .c o m
        System.arraycopy(refp, insertionpoint, newrefp, insertionpoint + 1, refp.length - insertionpoint);
        refp = newrefp;
        // this.rebuildRefl();
    } else
        System.err.println("Warning: Signal already exists at " + Long.toString(pos));
}

From source file:org.commonjava.maven.atlas.spi.neo4j.io.Conversions.java

public static boolean idListingContains(final String property, final Relationship relationship,
        final Collection<Node> targets) {
    if (!relationship.hasProperty(property)) {
        return false;
    }//from  w  w  w .j  a  va  2  s . c o m

    final long[] ids = (long[]) relationship.getProperty(property, new long[] {});
    //        LOGGER.info( "Relationship: %s\nValue of property: %s is: %s", relationship, property,
    //                     relationship.getProperty( property ) );

    Arrays.sort(ids);
    for (final Node target : targets) {
        if (Arrays.binarySearch(ids, target.getId()) > -1) {
            return true;
        }
    }

    return false;
}