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

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

Introduction

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

Prototype

@GwtIncompatible("NavigableMap")
public static <K, V1, V2> NavigableMap<K, V2> transformEntries(NavigableMap<K, V1> fromMap,
        EntryTransformer<? super K, ? super V1, V2> transformer) 

Source Link

Document

Returns a view of a navigable map whose values are derived from the original navigable map's entries.

Usage

From source file:org.ow2.sirocco.cloudmanager.api.openstack.server.utils.MapHelper.java

/**
 * Replace the values of the orignal map entries with the replace ones, add missing ones and remove the ones which are not defined in the replace map.
 *
 * @param original/*from w  ww.  ja v  a 2s.  c om*/
 * @param replace
 * @return
 */
public static Map<String, String> replaceMap(final Map<String, String> original,
        final Map<String, String> replace) {
    if (replace == null || replace.size() == 0) {
        return Maps.newHashMap();
    }

    // first, remove entries which are not defined in the replace map
    Map<String, String> filter = Maps.filterEntries(original, new Predicate<Map.Entry<String, String>>() {
        @Override
        public boolean apply(java.util.Map.Entry<String, String> input) {
            return replace.get(input.getKey()) != null;
        }
    });

    // then update the values of the filtered map
    Map<String, String> updated = Maps.transformEntries(filter,
            new Maps.EntryTransformer<String, String, String>() {
                @Override
                public String transformEntry(String key, String value) {
                    if (replace.get(key) != null) {
                        return replace.get(key);
                    } else {
                        return value;
                    }
                }
            });

    // then add the entries from the replace map which are not in the updated map
    Map<String, String> result = Maps.newHashMap(updated);
    result.putAll(Maps.difference(updated, replace).entriesOnlyOnRight());
    return result;
}

From source file:org.gradle.internal.execution.impl.OutputFilterUtil.java

public static ImmutableSortedMap<String, CurrentFileCollectionFingerprint> filterOutputFingerprints(
        final @Nullable ImmutableSortedMap<String, FileCollectionFingerprint> outputsAfterPreviousExecution,
        ImmutableSortedMap<String, CurrentFileCollectionFingerprint> outputsBeforeExecution,
        final ImmutableSortedMap<String, CurrentFileCollectionFingerprint> outputsAfterExecution) {
    return ImmutableSortedMap.copyOfSorted(Maps.transformEntries(outputsBeforeExecution,
            new Maps.EntryTransformer<String, CurrentFileCollectionFingerprint, CurrentFileCollectionFingerprint>() {
                @Override//from w  w  w.  java 2  s  .  co m
                @SuppressWarnings("NullableProblems")
                public CurrentFileCollectionFingerprint transformEntry(String propertyName,
                        CurrentFileCollectionFingerprint outputBeforeExecution) {
                    CurrentFileCollectionFingerprint outputAfterExecution = outputsAfterExecution
                            .get(propertyName);
                    FileCollectionFingerprint outputAfterPreviousExecution = getFingerprintForProperty(
                            outputsAfterPreviousExecution, propertyName);
                    return filterOutputFingerprint(outputAfterPreviousExecution, outputBeforeExecution,
                            outputAfterExecution);
                }
            }));
}

From source file:com.palantir.atlasdb.transaction.impl.SweepStrategyManagers.java

private static Map<String, SweepStrategy> getSweepStrategies(KeyValueService kvs) {
    return ImmutableMap.copyOf(Maps.transformEntries(kvs.getMetadataForTables(),
            new EntryTransformer<String, byte[], SweepStrategy>() {
                @Override/*from w ww  .  j  a  v a 2 s. co  m*/
                public SweepStrategy transformEntry(String tableName, byte[] tableMetadata) {
                    if (tableMetadata != null && tableMetadata.length > 0) {
                        return TableMetadata.BYTES_HYDRATOR.hydrateFromBytes(tableMetadata).getSweepStrategy();
                    } else {
                        return SweepStrategy.CONSERVATIVE;
                    }
                }
            }));
}

