Skip to content

Commit

Permalink
Migrate from jsr305's @immutable to Error Prone's.
Browse files Browse the repository at this point in the history
RELNOTES=Migrate from jsr305's @immutable to Error Prone's.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=176011904
  • Loading branch information
cpovirk committed Nov 16, 2017
1 parent 328481c commit 901e985
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 62 deletions.
Expand Up @@ -18,27 +18,35 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap;
import com.google.errorprone.annotations.Immutable;
import com.google.j2objc.annotations.WeakOuter;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* A {@code RegularImmutableTable} optimized for dense data.
*/
/** A {@code RegularImmutableTable} optimized for dense data. */
@GwtCompatible
@Immutable
@Immutable(containerOf = {"R", "C", "V"})
final class DenseImmutableTable<R, C, V> extends RegularImmutableTable<R, C, V> {
private final ImmutableMap<R, Integer> rowKeyToIndex;
private final ImmutableMap<C, Integer> columnKeyToIndex;
private final ImmutableMap<R, Map<C, V>> rowMap;
private final ImmutableMap<C, Map<R, V>> columnMap;
private final ImmutableMap<R, ImmutableMap<C, V>> rowMap;
private final ImmutableMap<C, ImmutableMap<R, V>> columnMap;

@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] rowCounts;

@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] columnCounts;

@SuppressWarnings("Immutable") // We don't modify this after construction.
private final V[][] values;

// For each cell in iteration order, the index of that cell's row key in the row key list.
@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] cellRowIndices;

// For each cell in iteration order, the index of that cell's column key in the column key list.
@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] cellColumnIndices;

