Example usage for java.util SortedMap isEmpty

List of usage examples for java.util SortedMap isEmpty

Introduction

In this page you can find the example usage for java.util SortedMap isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:org.jahia.admin.sites.ManageSites.java

private Locale determineDefaultLocale(ProcessingContext jParams, Map<Object, Object> infos) {
    Locale defaultLocale = jParams.getLocale();
    SortedMap<Integer, String> activeLanguageCodesByRank = new TreeMap<Integer, String>();
    for (Map.Entry<Object, Object> info : infos.entrySet()) {
        if (info.getKey() instanceof String) {
            Matcher m = LANGUAGE_RANK_PATTERN.matcher((String) info.getKey());
            if (m.find()) {
                String languageCode = m.group(1);
                boolean activated = Boolean
                        .parseBoolean((String) infos.get("language." + languageCode + ".activated"));

                if (activated) {
                    if ("1".equals(info.getValue())) {
                        return LanguageCodeConverters.languageCodeToLocale(languageCode);
                    } else {
                        activeLanguageCodesByRank.put(new Integer((String) info.getValue()), languageCode);
                    }/*from  w w w . j av  a 2s  .  c o m*/
                }
            }
        }
    }
    if (!activeLanguageCodesByRank.isEmpty()) {
        defaultLocale = LanguageCodeConverters
                .languageCodeToLocale(activeLanguageCodesByRank.get(activeLanguageCodesByRank.firstKey()));
    }
    return defaultLocale;
}

From source file:edu.vt.middleware.ldap.search.SearchExecutor.java

/**
 * This performs a Ldap search with the supplied <code>Query</code>.
 *
 * @param  ldapPool  <code>LdapPool</code> to use for searching
 * @param  query  <code>Query</code> to search for
 *
 * @return  <code>Iterator</code> - of search results
 *
 * @throws  PeopleSearchException  if an error occurs searching the Ldap
 *///from   ww w.ja  v a 2s  .  c o  m
public Iterator<SearchResult> executeSearch(final LdapPool<Ldap> ldapPool, final Query query)
        throws PeopleSearchException {
    final SortedMap<Integer, SearchResult> m = new TreeMap<Integer, SearchResult>();
    final List<String> l = new ArrayList<String>();

    if (this.queryTemplates != null && this.queryTemplates.size() > 0) {
        final SearchThread[] threads = new SearchThread[this.queryTemplates.size()];
        for (int i = 1; i <= this.queryTemplates.size(); i++) {
            final String ldapQuery = this.buildLdapQuery(this.getQueryTemplate(query.getQueryParameters(), i),
                    query.getSearchRestrictions());
            if (ldapQuery != null) {
                threads[i - 1] = new SearchThread(ldapPool, ldapQuery, query.getQueryAttributes());
            } else {
                threads[i - 1] = null;
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Performing search with " + threads.length + " threads");
        }

        int count = 0;
        boolean loop = true;
        while (loop && count < threads.length) {
            List<QueryResult> results = null;
            if (additive) {
                results = this.doAdditiveSearch(threads);
            } else {
                results = this.doIterativeSearch(threads[count]);
            }

            if (LOG.isDebugEnabled()) {
                LOG.debug("Loop " + count + " found " + results.size() + " results");
            }
            for (int i = 0; i < results.size(); i++) {
                int j = m.size();
                final QueryResult qr = results.get(i);
                if (qr != null) {
                    final SearchResult sr = qr.getSearchResult();
                    if (sr != null) {
                        if (!l.contains(sr.getName())) {
                            if (additive) {
                                qr.setSearchIteration(i);
                                qr.setTermCount(this.termCount);
                                this.processResults(qr);
                            } else {
                                qr.setSearchIteration(count);
                                qr.setTermCount(this.termCount);
                                this.processResults(qr);
                            }
                            // store for results
                            m.put(new Integer(j++), sr);
                            l.add(sr.getName());
                            if (LOG.isDebugEnabled()) {
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Query " + (additive ? i : count) + " found: " + sr.getName());
                                }
                            }
                        }
                    }
                }
            }
            if (additive || !m.isEmpty()) {
                loop = false;
            } else {
                count++;
            }
        }
    } else {
        if (LOG.isWarnEnabled()) {
            LOG.warn("No searches configured");
        }
    }

    Iterator<SearchResult> i = null;
    if (query.getFromResult() != null) {
        if (query.getToResult() != null) {
            if (query.getFromResult().intValue() <= query.getToResult().intValue()) {
                i = m.subMap(query.getFromResult(), query.getToResult()).values().iterator();
            }
        } else {
            i = m.tailMap(query.getFromResult()).values().iterator();
        }
    } else if (query.getToResult() != null) {
        i = m.headMap(query.getToResult()).values().iterator();
    } else {
        i = m.values().iterator();
    }
    return i;
}

