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:viper.api.time.TimeEncodedList.java

/**
 * {@inheritDoc}/*from  w  w w .j a v  a 2  s . com*/
 */
public Comparable firstAfterOrAt(Comparable c) {
    SortedMap tail = values.tailMap(c);
    if (!tail.isEmpty()) {
        return (Comparable) tail.firstKey();
    }
    return null;
}

From source file:viper.api.time.TimeEncodedList.java

/**
 * Get the value at the specified index in the list.
 * @param index the index into the list//from  www .  j  av a2s.c o m
 * @return the value at the specified index
 */
public Object get(Instant index) {
    assert index != null;
    SortedMap m = values.tailMap(index);
    if (!m.isEmpty() && m.firstKey().equals(index)) {
        return ((LelNode) m.get(index)).getValue();
    }
    m = values.headMap(index);
    if (!m.isEmpty()) {
        LelNode v = (LelNode) m.get(m.lastKey());
        if (v.getEnd().compareTo(index) > 0) {
            return v.getValue();
        }
    }
    return null;
}

From source file:viper.api.time.TimeEncodedList.java

/**
 * {@inheritDoc}//from  w w w  . j  a va  2 s . c o m
 */
public Comparable firstAfter(Comparable c) {
    SortedMap tail = values.tailMap(c);
    if (!tail.isEmpty()) {
        Iterator iter = tail.keySet().iterator();
        Comparable a = (Comparable) iter.next();
        if (a.compareTo(c) == 0) {
            if (iter.hasNext()) {
                a = (Comparable) iter.next();
            } else {
                return null;
            }
        }
        return c;
    }
    return null;
}

From source file:com.neoteric.starter.metrics.report.elastic.ElasticsearchReporter.java

