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

/**
 * Updates the orignal map with the values of the updates map. It will not add any entry to the original map.
 *
 * @param original// w  w  w. ja v a  2 s. com
 * @param updates
 * @return
 */
public static Map<String, String> updateMap(final Map<String, String> original,
        final Map<String, String> updates) {
    if (updates == null) {
        return original;
    }
    return Maps.transformEntries(original, new Maps.EntryTransformer<String, String, String>() {
        @Override
        public String transformEntry(String key, String value) {

            if (updates.get(key) != null) {
                return updates.get(key);
            } else {
                return value;
            }
        }
    });
}

From source file:com.spectralogic.ds3client.helpers.JobPartTrackerFactory.java

public static JobPartTracker buildPartTracker(final Iterable<BulkObject> objects) {
    final ArrayListMultimap<String, ObjectPart> multimap = ArrayListMultimap.create();
    for (final BulkObject bulkObject : Preconditions.checkNotNull(objects)) {
        multimap.put(bulkObject.getName(), new ObjectPart(bulkObject.getOffset(), bulkObject.getLength()));
    }/*from ww w.ja  va 2  s  . c  o  m*/
    return new JobPartTrackerImpl(new HashMap<>(
            Maps.transformEntries(multimap.asMap(), new BuildObjectPartTrackerFromObjectPartGroup())));
}

From source file:io.druid.firehose.rabbitmq.JacksonifiedConnectionFactory.java

private static Map<String, Object> getSerializableClientProperties(final Map<String, Object> clientProperties) {
    return Maps.transformEntries(clientProperties, new Maps.EntryTransformer<String, Object, Object>() {
        @Override//from w w w  .j  ava 2s  .com
        public Object transformEntry(String key, Object value) {
            if (value instanceof LongString) {
                return value.toString();
            }
            return value;
        }
    });
}

From source file:com.github.tomakehurst.wiremock.common.Urls.java

public static Map<String, QueryParameter> splitQuery(String query) {
    if (query == null) {
        return Collections.emptyMap();
    }//from   w  w w.  ja  va  2s. c  om

    Iterable<String> pairs = Splitter.on('&').split(query);
    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
    for (String queryElement : pairs) {
        int firstEqualsIndex = queryElement.indexOf('=');
        if (firstEqualsIndex == -1) {
            builder.putAll(queryElement, "");
        } else {
            String key = queryElement.substring(0, firstEqualsIndex);
            String value = decode(queryElement.substring(firstEqualsIndex + 1));
            builder.putAll(key, value);
        }
    }

    return Maps.transformEntries(builder.build().asMap(),
            new Maps.EntryTransformer<String, Collection<String>, QueryParameter>() {
                public QueryParameter transformEntry(String key, Collection<String> values) {
                    return new QueryParameter(key, ImmutableList.copyOf(values));
                }
            });
}

From source file:co.freeside.betamax.util.MultimapUtils.java

/**
 * Flattens a `Multimap` whose values are strings into a regular `Map`
 * whose/*from w w w .  java 2s .c o m*/
 * values are comma-separated strings.
 *
 * For example `{"a": ["foo", "bar"], "b": "baz"}` transforms to `{"a":
 * "foo,bar", "b": "baz"}`.
 */
public static Map<String, String> flatten(Multimap<String, String> multimap, String separator) {
    return Maps.transformEntries(multimap.asMap(), new JoinTransformer(separator));
}

From source file:co.freeside.betamax.util.MultimapUtils.java

public static Map<String, Collection<String>> unflatten(Map<String, String> map, String separator) {
    return Maps.transformEntries(map, new SplitTransformer(separator));
}

From source file:org.apache.isis.security.shiro.permrolemapper.PermissionToRoleMapperFromIni.java

/**
 * Using the same logic as in {@link IniRealm}.
 *///from   w w  w  .j a  va 2s . co  m
public PermissionToRoleMapperFromIni(Ini ini) {
    Map<String, String> section = ini.getSection(IniRealm.ROLES_SECTION_NAME);
    this.permissionsByRole = Maps.transformEntries(section,
            new EntryTransformer<String, String, List<String>>() {

                @Override
                public List<String> transformEntry(String key, String value) {
                    return Lists.newArrayList(PermissionUtils.toPermissionStrings(value));
                }
            });
}

From source file:com.prealpha.minelib.nbt.CompoundTag.java

@Override
public ByteBuffer toBytes() {
    // make copies to avoid encoding twice
    Map<String, ByteBuffer> nameBytes = ImmutableMap
            .copyOf(Maps.transformEntries(value, new EntryTransformer<String, Tag, ByteBuffer>() {
                @Override//from ww  w  .j av a2 s. c o m
                public ByteBuffer transformEntry(String key, Tag value) {
                    return new StringTag(key).toBytes();
                }
            }));
    Map<String, ByteBuffer> payloadBytes = ImmutableMap.copyOf(Maps.transformValues(value, Tag.ENCODER));

    int length = 1; // for the null terminating byte
    for (ByteBuffer buffer : nameBytes.values()) {
        length += buffer.capacity() + 1; // add one for the tagType
    }
    for (ByteBuffer buffer : payloadBytes.values()) {
        length += buffer.capacity();
    }

    ByteBuffer payload = ByteBuffer.allocate(length);
    for (String name : value.keySet()) {
        payload.put(value.get(name).getTagType().getBinaryType());
        payload.put(nameBytes.get(name));
        payload.put(payloadBytes.get(name));
    }
    payload.put((byte) 0x00);
    payload.position(0);
    return payload;
}

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

public <W> FluentMap<K, W> $convert(final Converter<Entry<K, V>, W> converter) {
    return $(Maps.transformEntries(delegate(), new Maps.EntryTransformer<K, V, W>() {
        @Override/*from   ww  w  .  j  a v  a2s.c  o m*/
        public W transformEntry(final K key, final V value) {
            return converter.convert(new Map.Entry<K, V>() {
                @Override
                public K getKey() {
                    return key;
                }

                @Override
                public V getValue() {
                    return value;
                }

                @Override
                public V setValue(final V value) {
                    throw new UnsupportedOperationException();
                }
            });
        }
    }));
}

From source file:org.opendaylight.centinel.alertcallback.CentinelAlertCallback.java

@Override
public Map<String, Object> getAttributes() {
    return Maps.transformEntries(configuration.getSource(),
            new Maps.EntryTransformer<String, Object, Object>() {
                @Override/* w w  w  . j a va 2 s .  c o  m*/
                public Object transformEntry(String key, Object value) {
                    return value;
                }
            });
}