alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (SetMultimapAsMapTester.java)

This example Java source code file (SetMultimapAsMapTester.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractmultimaptester, collection, gwtcompatible, list, set, setmultimap, setmultimapasmaptester, util

The SetMultimapAsMapTester.java Java example source code

/*
 * Copyright (C) 2013 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.common.collect.testing.google;

import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Maps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import com.google.common.testing.EqualsTester;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Testers for {@link SetMultimap#asMap}.
 *
 * @author Louis Wasserman
 * @param <K> The key type of the tested multimap.
 * @param <V> The value type of the tested multimap.
 */
@GwtCompatible
public class SetMultimapAsMapTester<K, V> extends AbstractMultimapTester> {
  public void testAsMapValuesImplementSet() {
    for (Collection<V> valueCollection : multimap().asMap().values()) {
      assertTrue(valueCollection instanceof Set);
    }
  }

  public void testAsMapGetImplementsSet() {
    for (K key : multimap().keySet()) {
      assertTrue(multimap().asMap().get(key) instanceof Set);
    }
  }

  @MapFeature.Require(SUPPORTS_REMOVE)
  public void testAsMapRemoveImplementsSet() {
    List<K> keys = new ArrayList(multimap().keySet());
    for (K key : keys) {
      resetCollection();
      assertTrue(multimap().asMap().remove(key) instanceof Set);
    }
  }

  @CollectionSize.Require(SEVERAL)
  public void testEquals() {
    resetContainer(
        Helpers.mapEntry(k0(), v0()),
        Helpers.mapEntry(k1(), v0()),
        Helpers.mapEntry(k0(), v3()));
    Map<K, Collection expected = Maps.newHashMap();
    expected.put(k0(), Sets.newHashSet(v0(), v3()));
    expected.put(k1(), Sets.newHashSet(v0()));
    new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals();
  }

  @CollectionSize.Require(SEVERAL)
  public void testEntrySetEquals() {
    resetContainer(
        Helpers.mapEntry(k0(), v0()),
        Helpers.mapEntry(k1(), v0()),
        Helpers.mapEntry(k0(), v3()));
    Set<Entry> expected = Sets.newHashSet();
    expected.add(Helpers.mapEntry(k0(), (Collection<V>) Sets.newHashSet(v0(), v3())));
    expected.add(Helpers.mapEntry(k1(), (Collection<V>) Sets.newHashSet(v0())));
    new EqualsTester().addEqualityGroup(expected, multimap().asMap().entrySet()).testEquals();
  }

  @CollectionSize.Require(SEVERAL)
  @MapFeature.Require(SUPPORTS_REMOVE)
  public void testValuesRemove() {
    resetContainer(
        Helpers.mapEntry(k0(), v0()),
        Helpers.mapEntry(k1(), v0()),
        Helpers.mapEntry(k0(), v3()));
    assertTrue(multimap().asMap().values().remove(Collections.singleton(v0())));
    assertEquals(2, multimap().size());
    assertEquals(Collections.singletonMap(k0(), Sets.newHashSet(v0(), v3())), multimap().asMap());
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java SetMultimapAsMapTester.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.