From source file:org.apache.hadoop.hbase.master.RegionManager.java

/**
 * Applies actions specific to a column family within a region.
 *//*from w ww. j  av  a 2  s.c om*/
private void applyCFActions(final HServerInfo serverInfo, final ArrayList<HMsg> returnMsgs,
        final SortedMap<byte[], SortedMap<byte[], Pair<HRegionInfo, HServerAddress>>> map,
        final HMsg.Type msg) {
    HServerAddress addr = serverInfo.getServerAddress();
    synchronized (map) {
        Iterator<SortedMap<byte[], Pair<HRegionInfo, HServerAddress>>> it1 = map.values().iterator();
        while (it1.hasNext()) {
            SortedMap<byte[], Pair<HRegionInfo, HServerAddress>> cfMap = it1.next();
            Iterator<Map.Entry<byte[], Pair<HRegionInfo, HServerAddress>>> it2 = cfMap.entrySet().iterator();
            while (it2.hasNext()) {
                Map.Entry mapPairs = it2.next();
                Pair<HRegionInfo, HServerAddress> pair = (Pair<HRegionInfo, HServerAddress>) mapPairs
                        .getValue();
                if (addr.equals(pair.getSecond())) {
                    byte[] columnFamily = (byte[]) mapPairs.getKey();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Sending " + msg + " " + pair.getFirst() + " to " + addr
                                + " for column family : " + new String(columnFamily));
                    }
                    returnMsgs.add(new HMsg(msg, pair.getFirst(), columnFamily));
                    it2.remove();
                }
            }
            if (cfMap.isEmpty()) {
                // If entire map is empty, remove it from the parent map.
                it1.remove();
            }
        }
    }
}

From source file:org.springframework.integration.zip.transformer.UnZipTransformer.java