From source file:org.gradle.api.internal.changedetection.changes.Util.java

public static ImmutableSortedMap<String, CurrentFileCollectionFingerprint> fingerprintAfterOutputsGenerated(
        final @Nullable ImmutableSortedMap<String, FileCollectionFingerprint> previous,
        ImmutableSortedMap<String, CurrentFileCollectionFingerprint> current,
        SortedSet<? extends TaskFilePropertySpec> outputProperties, boolean hasOverlappingOutputs,
        TaskInternal task, FileCollectionFingerprinterRegistry fingerprinterRegistry) {
    final ImmutableSortedMap<String, CurrentFileCollectionFingerprint> outputFilesAfter = fingerprintTaskFiles(
            task, outputProperties, fingerprinterRegistry);

    if (!hasOverlappingOutputs) {
        return outputFilesAfter;
    } else {//from  w ww  . j a v a  2s .  com
        return ImmutableSortedMap.copyOfSorted(Maps.transformEntries(current,
                new Maps.EntryTransformer<String, CurrentFileCollectionFingerprint, CurrentFileCollectionFingerprint>() {
                    @Override
                    @SuppressWarnings("NullableProblems")
                    public CurrentFileCollectionFingerprint transformEntry(String propertyName,
                            CurrentFileCollectionFingerprint beforeExecution) {
                        CurrentFileCollectionFingerprint afterExecution = outputFilesAfter.get(propertyName);
                        FileCollectionFingerprint afterPreviousExecution = Util
                                .getFingerprintAfterPreviousExecution(previous, propertyName);
                        return Util.filterOutputFingerprint(afterPreviousExecution, beforeExecution,
                                afterExecution);
                    }
                }));
    }
}

From source file:com.palantir.atlasdb.keyvalue.impl.RowResults.java

public static <T> SortedMap<byte[], RowResult<T>> viewOfSortedMap(SortedMap<byte[], SortedMap<byte[], T>> map) {
    return Maps.transformEntries(map, new Maps.EntryTransformer<byte[], SortedMap<byte[], T>, RowResult<T>>() {
        @Override/*  w w  w  .  j a  v a 2  s.co  m*/
        public RowResult<T> transformEntry(byte[] key, SortedMap<byte[], T> value) {
            return RowResult.create(key, value);
        }
    });
}

From source file:com.lexicalscope.fluent.map.FluentMap.java

public <W> FluentMap<K, W> $convertValues(final Converter<V, W> converter) {
    return $(Maps.transformEntries(delegate(), new Maps.EntryTransformer<K, V, W>() {
        @Override//from   www .j  ava2 s.c o  m
        public W transformEntry(final K key, final V value) {
            return converter.convert(value);
        }
    }));
}

From source file:google.registry.model.billing.RegistrarBillingUtils.java

/** Returns amount of money registrar currently owes registry in each currency. */
public static Map<CurrencyUnit, Money> loadBalance(Registrar registrar) {
    return Maps.transformEntries(getBillingEntryQueries(registrar),
            new EntryTransformer<CurrencyUnit, Query<RegistrarBillingEntry>, Money>() {
                @Override/*from  www . j  a v a  2  s  . c o  m*/
                public Money transformEntry(CurrencyUnit currency, Query<RegistrarBillingEntry> query) {
                    RegistrarBillingEntry entry = query.first().now();
                    return entry != null ? entry.getBalance() : Money.zero(currency);
                }
            });
}

From source file:com.datatorrent.contrib.kafka.KafkaMetadataUtil.java

/**
 * @param brokers in multiple clusters, keyed by cluster id
 * @param topic//from w w  w.j  a  v a  2  s.  c  om
 * @return Get the partition metadata list for the specific topic via the brokers
 * null if topic is not found
 */
public static Map<String, List<PartitionMetadata>> getPartitionsForTopic(SetMultimap<String, String> brokers,
        final String topic) {
    return Maps.transformEntries(brokers.asMap(),
            new EntryTransformer<String, Collection<String>, List<PartitionMetadata>>() {
                @Override
                public List<PartitionMetadata> transformEntry(String key, Collection<String> bs) {
                    return getPartitionsForTopic(new HashSet<String>(bs), topic);
                }
            });
}

