All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.isi.nlp.serialization.jackson.ImmutableMultimapProxy Maven / Gradle / Ivy

The newest version!
package edu.isi.nlp.serialization.jackson;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import edu.isi.nlp.collections.MapUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Works around a Jackson issue in serializing multimaps in a similar fashion to {@link
 * ImmutableMapProxy}. To use this:
 *
 * 
    *
  1. Don't make the multimap itself a {@code JsonProperty} *
  2. Provide a {@code JsonProperty}-annotated package-private accessor which returns {@code * ImmutableMultimapProxy.forMultimap(myMap)} *
  3. In your {@code JsonCreator} method, take an {@code ImmutableMultimapProxy} as an argument * and call {@link #toImmutableMultimap()} *
* * @deprecated Prefer {@link MapEntries} */ @Deprecated public class ImmutableMultimapProxy { @JsonProperty("keys") private final List keys; @JsonProperty("values") private final List> values; private ImmutableMultimapProxy( @JsonProperty("keys") final List keys, @JsonProperty("values") final List> values) { this.keys = checkNotNull(keys); this.values = checkNotNull(values); checkArgument(keys.size() == values.size()); } @Deprecated @SuppressWarnings("deprecation") public static ImmutableMultimapProxy forMultimap(Multimap map) { final List keys = Lists.newArrayListWithCapacity(map.size()); final List> values = Lists.newArrayListWithCapacity(map.size()); for (final Map.Entry> e : map.asMap().entrySet()) { keys.add(e.getKey()); values.add(e.getValue()); } return new ImmutableMultimapProxy<>(keys, values); } public ImmutableMultimap toImmutableMultimap() { final ImmutableMap> map = MapUtils.copyParallelListsToMap(keys, values); final ImmutableMultimap.Builder ret = ImmutableMultimap.builder(); for (final Map.Entry> entry : map.entrySet()) { ret.putAll(entry.getKey(), entry.getValue()); } return ret.build(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy