Example usage for com.google.common.collect Maps newTreeMap

List of usage examples for com.google.common.collect Maps newTreeMap

Introduction

In this page you can find the example usage for com.google.common.collect Maps newTreeMap.

Prototype

public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() 

Source Link

Document

Creates a mutable, empty TreeMap instance using the natural ordering of its elements.

Usage

From source file:com.yahoo.pulsar.broker.stats.BookieClientStatsGenerator.java

private void put(DestinationName destinationName, PendingBookieOpsStats bookieOpsStats) {
    String namespace = destinationName.getNamespace();
    if (!nsBookieClientStatsMap.containsKey(namespace)) {
        Map<String, PendingBookieOpsStats> destBookieClientStatsMap = Maps.newTreeMap();
        destBookieClientStatsMap.put(destinationName.toString(), bookieOpsStats);
        nsBookieClientStatsMap.put(namespace, destBookieClientStatsMap);
    } else {/*from   w  w  w.  j  ava2  s. co  m*/
        nsBookieClientStatsMap.get(namespace).put(destinationName.toString(), bookieOpsStats);
    }

}

From source file:com.pinterest.terrapin.zookeeper.ViewInfo.java

public ViewInfo(ExternalView externalView) {
    this.resource = externalView.getResourceName();
    Set<String> partitionSet = externalView.getPartitionSet();
    this.partitionMap = Maps.newTreeMap();
    for (String partition : partitionSet) {
        Map<String, String> stateMap = externalView.getStateMap(partition);
        Pair<String, Integer> resourceAndPartitionNum = TerrapinUtil.getResourceAndPartitionNum(partition);
        if (resourceAndPartitionNum == null) {
            LOG.warn("Invalid helix partition for " + resource + " : " + partition);
            continue;
        }/*  ww  w.j a va2 s .  co m*/
        if (stateMap == null) {
            continue;
        }
        List<String> instanceList = Lists.newArrayListWithCapacity(5);
        for (Map.Entry<String, String> entry : stateMap.entrySet()) {
            if (entry.getValue().equals("ONLINE")) {
                instanceList.add(entry.getKey());
            }
        }
        // Keep the list in alphabetical order.
        if (!instanceList.isEmpty()) {
            Collections.sort(instanceList);
            this.partitionMap.put(
                    TerrapinUtil.getViewPartitionName(resource, resourceAndPartitionNum.getRight()),
                    instanceList);
        }
    }
}

From source file:org.apache.nutch.api.impl.RAMConfManager.java

public Map<String, String> getAsMap(String confId) {
    Configuration configuration = configurations.get(confId);
    if (configuration == null) {
        return Collections.emptyMap();
    }/*from   w  w  w .  j  av  a  2 s.  c o m*/

    Iterator<Entry<String, String>> iterator = configuration.iterator();
    Map<String, String> configMap = Maps.newTreeMap();
    while (iterator.hasNext()) {
        Entry<String, String> entry = iterator.next();
        configMap.put(entry.getKey(), entry.getValue());
    }
    return configMap;
}

From source file:com.orange.clara.cloud.poc.s3.upload.UploadS3StreamImpl.java

@Override
public String upload(InputStream content, Blob blob) throws IOException {
    String bucketName = this.blobStoreContext.getSpringCloudBlobStore().getBucketName();
    String key = blob.getMetadata().getName();
    ContentMetadata metadata = blob.getMetadata().getContentMetadata();
    ObjectMetadataBuilder builder = ObjectMetadataBuilder.create().key(key)
            .contentType(MediaType.OCTET_STREAM.toString()).contentDisposition(key)
            .contentEncoding(metadata.getContentEncoding()).contentLanguage(metadata.getContentLanguage())
            .userMetadata(blob.getMetadata().getUserMetadata());
    String uploadId = this.s3Client.initiateMultipartUpload(bucketName, builder.build());
    Integer partNum = 1;// www  .  j a v  a  2s. c o  m
    Payload part = null;
    int bytesRead = 0;
    boolean shouldContinue = true;
    try {
        SortedMap<Integer, String> etags = Maps.newTreeMap();
        while (shouldContinue) {
            byte[] chunk = new byte[CHUNK_SIZE];
            bytesRead = ByteStreams.read(content, chunk, 0, chunk.length);
            if (bytesRead != chunk.length) {
                shouldContinue = false;
                chunk = Arrays.copyOf(chunk, bytesRead);
            }
            part = new ByteArrayPayload(chunk);
            prepareUploadPart(bucketName, key, uploadId, partNum, part, etags);
            partNum++;
        }
        return this.s3Client.completeMultipartUpload(bucketName, key, uploadId, etags);
    } catch (RuntimeException ex) {
        this.s3Client.abortMultipartUpload(bucketName, key, uploadId);
        throw ex;
    }
}