From source file:eu.esdihumboldt.hale.io.wfs.ui.KVPUtil.java

/**
 * Add typename and namespace parameters for a WFS request to the given URI
 * builder.//  w w  w.  jav a2s  .  c o m
 * 
 * @param builder the builder for the WFS request URI
 * @param selected the type names to include
 * @param version the targeted WFS version
 */
public static void addTypeNameParameter(URIBuilder builder, Iterable<QName> selected, WFSVersion version) {
    // namespaces mapped to prefixes
    Map<String, String> namespaces = new HashMap<>();
    // type names with updated prefix
    Set<QName> typeNames = new HashSet<>();

    for (QName type : selected) {
        String prefix;
        if (type.getNamespaceURI() != null && !type.getNamespaceURI().isEmpty()) {
            prefix = namespaces.get(type.getNamespaceURI());
            if (prefix == null) {
                // no mapping yet for namespace
                String candidate = type.getPrefix();
                prefix = addPrefix(candidate, type.getNamespaceURI(), namespaces);
            }
        } else {
            // default namespace
            prefix = XMLConstants.DEFAULT_NS_PREFIX;
        }

        // add updated type
        typeNames.add(new QName(type.getNamespaceURI(), type.getLocalPart(), prefix));
    }

    final String paramNamespaces;
    final String paramTypenames;
    final String prefixNamespaceDelim;
    switch (version) {
    case V1_1_0:
        paramNamespaces = "NAMESPACE";
        paramTypenames = "TYPENAME";
        prefixNamespaceDelim = "=";
        break;
    case V2_0_0:
    case V2_0_2:
        /*
         * XXX below are the values as defined in the WFS 2 specification.
         * There have been problems with some GeoServer instances if used in
         * that manner.
         */
        paramNamespaces = "NAMESPACES";
        paramTypenames = "TYPENAMES";
        prefixNamespaceDelim = ",";
        break;
    default:
        // fall-back to WFS 1.1
        paramNamespaces = "NAMESPACE";
        paramTypenames = "TYPENAME";
        prefixNamespaceDelim = "=";
    }

    // add namespace prefix definitions
    if (!namespaces.isEmpty()) {
        builder.addParameter(paramNamespaces, Joiner.on(',')
                .join(Maps.transformEntries(namespaces, new EntryTransformer<String, String, String>() {

                    @Override
                    public String transformEntry(String namespace, String prefix) {
                        StringBuilder sb = new StringBuilder();
                        sb.append("xmlns(");
                        sb.append(prefix);
                        sb.append(prefixNamespaceDelim);
                        sb.append(namespace);
                        sb.append(")");
                        return sb.toString();
                    }

                }).values()));
    }
    // add type names
    if (!typeNames.isEmpty()) {
        builder.addParameter(paramTypenames,
                Joiner.on(',').join(Iterables.transform(typeNames, new Function<QName, String>() {

                    @Override
                    public String apply(QName typeName) {
                        String prefix = typeName.getPrefix();
                        if (prefix == null || prefix.isEmpty()) {
                            return typeName.getLocalPart();
                        }
                        return prefix + ":" + typeName.getLocalPart();
                    }
                })));
    }
}

From source file:org.apache.oozie.util.PasswordMasker.java

Map<String, String> mask(Map<String, String> unmasked) {
    return Maps.transformEntries(unmasked, new Maps.EntryTransformer<String, String, String>() {
        @Override//w  w w  .  j av  a 2  s. c om
        public String transformEntry(@Nullable String key, @Nullable String value) {
            checkNotNull(key, "key has to be set");
            checkNotNull(value, "value has to be set");

            if (isPasswordKey(key)) {
                return PASSWORD_MASK;
            }

            if (containsPasswordFragment(value)) {
                return maskPasswordFragments(value);
            }

            return value;
        }
    });
}