Example usage for com.google.common.collect ImmutableSortedMap naturalOrder

List of usage examples for com.google.common.collect ImmutableSortedMap naturalOrder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSortedMap naturalOrder.

Prototype

public static <K extends Comparable<?>, V> Builder<K, V> naturalOrder() 

Source Link

Usage

From source file:ec.tss.tsproviders.utils.ParamBean.java

@Nonnull
public static ImmutableSortedMap<String, String> toSortedMap(@Nullable ParamBean[] params) {
    if (params == null || params.length == 0) {
        return ImmutableSortedMap.of();
    }/*from www  . j a va  2s .c  om*/
    ImmutableSortedMap.Builder<String, String> b = ImmutableSortedMap.naturalOrder();
    for (ParamBean o : params) {
        b.put(Strings.nullToEmpty(o.key), Strings.nullToEmpty(o.value));
    }
    return b.build();
}

From source file:com.facebook.buck.json.JsonObjectHashing.java

/**
 * Given a {@link Hasher} and a parsed BUCK file object, updates the Hasher
 * with the contents of the JSON object.
 *///  w  w w .j  av  a  2  s .c  o m
public static void hashJsonObject(Hasher hasher, @Nullable Object obj) {
    if (obj instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) obj;
        ImmutableSortedMap.Builder<String, Optional<Object>> sortedMapBuilder = ImmutableSortedMap
                .naturalOrder();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            Object key = entry.getKey();
            if (!(key instanceof String)) {
                throw new RuntimeException(String.format(
                        "Keys of JSON maps are expected to be strings. Actual type: %s, contents: %s",
                        key.getClass().getName(), key));
            }
            Object value = entry.getValue();
            if (value != null) {
                sortedMapBuilder.put((String) key, Optional.of(value));
            }
        }
        ImmutableSortedMap<String, Optional<Object>> sortedMap = sortedMapBuilder.build();
        hasher.putInt(HashedObjectType.MAP.ordinal());
        hasher.putInt(sortedMap.size());
        for (Map.Entry<String, Optional<Object>> entry : sortedMap.entrySet()) {
            hashJsonObject(hasher, entry.getKey());
            if (entry.getValue().isPresent()) {
                hashJsonObject(hasher, entry.getValue().get());
            } else {
                hashJsonObject(hasher, null);
            }
        }
    } else if (obj instanceof Collection) {
        Collection<?> collection = (Collection<?>) obj;
        hasher.putInt(HashedObjectType.LIST.ordinal());
        hasher.putInt(collection.size());
        for (Object collectionEntry : collection) {
            hashJsonObject(hasher, collectionEntry);
        }
    } else if (obj instanceof String) {
        hasher.putInt(HashedObjectType.STRING.ordinal());
        byte[] stringBytes = ((String) obj).getBytes(Charsets.UTF_8);
        hasher.putInt(stringBytes.length);
        hasher.putBytes(stringBytes);
    } else if (obj instanceof Boolean) {
        hasher.putInt(HashedObjectType.BOOLEAN.ordinal());
        hasher.putBoolean((boolean) obj);
    } else if (obj instanceof Number) {
        // This is gross, but it mimics the logic originally in RawParser.
        Number number = (Number) obj;
        if (number.longValue() == number.doubleValue()) {
            hasher.putInt(HashedObjectType.LONG.ordinal());
            hasher.putLong(number.longValue());
        } else {
            hasher.putInt(HashedObjectType.DOUBLE.ordinal());
            hasher.putDouble(number.doubleValue());
        }
    } else if (obj instanceof Void || obj == null) {
        hasher.putInt(HashedObjectType.NULL.ordinal());
    } else {
        throw new RuntimeException(String.format("Unsupported object %s (class %s)", obj, obj.getClass()));
    }
}

From source file:com.google.caliper.runner.ParameterSet.java

public static ParameterSet create(Class<?> theClass, Class<? extends Annotation> annotationClass)
        throws InvalidBenchmarkException {
    // deterministic order, not reflection order
    ImmutableMap.Builder<String, Parameter> parametersBuilder = ImmutableSortedMap.naturalOrder();

    for (Field field : theClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(annotationClass)) {
            Parameter parameter = Parameter.create(field);
            parametersBuilder.put(field.getName(), parameter);
        }//  w  w  w. j  a va 2s  . c o  m
    }
    return new ParameterSet(parametersBuilder.build());
}

From source file:com.facebook.presto.metadata.MetadataListing.java