From source file:org.prebake.core.BoundName.java

/**
 * Converts a string like {@code "foo"} or {@code "bar[\"x\":\"y\"]"} into
 * a bound identifier.//  ww  w .  j  ava 2  s. c o m
 * <p>
 * This factory function has the signature that
 * {@link org.prebake.js.YSONConverter.Factory#withType} expects.
 */
public static BoundName fromString(String s) {
    int lbracket = s.indexOf('[');
    String ident;
    ImmutableMap<String, String> bindings;
    if (lbracket < 0) {
        ident = s;
        // Optimization for common case.
        bindings = ImmutableMap.of();
    } else {
        ident = s.substring(0, lbracket);
        JsonSource src = new JsonSource(new StringReader(s.substring(lbracket + 1)));
        try {
            if (src.check("]")) {
                bindings = ImmutableMap.of();
            } else {
                SortedMap<String, String> b = Maps.newTreeMap();
                do {
                    String key = src.expectString();
                    src.expect(":");
                    String value = src.expectString();
                    String old = b.put(key, value);
                    if (old != null && !old.equals(value)) {
                        throw new IllegalArgumentException("Duplicate binding " + JsonSink.stringify(key)
                                + " with values " + JsonSink.stringify(old) + " and "
                                + JsonSink.stringify(value) + " for " + s);
                    }
                } while (src.check(","));
                src.expect("]");
                bindings = ImmutableMap.copyOf(b);
            }
        } catch (IOException ex) {
            // Propagate syntax errors as IllegalArgumentExceptions
            throw new IllegalArgumentException(ex.getMessage(), ex);
        }
    }
    if (!YSON.isValidDottedIdentifierName(ident)) {
        throw new IllegalArgumentException(s + " is not a valid identifier");
    }
    return new BoundName(ident, bindings);
}

From source file:com.netflix.ice.processor.ReservationCapacityPoller.java