@Override
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
        SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
        SortedMap<String, Timer> timers) {

    // nothing to do if we dont have any metrics to report
    if (gauges.isEmpty() && counters.isEmpty() && histograms.isEmpty() && meters.isEmpty()
            && timers.isEmpty()) {
        LOGGER.info("All metrics empty, nothing to report");
        return;//w w w. j  a va 2  s .c o m
    }

    if (!checkedForIndexTemplate) {
        checkForIndexTemplate();
    }
    final long timestamp = clock.getTime() / 1000;

    currentIndexName = index;
    if (indexDateFormat != null) {
        currentIndexName += "-" + indexDateFormat.format(new Date(timestamp * 1000));
    }

    try {
        HttpURLConnection connection;
        try {
            connection = openConnection("/_bulk", "POST");
        } catch (ElasticsearchConnectionException e) {
            LOGGER.error("Could not connect to any configured elasticsearch instances: {}",
                    Arrays.asList(hosts), e);
            return;
        }

        List<JsonMetric> percolationMetrics = new ArrayList<>();
        AtomicInteger entriesWritten = new AtomicInteger(0);

        for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
            if (entry.getValue().getValue() != null) {
                JsonMetric jsonMetric = new JsonGauge(name(prefix, entry.getKey()), timestamp,
                        entry.getValue());
                connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection,
                        entriesWritten);
                addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
            }
        }

        for (Map.Entry<String, Counter> entry : counters.entrySet()) {
            JsonCounter jsonMetric = new JsonCounter(name(prefix, entry.getKey()), timestamp, entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
            JsonHistogram jsonMetric = new JsonHistogram(name(prefix, entry.getKey()), timestamp,
                    entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        for (Map.Entry<String, Meter> entry : meters.entrySet()) {
            JsonMeter jsonMetric = new JsonMeter(name(prefix, entry.getKey()), timestamp, entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        for (Map.Entry<String, Timer> entry : timers.entrySet()) {
            JsonTimer jsonMetric = new JsonTimer(name(prefix, entry.getKey()), timestamp, entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        closeConnection(connection);

        // execute the notifier impl, in case percolation found matches
        if (percolationMetrics.size() > 0 && notifier != null) {
            for (JsonMetric jsonMetric : percolationMetrics) {
                List<String> matches = getPercolationMatches(jsonMetric);
                for (String match : matches) {
                    notifier.notify(jsonMetric, match);
                }
            }
        }
        // catch the exception to make sure we do not interrupt the live application
    } catch (ElasticsearchConnectionException e) {
        LOGGER.error("Couldnt report to elasticsearch server", e);
    } catch (IOException e) {
        LOGGER.error("Couldnt report to elasticsearch server", e);
    }
}

From source file:viper.api.time.TimeEncodedList.java

/**
 * Removes all values at the given range. Note that, like 
 * <code>SortedMap</code>, this means that value is set in the
 * range from start, inclusive, to stop, exclusive.
 * @param start the first index to remove
 * @param stop the first index that is not removed
 * @return <code>true</code> if any elements were removed
 *//*from  www .jav a  2 s. com*/
public boolean remove(Comparable start, Comparable stop) {
    boolean someFound = false;
    SortedMap head = values.headMap(start);
    if (!head.isEmpty()) {
        LelNode n = (LelNode) head.get(head.lastKey());
        if (n.getEnd().compareTo(start) > 0) {
            someFound = true;
            head.put(head.lastKey(), new LelNode((Instant) start, n.getValue()));
            if (n.getEnd().compareTo(stop) > 0) {
                // this object spans the whole removed range
                values.put(stop, new TimeEncodedList.LelNode(n.getEnd(), n.getValue()));
                return true;
            }
        }
    }

    // By now, will have removed anything coming in from the head
    // Just remove the stuff in the subMap, making sure to put back
    // the leftovers.
    SortedMap sub = values.subMap(start, stop);
    if (!sub.isEmpty()) {
        LelNode n = (LelNode) sub.get(sub.lastKey());
        if (n.getEnd().compareTo(stop) > 0) {
            values.put(stop, new LelNode(n.getEnd(), n.getValue()));
        }
        values.subMap(start, stop).clear();
        return true;
    }
    return someFound;
}

From source file:com.abuabdul.knodex.service.KxDocumentServiceImpl.java

public SortedMap<String, List<KnodexDoc>> listAllSentences() {
    log.debug("Entered KxDocumentServiceImpl.listAllSentences method");
    String indexKey = "";
    List<KnodexDoc> indexKnodexList = null;
    SortedMap<String, List<KnodexDoc>> fullListOfSentences = null;

    List<KnodexDoc> listAllKnodex = knodexDAO.findAll();

    if (listAllKnodex != null && !listAllKnodex.isEmpty()) {
        log.debug("The size of the overall key with sentences " + listAllKnodex.size());
        fullListOfSentences = new TreeMap<String, List<KnodexDoc>>();
        for (KnodexDoc knodexDoc : listAllKnodex) {
            if (knodexDoc != null && !StringUtils.isEmpty(knodexDoc.getKey())) {
                indexKey = knodexDoc.getKey();
                log.debug("Index key to get the list of sentences for each indexBy value " + indexKey);
                if (!fullListOfSentences.isEmpty() && fullListOfSentences.containsKey(indexKey)) {
                    indexKnodexList = fullListOfSentences.get(indexKey);
                    if (indexKnodexList == null) {
                        // Ideally this code will not execute
                        indexKnodexList = new ArrayList<KnodexDoc>();
                    }// w  w w.  j  av  a 2 s .c  o  m
                    indexKnodexList.add(knodexDoc);
                } else {
                    indexKnodexList = new ArrayList<KnodexDoc>();
                    indexKnodexList.add(knodexDoc);
                    fullListOfSentences.put(indexKey, indexKnodexList);
                }
            }
        }
    }
    return fullListOfSentences;
}

From source file:viper.api.time.TimeEncodedList.java

/**
 * Sets the value at the given range. Note that, like 
 * <code>SortedMap</code>, this means that value is set in the
 * range from start, inclusive, to stop, exclusive.
 * @param start the first index to set/*from ww  w  .  j a  v  a 2s. c  o  m*/
 * @param stop the first index that is not set
 * @param value all elements in the list in the range [start, stop)
 *   will take this value
 * @throws IllegalArgumentException if start is not less than stop
 */
public void set(Comparable start, Comparable stop, Object value) {
    if (start.compareTo(stop) >= 0) {
        throw new IllegalArgumentException("Start not strictly less than stop: " + start + " !< " + stop);
    }
    if (value == null) {
        remove(start, stop);
        return;
    }
    SortedMap head = values.headMap(start);
    if (!head.isEmpty()) {
        LelNode n = (LelNode) head.get(head.lastKey());
        if (n.getValue().equals(value)) {
            if (n.getEnd().compareTo(start) >= 0) {
                start = (Instant) head.lastKey();
            }
        } else if (n.getEnd().compareTo(start) > 0) {
            head.put(head.lastKey(), new LelNode((Instant) start, n.getValue()));
            if (n.getEnd().compareTo(stop) > 0) {
                values.put(start, new LelNode((Instant) stop, value));
                values.put(stop, new LelNode(n.getEnd(), n.getValue()));
                return;
            }
        }
    }
    SortedMap sub = values.subMap(start, stop);
    if (!sub.isEmpty()) {
        LelNode n = (LelNode) sub.get(sub.lastKey());
        if (n.getValue().equals(value)) {
            if (n.getEnd().compareTo(stop) > 0) {
                stop = n.getEnd();
            }
        } else if (n.getEnd().compareTo(stop) > 0) {
            values.put(stop, new LelNode(n.getEnd(), n.getValue()));
        }
    }
    values.subMap(start, stop).clear();
    values.put(start, new TimeEncodedList.LelNode((Instant) stop, value));
}

From source file:eagle.log.entity.meta.IndexDefinition.java

private byte[] generateUniqueIndexRowkey(byte[][] indexValues, int[] partitionHashCodes,
        SortedMap<Integer, Integer> tagMap) {
    final int prefixHashCode = indexPrefix.hashCode();
    int totalLength = 4;
    totalLength += (partitionHashCodes != null) ? (4 * partitionHashCodes.length) : 0;

    totalLength += (2 * indexValues.length);
    for (int i = 0; i < indexValues.length; ++i) {
        final byte[] value = indexValues[i];
        totalLength += value.length;/*from   w w w .  j av  a2 s  . com*/
    }
    if (tagMap != null && (!tagMap.isEmpty())) {
        totalLength += tagMap.size() * 8;
    }

    int offset = 0;
    final byte[] rowkey = new byte[totalLength];

    // 1. set prefix
    ByteUtil.intToBytes(prefixHashCode, rowkey, offset);
    offset += 4;

    // 2. set partition
    if (partitionHashCodes != null) {
        for (Integer partitionHashCode : partitionHashCodes) {
            ByteUtil.intToBytes(partitionHashCode, rowkey, offset);
            offset += 4;
        }
    }

    // 3. set index values
    for (int i = 0; i < columns.length; ++i) {
        ByteUtil.shortToBytes((short) indexValues[i].length, rowkey, offset);
        offset += 2;
        for (int j = 0; j < indexValues[i].length; ++j) {
            rowkey[offset++] = indexValues[i][j];
        }
    }

    // Check if it's non clustered index, then set the tag/value hash code
    if (tagMap != null && (!tagMap.isEmpty())) {
        // 4. set tag key/value hashes
        for (Map.Entry<Integer, Integer> entry : tagMap.entrySet()) {
            ByteUtil.intToBytes(entry.getKey(), rowkey, offset);
            offset += 4;
            ByteUtil.intToBytes(entry.getValue(), rowkey, offset);
            offset += 4;
        }
    }

    return rowkey;
}

From source file:net.ripe.rpki.commons.crypto.rfc3779.ResourceExtensionEncoder.java

/**
 * Encode the IP Address Block extension for Resource Certificates. This
 * extension is identified by {@link #OID_IP_ADDRESS_BLOCKS}.
 *
 * @param inheritIpv4 inherit IPv4 resources from signing certificate.
 * @param inheritIpv6 inherit IPv6 resources from signing certificate.
 * @param resources   the set of IPv4 and IPv6 resources.
 * @return the DER encoding of the IP Address Block Extension.
 *//*from   w w w.  j a  va2 s.co m*/
public ASN1Object encodeIpAddressBlocks(boolean inheritIpv4, boolean inheritIpv6, IpResourceSet resources) {
    SortedMap<AddressFamily, IpResourceSet> addressBlocks = new TreeMap<AddressFamily, IpResourceSet>();

    if (inheritIpv4) {
        addressBlocks.put(AddressFamily.IPV4, null);
    } else if (resources.containsType(IpResourceType.IPv4)) {
        addressBlocks.put(AddressFamily.IPV4, resources);
    }

    if (inheritIpv6) {
        addressBlocks.put(AddressFamily.IPV6, null);
    } else if (resources.containsType(IpResourceType.IPv6)) {
        addressBlocks.put(AddressFamily.IPV6, resources);
    }

    return addressBlocks.isEmpty() ? null : ipAddressBlocksToDer(addressBlocks);
}

From source file:org.elasticsearch.metrics.ElasticsearchReporter.java

@Override
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
        SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
        SortedMap<String, Timer> timers) {

    // nothing to do if we dont have any metrics to report
    if (gauges.isEmpty() && counters.isEmpty() && histograms.isEmpty() && meters.isEmpty()
            && timers.isEmpty()) {
        LOGGER.info("All metrics empty, nothing to report");
        return;//from  w  w w. j  a  v  a2s .c  o  m
    }

    if (!checkedForIndexTemplate) {
        checkForIndexTemplate();
    }
    final long timestamp = clock.getTime() / 1000;

    currentIndexName = index;
    if (indexDateFormat != null) {
        currentIndexName += "-" + indexDateFormat.format(new Date(timestamp * 1000));
    }

    try {
        HttpURLConnection connection = openConnection("/_bulk", "POST");
        if (connection == null) {
            LOGGER.error("Could not connect to any configured elasticsearch instances: {}",
                    Arrays.asList(hosts));
            return;
        }

        List<JsonMetric> percolationMetrics = new ArrayList<>();
        AtomicInteger entriesWritten = new AtomicInteger(0);

        for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
            if (entry.getValue().getValue() != null) {
                JsonMetric jsonMetric = new JsonGauge(name(prefix, entry.getKey()), timestamp,
                        entry.getValue());
                connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection,
                        entriesWritten);
                addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
            }
        }

        for (Map.Entry<String, Counter> entry : counters.entrySet()) {
            JsonCounter jsonMetric = new JsonCounter(name(prefix, entry.getKey()), timestamp, entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
            JsonHistogram jsonMetric = new JsonHistogram(name(prefix, entry.getKey()), timestamp,
                    entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        for (Map.Entry<String, Meter> entry : meters.entrySet()) {
            JsonMeter jsonMetric = new JsonMeter(name(prefix, entry.getKey()), timestamp, entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        for (Map.Entry<String, Timer> entry : timers.entrySet()) {
            JsonTimer jsonMetric = new JsonTimer(name(prefix, entry.getKey()), timestamp, entry.getValue());
            connection = writeJsonMetricAndRecreateConnectionIfNeeded(jsonMetric, connection, entriesWritten);
            addJsonMetricToPercolationIfMatching(jsonMetric, percolationMetrics);
        }

        closeConnection(connection);

        // execute the notifier impl, in case percolation found matches
        if (percolationMetrics.size() > 0 && notifier != null) {
            for (JsonMetric jsonMetric : percolationMetrics) {
                List<String> matches = getPercolationMatches(jsonMetric);
                for (String match : matches) {
                    notifier.notify(jsonMetric, match);
                }
            }
        }
        // catch the exception to make sure we do not interrupt the live application
    } catch (IOException e) {
        LOGGER.error("Couldnt report to elasticsearch server", e);
    }
}