@Override
protected Object doZipTransform(final Message<?> message) throws Exception {

    try {// ww  w  .  j  av a2  s .com
        final Object payload = message.getPayload();
        final Object unzippedData;

        InputStream inputStream = null;

        try {
            if (payload instanceof File) {
                final File filePayload = (File) payload;

                if (filePayload.isDirectory()) {
                    throw new UnsupportedOperationException(
                            String.format("Cannot unzip a directory: '%s'", filePayload.getAbsolutePath()));
                }

                if (!SpringZipUtils.isValid(filePayload)) {
                    throw new IllegalStateException(
                            String.format("Not a zip file: '%s'.", filePayload.getAbsolutePath()));
                }

                inputStream = new FileInputStream(filePayload);
            } else if (payload instanceof InputStream) {
                inputStream = (InputStream) payload;
            } else if (payload instanceof byte[]) {
                inputStream = new ByteArrayInputStream((byte[]) payload);
            } else {
                throw new IllegalArgumentException(String.format("Unsupported payload type '%s'. "
                        + "The only supported payload types are java.io.File, byte[] and java.io.InputStream",
                        payload.getClass().getSimpleName()));
            }

            final SortedMap<String, Object> uncompressedData = new TreeMap<String, Object>();

            ZipUtil.iterate(inputStream, new ZipEntryCallback() {

                @Override
                public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws IOException {

                    final String zipEntryName = zipEntry.getName();
                    final long zipEntryTime = zipEntry.getTime();
                    final long zipEntryCompressedSize = zipEntry.getCompressedSize();
                    final String type = zipEntry.isDirectory() ? "directory" : "file";

                    if (logger.isInfoEnabled()) {
                        logger.info(String.format(
                                "Unpacking Zip Entry - Name: '%s',Time: '%s', "
                                        + "Compressed Size: '%s', Type: '%s'",
                                zipEntryName, zipEntryTime, zipEntryCompressedSize, type));
                    }

                    if (ZipResultType.FILE.equals(zipResultType)) {
                        final File destinationFile = checkPath(message, zipEntryName);

                        if (zipEntry.isDirectory()) {
                            destinationFile.mkdirs(); //NOSONAR false positive
                        } else {
                            SpringZipUtils.copy(zipEntryInputStream, destinationFile);
                            uncompressedData.put(zipEntryName, destinationFile);
                        }
                    } else if (ZipResultType.BYTE_ARRAY.equals(zipResultType)) {
                        if (!zipEntry.isDirectory()) {
                            checkPath(message, zipEntryName);
                            byte[] data = IOUtils.toByteArray(zipEntryInputStream);
                            uncompressedData.put(zipEntryName, data);
                        }
                    } else {
                        throw new IllegalStateException("Unsupported zipResultType " + zipResultType);
                    }
                }

                public File checkPath(final Message<?> message, final String zipEntryName) throws IOException {
                    final File tempDir = new File(workDirectory, message.getHeaders().getId().toString());
                    tempDir.mkdirs(); //NOSONAR false positive
                    final File destinationFile = new File(tempDir, zipEntryName);

                    /* If we see the relative traversal string of ".." we need to make sure
                     * that the outputdir + name doesn't leave the outputdir.
                     */
                    if (!destinationFile.getCanonicalPath().startsWith(workDirectory.getCanonicalPath())) {
                        throw new ZipException("The file " + zipEntryName
                                + " is trying to leave the target output directory of " + workDirectory);
                    }
                    return destinationFile;
                }
            });

            if (uncompressedData.isEmpty()) {
                if (logger.isWarnEnabled()) {
                    logger.warn(
                            "No data unzipped from payload with message Id " + message.getHeaders().getId());
                }
                unzippedData = null;
            } else {

                if (this.expectSingleResult) {
                    if (uncompressedData.size() == 1) {
                        unzippedData = uncompressedData.values().iterator().next();
                    } else {
                        throw new MessagingException(message,
                                String.format(
                                        "The UnZip operation extracted %s "
                                                + "result objects but expectSingleResult was 'true'.",
                                        uncompressedData.size()));
                    }
                } else {
                    unzippedData = uncompressedData;
                }

            }
        } finally {
            IOUtils.closeQuietly(inputStream);
            if (payload instanceof File && this.deleteFiles) {
                final File filePayload = (File) payload;
                if (!filePayload.delete() && logger.isWarnEnabled()) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("failed to delete File '" + filePayload + "'");
                    }
                }
            }
        }
        return unzippedData;
    } catch (Exception e) {
        throw new MessageHandlingException(message, "Failed to apply Zip transformation.", e);
    }
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.VehicleLocationSimulationServiceImpl.java

