Skip to content

Commit

Permalink
Add @CheckReturnValue to Multimap and Table. This forces the caller o…
Browse files Browse the repository at this point in the history
…f these APIs to "do something" with the result of the call.

For more information, see []
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=115771812
  • Loading branch information
kluever authored and cpovirk committed Feb 29, 2016
1 parent 012d8f4 commit c56fa61
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.google.common.collect;

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

/**
* Unit test for {@link ForwardingMultimap}.
Expand Down Expand Up @@ -46,27 +50,27 @@ public class ForwardingMultimapTest extends ForwardingTestCase {
}

public void testSize() {
forward.size();
int unused = forward.size();
assertEquals("[size]", getCalls());
}

public void testIsEmpty() {
forward.isEmpty();
boolean unused = forward.isEmpty();
assertEquals("[isEmpty]", getCalls());
}

public void testContainsKey_Object() {
forward.containsKey("asdf");
boolean unused = forward.containsKey("asdf");
assertEquals("[containsKey(Object)]", getCalls());
}

public void testContainsValue_Object() {
forward.containsValue("asdf");
boolean unused = forward.containsValue("asdf");
assertEquals("[containsValue(Object)]", getCalls());
}

public void testContainsEntry_Object_Object() {
forward.containsEntry("asdf", false);
boolean unused = forward.containsEntry("asdf", false);
assertEquals("[containsEntry(Object,Object)]", getCalls());
}

Expand Down Expand Up @@ -106,47 +110,47 @@ public void testClear() {
}

public void testGet_Key() {
forward.get(null);
Collection<Boolean> unused = forward.get(null);
assertEquals("[get(Object)]", getCalls());
}

public void testKeySet() {
forward.keySet();
Set<String> unused = forward.keySet();
assertEquals("[keySet]", getCalls());
}

public void testKeys() {
forward.keys();
Multiset<String> unused = forward.keys();
assertEquals("[keys]", getCalls());
}

public void testValues() {
forward.values();
Collection<Boolean> unused = forward.values();
assertEquals("[values]", getCalls());
}

public void testEntries() {
forward.entries();
Collection<Entry<String, Boolean>> unused = forward.entries();
assertEquals("[entries]", getCalls());
}

public void testAsMap() {
forward.asMap();
Map<String, Collection<Boolean>> unused = forward.asMap();
assertEquals("[asMap]", getCalls());
}

public void testEquals() {
forward.equals(null);
boolean unused = forward.equals(null);
assertEquals("[equals(Object)]", getCalls());
}

public void testHashCode() {
forward.hashCode();
int unused = forward.hashCode();
assertEquals("[hashCode]", getCalls());
}

public void testToString() {
forward.toString();
String unused = forward.toString();
assertEquals("[toString]", getCalls());
}
}
40 changes: 23 additions & 17 deletions guava-tests/test/com/google/common/collect/ForwardingTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

package com.google.common.collect;

import com.google.common.collect.Table.Cell;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
* Tests {@link ForwardingTable}.
*
Expand Down Expand Up @@ -43,12 +49,12 @@ public class ForwardingTableTest extends ForwardingTestCase {
}

public void testHashCode() {
forward.hashCode();
int unused = forward.hashCode();
assertEquals("[hashCode]", getCalls());
}

public void testCellSet() {
forward.cellSet();
Set<Cell<String, Integer, Boolean>> unused = forward.cellSet();
assertEquals("[cellSet]", getCalls());
}

Expand All @@ -58,47 +64,47 @@ public void testClear() {
}

public void testColumn() {
forward.column(1);
Map<String, Boolean> unused = forward.column(1);
assertEquals("[column(Object)]", getCalls());
}

public void testColumnKeySet() {
forward.columnKeySet();
Set<Integer> unused = forward.columnKeySet();
assertEquals("[columnKeySet]", getCalls());
}

public void testColumnMap() {
forward.columnMap();
Map<Integer, Map<String, Boolean>> unused = forward.columnMap();
assertEquals("[columnMap]", getCalls());
}

public void testContains() {
forward.contains("blah", 1);
boolean unused = forward.contains("blah", 1);
assertEquals("[contains(Object,Object)]", getCalls());
}

public void testContainsColumn() {
forward.containsColumn(1);
boolean unused = forward.containsColumn(1);
assertEquals("[containsColumn(Object)]", getCalls());
}

public void testContainsRow() {
forward.containsRow("blah");
boolean unused = forward.containsRow("blah");
assertEquals("[containsRow(Object)]", getCalls());
}

public void testContainsValue() {
forward.containsValue(false);
boolean unused = forward.containsValue(false);
assertEquals("[containsValue(Object)]", getCalls());
}

public void testGet() {
forward.get("blah", 1);
Boolean unused = forward.get("blah", 1);
assertEquals("[get(Object,Object)]", getCalls());
}

public void testIsEmpty() {
forward.isEmpty();
boolean unused = forward.isEmpty();
assertEquals("[isEmpty]", getCalls());
}

Expand All @@ -118,32 +124,32 @@ public void testRemove() {
}

public void testRow() {
forward.row("String");
Map<Integer, Boolean> unused = forward.row("String");
assertEquals("[row(Object)]", getCalls());
}

public void testRowKeySet() {
forward.rowKeySet();
Set<String> unused = forward.rowKeySet();
assertEquals("[rowKeySet]", getCalls());
}

public void testRowMap() {
forward.rowMap();
Map<String, Map<Integer, Boolean>> unused = forward.rowMap();
assertEquals("[rowMap]", getCalls());
}

public void testSize() {
forward.size();
int unused = forward.size();
assertEquals("[size]", getCalls());
}

public void testValues() {
forward.values();
Collection<Boolean> unused = forward.values();
assertEquals("[values]", getCalls());
}

public void testEqualsObject() {
forward.equals(null);
boolean unused = forward.equals(null);
assertEquals("[equals(Object)]", getCalls());
}

Expand Down
9 changes: 9 additions & 0 deletions guava/src/com/google/common/collect/ForwardingMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
Expand All @@ -34,6 +36,7 @@
* @author Robert Konigsberg
* @since 2.0
*/
@CheckReturnValue
@GwtCompatible
public abstract class ForwardingMultimap<K, V> extends ForwardingObject implements Multimap<K, V> {

Expand Down Expand Up @@ -93,31 +96,37 @@ public Set<K> keySet() {
return delegate().keySet();
}

@CanIgnoreReturnValue
@Override
public boolean put(K key, V value) {
return delegate().put(key, value);
}

@CanIgnoreReturnValue
@Override
public boolean putAll(K key, Iterable<? extends V> values) {
return delegate().putAll(key, values);
}

@CanIgnoreReturnValue
@Override
public boolean putAll(Multimap<? extends K, ? extends V> multimap) {
return delegate().putAll(multimap);
}

@CanIgnoreReturnValue
@Override
public boolean remove(@Nullable Object key, @Nullable Object value) {
return delegate().remove(key, value);
}

@CanIgnoreReturnValue
@Override
public Collection<V> removeAll(@Nullable Object key) {
return delegate().removeAll(key);
}

@CanIgnoreReturnValue
@Override
public Collection<V> replaceValues(K key, Iterable<? extends V> values) {
return delegate().replaceValues(key, values);
Expand Down
6 changes: 6 additions & 0 deletions guava/src/com/google/common/collect/ForwardingTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import javax.annotation.CheckReturnValue;

/**
* A table which forwards all its method calls to another table. Subclasses
* should override one or more methods to modify the behavior of the backing
Expand All @@ -31,6 +34,7 @@
* @author Gregory Kick
* @since 7.0
*/
@CheckReturnValue
@GwtCompatible
public abstract class ForwardingTable<R, C, V> extends ForwardingObject implements Table<R, C, V> {
/** Constructor for use by subclasses. */
Expand Down Expand Up @@ -94,6 +98,7 @@ public boolean isEmpty() {
return delegate().isEmpty();
}

@CanIgnoreReturnValue
@Override
public V put(R rowKey, C columnKey, V value) {
return delegate().put(rowKey, columnKey, value);
Expand All @@ -104,6 +109,7 @@ public void putAll(Table<? extends R, ? extends C, ? extends V> table) {
delegate().putAll(table);
}

@CanIgnoreReturnValue
@Override
public V remove(Object rowKey, Object columnKey) {
return delegate().remove(rowKey, columnKey);
Expand Down
9 changes: 9 additions & 0 deletions guava/src/com/google/common/collect/Multimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -160,6 +162,7 @@
* @author Jared Levy
* @since 2.0
*/
@CheckReturnValue
@GwtCompatible
public interface Multimap<K, V> {
// Query Operations
Expand Down Expand Up @@ -212,6 +215,7 @@ public interface Multimap<K, V> {
* {@code false} if the multimap already contained the key-value pair and
* doesn't allow duplicates
*/
@CanIgnoreReturnValue
boolean put(@Nullable K key, @Nullable V value);

/**
Expand All @@ -222,6 +226,7 @@ public interface Multimap<K, V> {
*
* @return {@code true} if the multimap changed
*/
@CanIgnoreReturnValue
boolean remove(@Nullable Object key, @Nullable Object value);

// Bulk Operations
Expand All @@ -239,6 +244,7 @@ public interface Multimap<K, V> {
*
* @return {@code true} if the multimap changed
*/
@CanIgnoreReturnValue
boolean putAll(@Nullable K key, Iterable<? extends V> values);

/**
Expand All @@ -247,6 +253,7 @@ public interface Multimap<K, V> {
*
* @return {@code true} if the multimap changed
*/
@CanIgnoreReturnValue
boolean putAll(Multimap<? extends K, ? extends V> multimap);

/**
Expand All @@ -261,6 +268,7 @@ public interface Multimap<K, V> {
* <i>may</i> be modifiable, but updating it will have no effect on the
* multimap.
*/
@CanIgnoreReturnValue
Collection<V> replaceValues(@Nullable K key, Iterable<? extends V> values);

/**
Expand All @@ -274,6 +282,7 @@ public interface Multimap<K, V> {
* collection <i>may</i> be modifiable, but updating it will have no
* effect on the multimap.
*/
@CanIgnoreReturnValue
Collection<V> removeAll(@Nullable Object key);

/**
Expand Down
Loading

0 comments on commit c56fa61

Please sign in to comment.