@Override
protected void poll() throws Exception {
    ProcessorConfig config = ProcessorConfig.getInstance();

    // read from s3 if not exists
    File file = new File(config.localDir, "reservation_capacity.txt");

    if (!file.exists()) {
        logger.info("downloading " + file + "...");
        AwsUtils.downloadFileIfNotExist(config.workS3BucketName, config.workS3BucketPrefix, file);
        logger.info("downloaded " + file);
    }/*from w w w .  j a va2  s.  co m*/

    // read from file
    Map<String, ReservedInstances> reservations = Maps.newTreeMap();
    if (file.exists()) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line;

            while ((line = reader.readLine()) != null) {
                String[] tokens = line.split(",");
                String accountId = tokens[0];
                String region = tokens[1];
                String reservationId = tokens[2];
                String zone = tokens[3];
                Long start = Long.parseLong(tokens[4]);
                long duration = Long.parseLong(tokens[5]);
                String instanceType = tokens[6];
                String productDescription = tokens[7];
                int instanceCount = Integer.parseInt(tokens[8]);
                String offeringType = tokens[9];
                String state = tokens[10];
                Long end = tokens.length > 11 ? Long.parseLong(tokens[11]) : null;
                float fixedPrice = tokens.length > 12 ? Float.parseFloat(tokens[12]) : 0;
                float usagePrice = tokens.length > 13 ? Float.parseFloat(tokens[13]) : 0;

                ReservedInstances reservation = new ReservedInstances().withAvailabilityZone(zone)
                        .withStart(new Date(start)).withDuration(duration).withInstanceType(instanceType)
                        .withProductDescription(productDescription).withInstanceCount(instanceCount)
                        .withOfferingType(offeringType).withState(state).withFixedPrice(fixedPrice)
                        .withUsagePrice(usagePrice);
                if (end != null)
                    reservation.setEnd(new Date(end));
                else
                    reservation.setEnd(new Date(start + duration * 1000));

                reservations.put(accountId + "," + region + "," + reservationId, reservation);
            }
        } catch (Exception e) {
            logger.error("error in reading " + file, e);
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (Exception e) {
                }
        }
    }
    logger.info("read " + reservations.size() + " reservations.");

    for (Account account : config.accountService.getReservationAccounts().keySet()) {
        try {
            AmazonEC2Client ec2Client;
            String assumeRole = config.accountService.getReservationAccessRoles().get(account);
            if (assumeRole != null) {
                String externalId = config.accountService.getReservationAccessExternalIds().get(account);
                final Credentials credentials = AwsUtils.getAssumedCredentials(account.id, assumeRole,
                        externalId);
                ec2Client = new AmazonEC2Client(new AWSSessionCredentials() {
                    public String getAWSAccessKeyId() {
                        return credentials.getAccessKeyId();
                    }

                    public String getAWSSecretKey() {
                        return credentials.getSecretAccessKey();
                    }

                    public String getSessionToken() {
                        return credentials.getSessionToken();
                    }
                });
            } else
                ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider.getCredentials(),
                        AwsUtils.clientConfig);

            for (Region region : Region.getAllRegions()) {

                ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com");

                try {
                    DescribeReservedInstancesResult result = ec2Client.describeReservedInstances();
                    for (ReservedInstances reservation : result.getReservedInstances()) {
                        String key = account.id + "," + region.name + ","
                                + reservation.getReservedInstancesId();
                        reservations.put(key, reservation);
                        if (reservation.getEnd() == null)
                            reservation.setEnd(new Date(
                                    reservation.getStart().getTime() + reservation.getDuration() * 1000L));
                        if (reservation.getFixedPrice() == null)
                            reservation.setFixedPrice(0f);
                        if (reservation.getUsagePrice() == null)
                            reservation.setUsagePrice(0f);
                    }
                } catch (Exception e) {
                    logger.error("error in describeReservedInstances for " + region.name + " " + account.name,
                            e);
                }
            }

            ec2Client.shutdown();
        } catch (Exception e) {
            logger.error("Error in describeReservedInstances for " + account.name, e);
        }
    }

    config.reservationService.updateEc2Reservations(reservations);
    updatedConfig = true;

    // archive to disk
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        for (String key : reservations.keySet()) {
            ReservedInstances reservation = reservations.get(key);
            String[] line = new String[] { key, reservation.getAvailabilityZone(),
                    reservation.getStart().getTime() + "", reservation.getDuration().toString(),
                    reservation.getInstanceType(), reservation.getProductDescription(),
                    reservation.getInstanceCount().toString(), reservation.getOfferingType(),
                    reservation.getState(), reservation.getEnd().getTime() + "",
                    reservation.getFixedPrice() + "", reservation.getUsagePrice() + "", };
            writer.write(StringUtils.join(line, ","));
            writer.newLine();
        }
    } catch (Exception e) {
        logger.error("", e);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (Exception e) {
            }
    }
    logger.info("archived " + reservations.size() + " reservations.");

    // archive to s3
    logger.info("uploading " + file + "...");
    AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, config.localDir, file.getName());
    logger.info("uploaded " + file);
}

From source file:org.elasticsearch.search.suggest.context.ContextBuilder.java