public static SortedMap<String, ConnectorId> listCatalogs(Session session, Metadata metadata,
        AccessControl accessControl) {/*from w w  w .  j  a  v  a 2 s. co  m*/
    Map<String, ConnectorId> catalogNames = metadata.getCatalogNames(session);
    Set<String> allowedCatalogs = accessControl.filterCatalogs(session.getIdentity(), catalogNames.keySet());

    ImmutableSortedMap.Builder<String, ConnectorId> result = ImmutableSortedMap.naturalOrder();
    for (Map.Entry<String, ConnectorId> entry : catalogNames.entrySet()) {
        if (allowedCatalogs.contains(entry.getKey())) {
            result.put(entry);
        }
    }
    return result.build();
}

From source file:com.publictransitanalytics.scoregenerator.schedule.Trip.java

private static List<VehicleEvent> getSequence(final Set<ScheduleEntry> stops,
        final ScheduleInterpolator interpolator) {
    final ImmutableSortedMap.Builder<Integer, VehicleEvent> sequenceBuilder = ImmutableSortedMap.naturalOrder();

    for (final ScheduleEntry entry : stops) {
        final Optional<LocalDateTime> optionalTime = entry.getTime();
        final LocalDateTime time;
        if (optionalTime.isPresent()) {
            time = optionalTime.get();/*  ww  w  .  j av a 2 s .com*/
            interpolator.setBaseTime(time);
        } else {
            time = interpolator.getInterpolatedTime(entry.getStop());
        }

        final VehicleEvent scheduledLocation = new VehicleEvent(entry.getStop(), time);
        final int sequenceNumber = entry.getSequence();
        sequenceBuilder.put(sequenceNumber, scheduledLocation);
    }
    final List<VehicleEvent> sequence = ImmutableList.copyOf(sequenceBuilder.build().values());
    return sequence;
}

From source file:dk.ilios.spanner.benchmark.ParameterSet.java

/**
 * Returns the set of all parameters and their possible values.
 * @param benchmarkClass    Benchmark class.
 * @param annotationClass   Annotation specifying a parameter.
 * @return/*from  w  w w .j  a v  a 2  s  .  co m*/
 * @throws InvalidBenchmarkException
 */
public static ParameterSet create(Class<?> benchmarkClass, Class<? extends Annotation> annotationClass)
        throws InvalidBenchmarkException {
    // deterministic order, not reflection order
    ImmutableMap.Builder<String, Parameter> parametersBuilder = ImmutableSortedMap.naturalOrder();

    for (Field field : benchmarkClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(annotationClass)) {
            Parameter parameter = Parameter.create(field);
            parametersBuilder.put(field.getName(), parameter);
        }
    }
    return new ParameterSet(parametersBuilder.build());
}

From source file:org.gradle.model.internal.manage.schema.ModelSchema.java

public ModelSchema(Class<T> type, Iterable<ModelProperty<?>> properties) {
    this.type = type;
    ImmutableSortedMap.Builder<String, ModelProperty<?>> builder = ImmutableSortedMap.naturalOrder();
    for (ModelProperty<?> property : properties) {
        builder.put(property.getName(), property);
    }//from ww  w . ja  v a2 s.co  m

    this.properties = builder.build();
}

From source file:org.gradle.model.internal.manage.schema.ModelImplTypeSchema.java

public ModelImplTypeSchema(ModelType<T> type, Kind kind, Iterable<ModelProperty<?>> properties) {
    super(type, kind);
    ImmutableSortedMap.Builder<String, ModelProperty<?>> builder = ImmutableSortedMap.naturalOrder();
    for (ModelProperty<?> property : properties) {
        builder.put(property.getName(), property);
    }//from   w w  w .  j a  v a 2  s  . co m
    this.properties = builder.build();
}

From source file:org.gradle.model.internal.manage.state.ManagedModelElement.java

public ManagedModelElement(ModelSchema<T> schema) {
    this.type = schema.getType();
    ImmutableSortedMap.Builder<String, ModelPropertyInstance<?>> builder = ImmutableSortedMap.naturalOrder();
    for (ModelProperty<?> property : schema.getProperties().values()) {
        builder.put(property.getName(), ModelPropertyInstance.of(property));
    }//from   w  ww.j  a  v  a  2s .  com
    this.properties = builder.build();
}

From source file:org.gradle.model.internal.manage.schema.ModelStructSchema.java

public ModelStructSchema(ModelType<T> type, Iterable<ModelProperty<?>> properties,
        Class<? extends T> managedImpl) {
    super(type, Kind.STRUCT);
    ImmutableSortedMap.Builder<String, ModelProperty<?>> builder = ImmutableSortedMap.naturalOrder();
    for (ModelProperty<?> property : properties) {
        builder.put(property.getName(), property);
    }/*  w w w. java2 s . c  om*/
    this.properties = builder.build();
    this.managedImpl = new WeakReference<Class<? extends T>>(managedImpl);
}