@Override
public int addSimulationForBlockInstance(AgencyAndId blockId, long serviceDate, long actualTime,
        boolean bypassInference, boolean isRunBased, boolean realtime, boolean fillActual,
        boolean reportsOperatorId, boolean reportsRunId, boolean allowRunTransitions, Properties properties) {

    final Random random = new Random();

    final SimulatorTask task = new SimulatorTask();

    final String tag = generateBlockServiceDateTag(blockId, serviceDate);
    task.addTag(tag);//from w  w  w.  j  a v a 2  s  .  c  o m

    task.setPauseOnStart(false);
    task.setRunInRealtime(realtime);
    task.setBypassInference(bypassInference);
    task.setFillActualProperties(fillActual);

    final BlockInstance blockInstance = _blockCalendarService.getBlockInstance(blockId, serviceDate);
    final BlockConfigurationEntry block = blockInstance.getBlock();

    int scheduleTime = (int) ((actualTime - serviceDate) / 1000);
    final int shiftStartTime = getShiftStartTimeProperty(properties);
    scheduleTime -= shiftStartTime;

    final AgencyAndId vehicleId = getVehicleIdProperty(random, properties, blockId.getAgencyId());

    final double locationSigma = getLocationSigma(properties);
    final SortedMap<Double, Integer> scheduleDeviations = getScheduleDeviations(properties);

    if (isRunBased) {

        final RunTripEntry runTrip = _runService.getActiveRunTripEntryForBlockInstance(blockInstance,
                scheduleTime);

        if (runTrip == null) {
            _log.warn("no runTrip for blockInstance=" + blockInstance + " and scheduleTime=" + scheduleTime);
            return -1;
        }

        _log.info("Using runTrip=" + runTrip.toString());

        // TODO pass these along to run-simulation method
        // List<Double> transitionParams =
        // getRunTransitionParams(properties);

        // TODO create "config" object to hold this mess?
        generateRunSim(random, task, runTrip, serviceDate, scheduleTime, shiftStartTime, reportsOperatorId,
                reportsRunId, allowRunTransitions, scheduleDeviations, locationSigma, vehicleId);

        return addTask(task);
    }

    final List<BlockStopTimeEntry> stopTimes = block.getStopTimes();

    final BlockStopTimeEntry firstStopTime = stopTimes.get(0);
    final BlockStopTimeEntry lastStopTime = stopTimes.get(stopTimes.size() - 1);
    final int firstTime = firstStopTime.getStopTime().getArrivalTime();
    final int lastTime = lastStopTime.getStopTime().getDepartureTime();

    if (isIncludeStartTime(properties))
        scheduleTime = firstTime;

    scheduleTime = Math.max(firstTime, scheduleTime);

    while (scheduleTime <= lastTime) {

        final NycTestInferredLocationRecord record = new NycTestInferredLocationRecord();

        final ScheduledBlockLocation blockLocation = _scheduledBlockLocationService
                .getScheduledBlockLocationFromScheduledTime(block, scheduleTime);

        /**
         * Not in service?
         */
        if (blockLocation == null)
            return -1;

        final BlockTripEntry trip = blockLocation.getActiveTrip();

        final AgencyAndId tripId = trip.getTrip().getId();

        final CoordinatePoint location = blockLocation.getLocation();
        record.setActualBlockId(AgencyAndIdLibrary.convertToString(blockId));
        record.setActualDistanceAlongBlock(blockLocation.getDistanceAlongBlock());
        final int actualScheduleTime = blockLocation.getScheduledTime();

        String dsc = _destinationSignCodeService.getDestinationSignCodeForTripId(tripId);
        if (dsc == null)
            dsc = "0";

        int scheduleDeviation = 0;

        if (!scheduleDeviations.isEmpty()) {
            final double ratio = (scheduleTime - firstTime) / ((double) (lastTime - firstTime));
            scheduleDeviation = (int) InterpolationLibrary.interpolate(scheduleDeviations, ratio,
                    EOutOfRangeStrategy.LAST_VALUE);
        }

        final long timestamp = serviceDate + (scheduleTime + shiftStartTime + scheduleDeviation) * 1000;

        final CoordinatePoint p = applyLocationNoise(location.getLat(), location.getLon(), locationSigma,
                random);

        record.setDsc(dsc);
        record.setLat(p.getLat());
        record.setLon(p.getLon());
        record.setTimestamp(timestamp);
        record.setVehicleId(vehicleId);
        record.setActualServiceDate(serviceDate);
        record.setActualScheduleTime(actualScheduleTime);
        record.setActualDsc(dsc);
        record.setActualPhase(EVehiclePhase.IN_PROGRESS.toString());

        task.addRecord(record);

        scheduleTime += 60 + random.nextGaussian() * 10;
    }

    return addTask(task);
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

/**
 * Test method for 'java.util.Map.put(Object, Object)'. This test shows some
 * bad behavior of the TreeMap class before JDK 7. A mapping with null key can
 * be put in but several methods are are unusable afterward.
 *
 * A SortedMap with natural ordering (no comparator) is supposed to throw a
 * null pointer exception if a null keys are "not supported". For a natural
 * ordered TreeMap before JDK 7, a null pointer exception is not thrown. But,
 * the map is left in a state where any other key based methods result in a
 * null pointer exception./*from  w ww .j ava2  s.c  o  m*/
 *
 * @see java.util.Map#put(Object, Object)
 */
public void testPut_nullKey() {
    K[] keys = getSortedKeys();
    V[] values = getSortedValues();
    SortedMap<K, V> sortedMap = createNavigableMap();

    if (useNullKey()) {
        assertNull(sortedMap.put(null, values[0]));
        assertTrue(sortedMap.containsValue(values[0]));

        // the map methods the continue to function
        sortedMap.containsValue(null);
        sortedMap.containsValue(values[0]);
        sortedMap.entrySet();
        sortedMap.equals(createMap());
        sortedMap.hashCode();
        sortedMap.isEmpty();
        sortedMap.keySet();
        sortedMap.putAll(createMap());
        sortedMap.size();
        sortedMap.values();

        // all of the sorted map methods still function
        sortedMap.comparator();
        sortedMap.firstKey();
        sortedMap.lastKey();
        sortedMap.subMap(getLessThanMinimumKey(), getGreaterThanMaximumKey());
        sortedMap.headMap(getLessThanMinimumKey());
        sortedMap.tailMap(getLessThanMinimumKey());
    } else if (TestUtils.getJdkVersion() > 6) {
        // nulls are rejected immediately and don't poison the map anymore
        try {
            assertNull(sortedMap.put(null, values[0]));
            fail("should have thrown");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            assertNull(sortedMap.put(null, values[1]));
            fail("expected exception adding second null");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.containsKey(null);
            fail("expected exception on containsKey(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        sortedMap.containsKey(keys[0]);
        try {
            sortedMap.get(null);
            fail("expected exception on get(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        sortedMap.get(keys[0]);
        try {
            sortedMap.remove(null);
        } catch (NullPointerException e) {
            // expected
        }
        sortedMap.remove(keys[0]);
    } else {
        // before JDK 7, nulls poisoned the map
        try {
            assertNull(sortedMap.put(null, values[0]));
            // note: first null added is not required to throw NPE since no
            // comparisons are needed
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            assertNull(sortedMap.put(null, values[1]));
            fail("expected exception adding second null");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.containsKey(null);
            fail("expected exception on containsKey(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.containsKey(keys[0]);
            fail("expected exception on contains(key)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.get(null);
            fail("expected exception on get(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.get(keys[0]);
            fail("expected exception on get(key)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.remove(null);
            fail("expected exception on remove(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.remove(keys[0]);
            fail("expected exception on remove(key)");
        } catch (NullPointerException e) {
            // expected outcome
        }
    }
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

public void testHeadMapLjava_lang_Object() {
    K[] keys = getSortedKeys();//from   www . j  a  v a2s  . c o  m
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    for (int i = 0; i < keys.length; i++) {
        map.put(keys[i], values[i]);
    }

    Map<K, V> head = map.headMap(keys[3]);
    assertEquals(3, head.size());
    assertTrue(head.containsKey(keys[0]));
    assertTrue(head.containsValue(values[1]));
    assertTrue(head.containsKey(keys[2]));

    if (useNullKey() && useNullValue()) {
        map.put(null, null);

        SortedMap<K, V> submap = map.headMap(null);
        assertEquals(0, submap.size());

        Set<K> keySet = submap.keySet();
        assertEquals(0, keySet.size());

        Set<Map.Entry<K, V>> entrySet = submap.entrySet();
        assertEquals(0, entrySet.size());

        Collection<V> valueCollection = submap.values();
        assertEquals(0, valueCollection.size());

        map.remove(null);
    }

    SortedMap<K, V> submap = map.headMap(getLessThanMinimumKey());
    assertEquals(submap.size(), 0);
    assertTrue(submap.isEmpty());
    try {
        submap.firstKey();
        fail("NoSuchElementException should be thrown");
    } catch (NoSuchElementException expected) {
    }

    try {
        submap.lastKey();
        fail("NoSuchElementException should be thrown");
    } catch (NoSuchElementException expected) {
    }

    try {
        submap.headMap(null);
        assertTrue("expected exception", useNullKey());
    } catch (NullPointerException e) {
        assertFalse("unexpected NPE", useNullKey());
    }
}

From source file:org.apache.nifi.provenance.MiNiFiPersistentProvenanceRepository.java

/**
 * <p>//from   w  w w. j  ava  2s  .c  o m
 * Merges all of the given Journal Files into a single, merged Provenance Event Log File. As these records are merged, they will be compressed, if the repository is configured to compress records
 * </p>
 * <p>
 * <p>
 * If the repository is configured to compress the data, the file written to may not be the same as the <code>suggestedMergeFile</code>, as a filename extension of '.gz' may be appended. If the
 * journals are successfully merged, the file that they were merged into will be returned. If unable to merge the records (for instance, because the repository has been closed or because the list
 * of journal files was empty), this method will return <code>null</code>.
 * </p>
 *
 * @param journalFiles       the journal files to merge
 * @param suggestedMergeFile the file to write the merged records to
 * @param eventReporter      the event reporter to report any warnings or errors to; may be null.
 * @return the file that the given journals were merged into, or <code>null</code> if no records were merged.
 * @throws IOException if a problem occurs writing to the mergedFile, reading from a journal
 */
File mergeJournals(final List<File> journalFiles, final File suggestedMergeFile,
        final EventReporter eventReporter) throws IOException {
    if (this.closed.get()) {
        logger.info("Provenance Repository has been closed; will not merge journal files to {}",
                suggestedMergeFile);
        return null;
    }

    if (journalFiles.isEmpty()) {
        logger.debug("Couldn't merge journals: Journal Files is empty; won't merge journals");
        return null;
    }

    Collections.sort(journalFiles, new Comparator<File>() {
        @Override
        public int compare(final File o1, final File o2) {
            final String suffix1 = StringUtils.substringAfterLast(o1.getName(), ".");
            final String suffix2 = StringUtils.substringAfterLast(o2.getName(), ".");

            try {
                final int journalIndex1 = Integer.parseInt(suffix1);
                final int journalIndex2 = Integer.parseInt(suffix2);
                return Integer.compare(journalIndex1, journalIndex2);
            } catch (final NumberFormatException nfe) {
                return o1.getName().compareTo(o2.getName());
            }
        }
    });

    final String firstJournalFile = journalFiles.get(0).getName();
    final String firstFileSuffix = StringUtils.substringAfterLast(firstJournalFile, ".");
    final boolean allPartialFiles = firstFileSuffix.equals("0");

    // check if we have all of the "partial" files for the journal.
    if (allPartialFiles) {
        if (suggestedMergeFile.exists()) {
            // we have all "partial" files and there is already a merged file. Delete the data from the index
            // because the merge file may not be fully merged. We will re-merge.
            logger.warn("Merged Journal File {} already exists; however, all partial journal files also exist "
                    + "so assuming that the merge did not finish. Repeating procedure in order to ensure consistency.");

            // Since we only store the file's basename, block offset, and event ID, and because the newly created file could end up on
            // a different Storage Directory than the original, we need to ensure that we delete both the partially merged
            // file and the TOC file. Otherwise, we could get the wrong copy and have issues retrieving events.
            if (!suggestedMergeFile.delete()) {
                logger.error(
                        "Failed to delete partially written Provenance Journal File {}. This may result in events from this journal "
                                + "file not being able to be displayed. This file should be deleted manually.",
                        suggestedMergeFile);
            }

            final File tocFile = TocUtil.getTocFile(suggestedMergeFile);
            if (tocFile.exists() && !tocFile.delete()) {
                logger.error(
                        "Failed to delete .toc file {}; this may result in not being able to read the Provenance Events from the {} Journal File. "
                                + "This can be corrected by manually deleting the {} file",
                        tocFile, suggestedMergeFile, tocFile);
            }
        }
    } else {
        logger.warn("Cannot merge journal files {} because expected first file to end with extension '.0' "
                + "but it did not; assuming that the files were already merged but only some finished deletion "
                + "before restart. Deleting remaining partial journal files.", journalFiles);

        for (final File file : journalFiles) {
            if (!file.delete() && file.exists()) {
                logger.warn(
                        "Failed to delete unneeded journal file {}; this file should be cleaned up manually",
                        file);
            }
        }

        return null;
    }

    final long startNanos = System.nanoTime();

    // Map each journal to a RecordReader
    final List<RecordReader> readers = new ArrayList<>();
    int records = 0;

    final boolean isCompress = configuration.isCompressOnRollover();
    final File writerFile = isCompress
            ? new File(suggestedMergeFile.getParentFile(), suggestedMergeFile.getName() + ".gz")
            : suggestedMergeFile;

    try {
        for (final File journalFile : journalFiles) {
            try {
                // Use MAX_VALUE for number of chars because we don't want to truncate the value as we write it
                // out. This allows us to later decide that we want more characters and still be able to retrieve
                // the entire event.
                readers.add(RecordReaders.newRecordReader(journalFile, null, Integer.MAX_VALUE));
            } catch (final EOFException eof) {
                // there's nothing here. Skip over it.
            } catch (final IOException ioe) {
                logger.warn("Unable to merge {} with other Journal Files due to {}", journalFile,
                        ioe.toString());
                if (logger.isDebugEnabled()) {
                    logger.warn("", ioe);
                }

                if (eventReporter != null) {
                    eventReporter.reportEvent(Severity.ERROR, EVENT_CATEGORY, "re " + ioe.toString());
                }
            }
        }

        // Create a Map so that the key is the next record available from a reader and the value is the Reader from which
        // the record came. This sorted map is then used so that we are able to always get the first entry, which is the next
        // lowest record id
        final SortedMap<StandardProvenanceEventRecord, RecordReader> recordToReaderMap = new TreeMap<>(
                new Comparator<StandardProvenanceEventRecord>() {
                    @Override
                    public int compare(final StandardProvenanceEventRecord o1,
                            final StandardProvenanceEventRecord o2) {
                        return Long.compare(o1.getEventId(), o2.getEventId());
                    }
                });

        long minEventId = 0L;
        long earliestTimestamp = System.currentTimeMillis();
        for (final RecordReader reader : readers) {
            StandardProvenanceEventRecord record = null;

            try {
                record = reader.nextRecord();
            } catch (final EOFException eof) {
            } catch (final Exception e) {
                logger.warn("Failed to generate Provenance Event Record from Journal due to " + e
                        + "; it's possible that the record wasn't "
                        + "completely written to the file. This record will be skipped.");
                if (logger.isDebugEnabled()) {
                    logger.warn("", e);
                }

                if (eventReporter != null) {
                    eventReporter.reportEvent(Severity.WARNING, EVENT_CATEGORY,
                            "Failed to read Provenance Event Record from Journal due to " + e
                                    + "; it's possible that hte record wasn't completely written to the file. This record will be skipped.");
                }
            }

            if (record == null) {
                continue;
            }

            if (record.getEventTime() < earliestTimestamp) {
                earliestTimestamp = record.getEventTime();
            }

            if (record.getEventId() < minEventId) {
                minEventId = record.getEventId();
            }

            recordToReaderMap.put(record, reader);
        }

        // loop over each entry in the map, persisting the records to the merged file in order, and populating the map
        // with the next entry from the journal file from which the previous record was written.
        try (final RecordWriter writer = RecordWriters.newSchemaRecordWriter(writerFile,
                configuration.isCompressOnRollover(), true)) {
            writer.writeHeader(minEventId);

            while (!recordToReaderMap.isEmpty()) {
                final Map.Entry<StandardProvenanceEventRecord, RecordReader> entry = recordToReaderMap
                        .entrySet().iterator().next();
                final StandardProvenanceEventRecord record = entry.getKey();
                final RecordReader reader = entry.getValue();

                writer.writeRecord(record, record.getEventId());
                final int blockIndex = writer.getTocWriter().getCurrentBlockIndex();

                records++;

                // Remove this entry from the map
                recordToReaderMap.remove(record);

                // Get the next entry from this reader and add it to the map
                StandardProvenanceEventRecord nextRecord = null;

                try {
                    nextRecord = reader.nextRecord();
                } catch (final EOFException eof) {
                }

                if (nextRecord != null) {
                    recordToReaderMap.put(nextRecord, reader);
                }
            }
        }
    } finally {
        for (final RecordReader reader : readers) {
            try {
                reader.close();
            } catch (final IOException ioe) {
            }
        }
    }

    // Success. Remove all of the journal files, as they're no longer needed, now that they've been merged.
    for (final File journalFile : journalFiles) {
        if (!journalFile.delete() && journalFile.exists()) {
            logger.warn("Failed to remove temporary journal file {}; this file should be cleaned up manually",
                    journalFile.getAbsolutePath());

            if (eventReporter != null) {
                eventReporter.reportEvent(Severity.WARNING, EVENT_CATEGORY,
                        "Failed to remove temporary journal file " + journalFile.getAbsolutePath()
                                + "; this file should be cleaned up manually");
            }
        }

        final File tocFile = getTocFile(journalFile);
        if (!tocFile.delete() && tocFile.exists()) {
            logger.warn(
                    "Failed to remove temporary journal TOC file {}; this file should be cleaned up manually",
                    tocFile.getAbsolutePath());

            if (eventReporter != null) {
                eventReporter.reportEvent(Severity.WARNING, EVENT_CATEGORY,
                        "Failed to remove temporary journal TOC file " + tocFile.getAbsolutePath()
                                + "; this file should be cleaned up manually");
            }
        }
    }

    if (records == 0) {
        writerFile.delete();
        logger.debug("Couldn't merge journals: No Records to merge");
        return null;
    } else {
        final long nanos = System.nanoTime() - startNanos;
        final long millis = TimeUnit.MILLISECONDS.convert(nanos, TimeUnit.NANOSECONDS);
        logger.info(
                "Successfully merged {} journal files ({} records) into single Provenance Log File {} in {} milliseconds",
                journalFiles.size(), records, suggestedMergeFile, millis);
    }

    return writerFile;
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

public void testSubMap_entrySet() {
    K[] keys = getSortedKeys();/*from w w  w . j  a  v  a2s . c  o m*/
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    map.put(keys[0], values[0]);
    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);
    map.put(keys[3], values[3]);

    SortedMap<K, V> subMap = map.subMap(keys[1], keys[3]);
    Set<Entry<K, V>> entries = subMap.entrySet();
    assertEquals(2, subMap.size());
    assertEquals(subMap.size(), entries.size());
    assertFalse(entries.contains(new SimpleEntry<K, V>(keys[0], values[0])));
    assertTrue(entries.contains(new SimpleEntry<K, V>(keys[1], values[1])));
    assertTrue(entries.contains(new SimpleEntry<K, V>(keys[2], values[2])));
    assertFalse(entries.contains(new SimpleEntry<K, V>(keys[3], values[3])));

    entries.remove(new SimpleEntry<K, V>(keys[1], values[1]));
    assertEquals(3, map.size());
    assertEquals(subMap.size(), entries.size());
    assertFalse(entries.contains(new SimpleEntry<K, V>(keys[1], values[1])));
    assertFalse(subMap.containsKey(keys[1]));
    assertFalse(subMap.containsValue(values[1]));

    entries.clear();
    assertEquals(2, map.size());
    assertEquals(subMap.size(), entries.size());
    assertTrue(entries.isEmpty());
    assertTrue(subMap.isEmpty());

    subMap.put(keys[2], values[2]);
    assertEquals(1, subMap.size());
    assertEquals(subMap.size(), entries.size());

    subMap.put(keys[1], values[1]);
    Iterator<Entry<K, V>> it = entries.iterator();
    while (it.hasNext()) {
        Map.Entry<K, V> entry = it.next();
        subMap.containsKey(entry.getKey());
        subMap.containsValue(entry.getValue());
        it.remove();
    }
    try {
        it.next();
        fail("should throw NoSuchElementException");
    } catch (NoSuchElementException expected) {
    }
    assertEquals(2, map.size());
    assertEquals(0, subMap.size());
    assertEquals(subMap.size(), entries.size());

    map = createNavigableMap();
    Set<Entry<K, V>> entrySet = map.entrySet();
    map.put(keys[0], values[0]);
    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
    map.clear();
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
    map.put(keys[0], values[0]);
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
    entrySet.clear();
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
}