public static SortedMap<String, ContextMapping> loadMappings(Object configuration)
        throws ElasticsearchParseException {
    if (configuration instanceof Map) {
        Map<String, Object> configurations = (Map<String, Object>) configuration;
        SortedMap<String, ContextMapping> mappings = Maps.newTreeMap();
        for (Entry<String, Object> config : configurations.entrySet()) {
            String name = config.getKey();
            mappings.put(name, loadMapping(name, (Map<String, Object>) config.getValue()));
        }//  ww w .  ja v  a 2  s  .c om
        return mappings;
    } else if (configuration == null) {
        return ContextMapping.EMPTY_MAPPING;
    } else {
        throw new ElasticsearchParseException("no valid context configuration");
    }
}

From source file:things.thing.ThingUtils.java

public Map<String, Map<String, String>> getRegisteredTypeProperties() {

    if (typePropertiesMap == null) {
        Map<String, Map<String, String>> temp = Maps.newTreeMap();
        for (String type : tr.getAllTypes()) {
            Class typeClass = tr.getTypeClass(type);
            BeanInfo info = null;
            try {
                info = Introspector.getBeanInfo(typeClass);
            } catch (IntrospectionException e) {
                throw new TypeRuntimeException("Can't generate info for type: " + type, type, e);
            }/*  ww  w .j  ava 2  s . com*/

            Map<String, String> properties = Maps.newTreeMap();

            for (PropertyDescriptor desc : info.getPropertyDescriptors()) {
                String name = desc.getName();
                if ("class".equals(name) || "id".equals(name)) {
                    continue;
                }
                Class propClass = desc.getPropertyType();
                properties.put(name, propClass.getSimpleName());
            }
            temp.put(type, properties);
        }
        typePropertiesMap = ImmutableMap.copyOf(temp);
    }
    return typePropertiesMap;

}

From source file:io.druid.query.metadata.SegmentAnalyzer.java

public Map<String, ColumnAnalysis> analyze(QueryableIndex index,
        EnumSet<SegmentMetadataQuery.AnalysisType> analysisTypes) {
    Preconditions.checkNotNull(index, "Index cannot be null");

    Map<String, ColumnAnalysis> columns = Maps.newTreeMap();

    for (String columnName : index.getColumnNames()) {
        final Column column = index.getColumn(columnName);
        final ColumnCapabilities capabilities = column.getCapabilities();

        final ColumnAnalysis analysis;
        final ValueType type = capabilities.getType();
        switch (type) {
        case LONG:
            analysis = analyzeLongColumn(column, analysisTypes);
            break;
        case FLOAT:
            analysis = analyzeFloatColumn(column, analysisTypes);
            break;
        case STRING:
            analysis = analyzeStringColumn(column, analysisTypes);
            break;
        case COMPLEX:
            analysis = analyzeComplexColumn(column, analysisTypes);
            break;
        default:/*  w w  w . j  a  va  2  s  .c o  m*/
            log.warn("Unknown column type[%s].", type);
            analysis = ColumnAnalysis.error(String.format("unknown_type_%s", type));
        }

        columns.put(columnName, analysis);
    }

    columns.put(Column.TIME_COLUMN_NAME, lengthBasedAnalysis(index.getColumn(Column.TIME_COLUMN_NAME),
            NUM_BYTES_IN_TIMESTAMP, analysisTypes));

    return columns;
}

From source file:co.cask.cdap.data2.transaction.snapshot.SnapshotCodecV1.java

@Override
protected NavigableMap<Long, TransactionManager.InProgressTx> decodeInProgress(BinaryDecoder decoder)
        throws IOException {

    int size = decoder.readInt();
    NavigableMap<Long, TransactionManager.InProgressTx> inProgress = Maps.newTreeMap();
    while (size != 0) { // zero denotes end of list as per AVRO spec
        for (int remaining = size; remaining > 0; --remaining) {
            inProgress.put(decoder.readLong(),
                    // 1st version did not store visibilityUpperBound. It is safe to set firstInProgress to 0,
                    // it may decrease performance until this tx is finished, but correctness will be preserved.
                    new TransactionManager.InProgressTx(0L, decoder.readLong()));
        }/*from   w w w . j av  a  2s .  co m*/
        size = decoder.readInt();
    }
    return inProgress;
}