DenseImmutableTable(
Expand Down Expand Up @@ -183,7 +191,7 @@ boolean isPartialView() {
}

@WeakOuter
private final class RowMap extends ImmutableArrayMap<R, Map<C, V>> {
private final class RowMap extends ImmutableArrayMap<R, ImmutableMap<C, V>> {
private RowMap() {
super(rowCounts.length);
}
Expand All @@ -194,7 +202,7 @@ ImmutableMap<R, Integer> keyToIndex() {
}

@Override
Map<C, V> getValue(int keyIndex) {
ImmutableMap<C, V> getValue(int keyIndex) {
return new Row(keyIndex);
}

Expand All @@ -205,7 +213,7 @@ boolean isPartialView() {
}

@WeakOuter
private final class ColumnMap extends ImmutableArrayMap<C, Map<R, V>> {
private final class ColumnMap extends ImmutableArrayMap<C, ImmutableMap<R, V>> {
private ColumnMap() {
super(columnCounts.length);
}
Expand All @@ -216,7 +224,7 @@ ImmutableMap<C, Integer> keyToIndex() {
}

@Override
Map<R, V> getValue(int keyIndex) {
ImmutableMap<R, V> getValue(int keyIndex) {
return new Column(keyIndex);
}

Expand All @@ -228,12 +236,16 @@ boolean isPartialView() {

@Override
public ImmutableMap<C, Map<R, V>> columnMap() {
return columnMap;
// Casts without copying.
ImmutableMap<C, ImmutableMap<R, V>> columnMap = this.columnMap;
return ImmutableMap.<C, Map<R, V>>copyOf(columnMap);
}

@Override
public ImmutableMap<R, Map<C, V>> rowMap() {
return rowMap;
// Casts without copying.
ImmutableMap<R, ImmutableMap<C, V>> rowMap = this.rowMap;
return ImmutableMap.<R, Map<C, V>>copyOf(rowMap);
}

@Override
Expand Down
Expand Up @@ -15,27 +15,29 @@
package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.Immutable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.concurrent.Immutable;

/**
* A {@code RegularImmutableTable} optimized for sparse data.
*/
/** A {@code RegularImmutableTable} optimized for sparse data. */
@GwtCompatible
@Immutable
@Immutable(containerOf = {"R", "C", "V"})
final class SparseImmutableTable<R, C, V> extends RegularImmutableTable<R, C, V> {
static final ImmutableTable<Object, Object, Object> EMPTY =
new SparseImmutableTable<>(
ImmutableList.<Cell<Object, Object, Object>>of(), ImmutableSet.of(), ImmutableSet.of());

private final ImmutableMap<R, Map<C, V>> rowMap;
private final ImmutableMap<C, Map<R, V>> columnMap;
private final ImmutableMap<R, ImmutableMap<C, V>> rowMap;
private final ImmutableMap<C, ImmutableMap<R, V>> columnMap;

// For each cell in iteration order, the index of that cell's row key in the row key list.
@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] cellRowIndices;

// For each cell in iteration order, the index of that cell's column key in the list of column
// keys present in that row.
@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] cellColumnInRowIndices;

SparseImmutableTable(
Expand Down Expand Up @@ -78,13 +80,15 @@ final class SparseImmutableTable<R, C, V> extends RegularImmutableTable<R, C, V>
}
this.cellRowIndices = cellRowIndices;
this.cellColumnInRowIndices = cellColumnInRowIndices;
ImmutableMap.Builder<R, Map<C, V>> rowBuilder = new ImmutableMap.Builder<>(rows.size());
ImmutableMap.Builder<R, ImmutableMap<C, V>> rowBuilder =
new ImmutableMap.Builder<>(rows.size());
for (Entry<R, Map<C, V>> row : rows.entrySet()) {
rowBuilder.put(row.getKey(), ImmutableMap.copyOf(row.getValue()));
}
this.rowMap = rowBuilder.build();

ImmutableMap.Builder<C, Map<R, V>> columnBuilder = new ImmutableMap.Builder<>(columns.size());
ImmutableMap.Builder<C, ImmutableMap<R, V>> columnBuilder =
new ImmutableMap.Builder<>(columns.size());
for (Entry<C, Map<R, V>> col : columns.entrySet()) {
columnBuilder.put(col.getKey(), ImmutableMap.copyOf(col.getValue()));
}
Expand All @@ -93,12 +97,16 @@ final class SparseImmutableTable<R, C, V> extends RegularImmutableTable<R, C, V>

@Override
public ImmutableMap<C, Map<R, V>> columnMap() {
return columnMap;
// Casts without copying.
ImmutableMap<C, ImmutableMap<R, V>> columnMap = this.columnMap;
return ImmutableMap.<C, Map<R, V>>copyOf(columnMap);
}

@Override
public ImmutableMap<R, Map<C, V>> rowMap() {
return rowMap;
// Casts without copying.
ImmutableMap<R, ImmutableMap<C, V>> rowMap = this.rowMap;
return ImmutableMap.<R, Map<C, V>>copyOf(rowMap);
}

@Override
Expand All @@ -109,8 +117,8 @@ public int size() {
@Override
Cell<R, C, V> getCell(int index) {
int rowIndex = cellRowIndices[index];
Entry<R, Map<C, V>> rowEntry = rowMap.entrySet().asList().get(rowIndex);
ImmutableMap<C, V> row = (ImmutableMap<C, V>) rowEntry.getValue();
Entry<R, ImmutableMap<C, V>> rowEntry = rowMap.entrySet().asList().get(rowIndex);
ImmutableMap<C, V> row = rowEntry.getValue();
int columnIndex = cellColumnInRowIndices[index];
Entry<C, V> colEntry = row.entrySet().asList().get(columnIndex);
return cellOf(rowEntry.getKey(), colEntry.getKey(), colEntry.getValue());
Expand All @@ -119,7 +127,7 @@ Cell<R, C, V> getCell(int index) {
@Override
V getValue(int index) {
int rowIndex = cellRowIndices[index];
ImmutableMap<C, V> row = (ImmutableMap<C, V>) rowMap.values().asList().get(rowIndex);
ImmutableMap<C, V> row = rowMap.values().asList().get(rowIndex);
int columnIndex = cellColumnInRowIndices[index];
return row.values().asList().get(columnIndex);
}
Expand Down
2 changes: 1 addition & 1 deletion android/guava/src/com/google/common/net/HostAndPort.java
Expand Up @@ -22,9 +22,9 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.errorprone.annotations.Immutable;
import java.io.Serializable;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* An immutable representation of a host and port.
Expand Down
2 changes: 1 addition & 1 deletion android/guava/src/com/google/common/net/MediaType.java
Expand Up @@ -36,6 +36,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
Expand All @@ -44,7 +45,6 @@
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* Represents an <a href="http://en.wikipedia.org/wiki/Internet_media_type">Internet Media Type</a>
Expand Down
Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.Immutable;

/**
* Base class for implementing services that can handle {@link #doStart} and {@link #doStop}
Expand Down Expand Up @@ -542,7 +541,7 @@ public String toString() {
* snapshot of the state and therefore it can be used to answer simple queries without needing to
* grab a lock.
*/
@Immutable
// @Immutable except that Throwable is mutable (initCause(), setStackTrace(), mutable subclasses).
private static final class StateSnapshot {
/**
* The internal state, which equals external state unless shutdownWhenStartupFinishes is true.
Expand Down
38 changes: 25 additions & 13 deletions guava/src/com/google/common/collect/DenseImmutableTable.java
Expand Up @@ -18,27 +18,35 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap;
import com.google.errorprone.annotations.Immutable;
import com.google.j2objc.annotations.WeakOuter;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* A {@code RegularImmutableTable} optimized for dense data.
*/
/** A {@code RegularImmutableTable} optimized for dense data. */
@GwtCompatible
@Immutable
@Immutable(containerOf = {"R", "C", "V"})
final class DenseImmutableTable<R, C, V> extends RegularImmutableTable<R, C, V> {
private final ImmutableMap<R, Integer> rowKeyToIndex;
private final ImmutableMap<C, Integer> columnKeyToIndex;
private final ImmutableMap<R, Map<C, V>> rowMap;
private final ImmutableMap<C, Map<R, V>> columnMap;
private final ImmutableMap<R, ImmutableMap<C, V>> rowMap;
private final ImmutableMap<C, ImmutableMap<R, V>> columnMap;

@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] rowCounts;

@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] columnCounts;

@SuppressWarnings("Immutable") // We don't modify this after construction.
private final V[][] values;

// For each cell in iteration order, the index of that cell's row key in the row key list.
@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] cellRowIndices;

// For each cell in iteration order, the index of that cell's column key in the column key list.
@SuppressWarnings("Immutable") // We don't modify this after construction.
private final int[] cellColumnIndices;

DenseImmutableTable(
Expand Down Expand Up @@ -183,7 +191,7 @@ boolean isPartialView() {
}

@WeakOuter
private final class RowMap extends ImmutableArrayMap<R, Map<C, V>> {
private final class RowMap extends ImmutableArrayMap<R, ImmutableMap<C, V>> {
private RowMap() {
super(rowCounts.length);
}
Expand All @@ -194,7 +202,7 @@ ImmutableMap<R, Integer> keyToIndex() {
}

@Override
Map<C, V> getValue(int keyIndex) {
ImmutableMap<C, V> getValue(int keyIndex) {
return new Row(keyIndex);
}

Expand All @@ -205,7 +213,7 @@ boolean isPartialView() {
}

@WeakOuter
private final class ColumnMap extends ImmutableArrayMap<C, Map<R, V>> {
private final class ColumnMap extends ImmutableArrayMap<C, ImmutableMap<R, V>> {
private ColumnMap() {
super(columnCounts.length);
}
Expand All @@ -216,7 +224,7 @@ ImmutableMap<C, Integer> keyToIndex() {
}

@Override
Map<R, V> getValue(int keyIndex) {
ImmutableMap<R, V> getValue(int keyIndex) {
return new Column(keyIndex);
}

Expand All @@ -228,12 +236,16 @@ boolean isPartialView() {

@Override
public ImmutableMap<C, Map<R, V>> columnMap() {
return columnMap;
// Casts without copying.
ImmutableMap<C, ImmutableMap<R, V>> columnMap = this.columnMap;
return ImmutableMap.<C, Map<R, V>>copyOf(columnMap);
}

@Override
public ImmutableMap<R, Map<C, V>> rowMap() {
return rowMap;
// Casts without copying.
ImmutableMap<R, ImmutableMap<C, V>> rowMap = this.rowMap;
return ImmutableMap.<R, Map<C, V>>copyOf(rowMap);
}

@Override
Expand Down

0 comments on commit 901e985

Please sign in to comment.