diff --git a/android/guava/src/com/google/common/collect/ImmutableBiMap.java b/android/guava/src/com/google/common/collect/ImmutableBiMap.java index c1caad0f4557..6c6057b98954 100644 --- a/android/guava/src/com/google/common/collect/ImmutableBiMap.java +++ b/android/guava/src/com/google/common/collect/ImmutableBiMap.java @@ -310,7 +310,7 @@ public static Builder builderWithExpectedSize(int expectedSize) { * .put("one", 1) * .put("two", 2) * .put("three", 3) - * .build(); + * .buildOrThrow(); * } * *

For small immutable bimaps, the {@code ImmutableBiMap.of()} methods are even more @@ -323,8 +323,8 @@ public static Builder builderWithExpectedSize(int expectedSize) { * want a different order, consider using {@link #orderEntriesByValue(Comparator)}, which changes * this builder to sort entries by value. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple bimaps in series. Each bimap is a superset of the bimaps created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple bimaps in series. Each bimap is a superset of the bimaps created before it. * * @since 2.0 */ @@ -423,10 +423,27 @@ Builder combine(ImmutableMap.Builder builder) { * order in which entries were inserted into the builder, unless {@link #orderEntriesByValue} * was called, in which case entries are sorted by value. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate keys or values. The {@code build()} method + * will soon be deprecated. + * * @throws IllegalArgumentException if duplicate keys or values were added */ @Override public ImmutableBiMap build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable bimap, or throws an exception if any key or value was added + * more than once. The iteration order of the returned bimap is the order in which entries were + * inserted into the builder, unless {@link #orderEntriesByValue} was called, in which case + * entries are sorted by value. + * + * @throws IllegalArgumentException if duplicate keys or values were added + */ + @Override + public ImmutableBiMap buildOrThrow() { if (size == 0) { return of(); } diff --git a/android/guava/src/com/google/common/collect/ImmutableMap.java b/android/guava/src/com/google/common/collect/ImmutableMap.java index 7b1ab74045a2..0fa477a6e301 100644 --- a/android/guava/src/com/google/common/collect/ImmutableMap.java +++ b/android/guava/src/com/google/common/collect/ImmutableMap.java @@ -359,7 +359,7 @@ static IllegalArgumentException conflictException( * .put("one", 1) * .put("two", 2) * .put("three", 3) - * .build(); + * .buildOrThrow(); * } * *

For small immutable maps, the {@code ImmutableMap.of()} methods are even more @@ -373,8 +373,8 @@ static IllegalArgumentException conflictException( * sort by keys, or call {@link #orderEntriesByValue(Comparator)}, which changes this builder to * sort entries by value. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple maps in series. Each map is a superset of the maps created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple maps in series. Each map is a superset of the maps created before it. * * @since 2.0 */ @@ -508,10 +508,26 @@ Builder combine(Builder other) { * in which entries were inserted into the builder, unless {@link #orderEntriesByValue} was * called, in which case entries are sorted by value. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate keys. The {@code build()} method will soon be + * deprecated. + * * @throws IllegalArgumentException if duplicate keys were added */ - @SuppressWarnings("unchecked") public ImmutableMap build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable map, or throws an exception if any key was added more than + * once. The iteration order of the returned map is the order in which entries were inserted + * into the builder, unless {@link #orderEntriesByValue} was called, in which case entries are + * sorted by value. + * + * @throws IllegalArgumentException if duplicate keys were added + */ + @SuppressWarnings("unchecked") + public ImmutableMap buildOrThrow() { /* * If entries is full, then this implementation may end up using the entries array * directly and writing over the entry objects with non-terminal entries, but this is diff --git a/android/guava/src/com/google/common/collect/ImmutableSortedMap.java b/android/guava/src/com/google/common/collect/ImmutableSortedMap.java index c9767f15c20e..a4942808230c 100644 --- a/android/guava/src/com/google/common/collect/ImmutableSortedMap.java +++ b/android/guava/src/com/google/common/collect/ImmutableSortedMap.java @@ -560,14 +560,14 @@ public static , V> Builder reverseOrder() { * .put(1, "one") * .put(2, "two") * .put(3, "three") - * .build(); + * .buildOrThrow(); * } * *

For small immutable sorted maps, the {@code ImmutableSortedMap.of()} methods are even * more convenient. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple maps in series. Each map is a superset of the maps created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple maps in series. Each map is a superset of the maps created before it. * * @since 2.0 */ @@ -686,11 +686,27 @@ Builder combine(ImmutableSortedMap.Builder other) { /** * Returns a newly-created immutable sorted map. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate keys. The {@code build()} method will soon be + * deprecated. + * * @throws IllegalArgumentException if any two keys are equal according to the comparator (which * might be the keys' natural order) */ @Override public ImmutableSortedMap build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable sorted map, or throws an exception if any two keys are + * equal. + * + * @throws IllegalArgumentException if any two keys are equal according to the comparator (which + * might be the keys' natural order) + */ + @Override + public ImmutableSortedMap buildOrThrow() { switch (size) { case 0: return emptyMap(comparator); diff --git a/android/guava/src/com/google/common/collect/ImmutableTable.java b/android/guava/src/com/google/common/collect/ImmutableTable.java index 4a087ce6df68..12d9659a7c55 100644 --- a/android/guava/src/com/google/common/collect/ImmutableTable.java +++ b/android/guava/src/com/google/common/collect/ImmutableTable.java @@ -122,7 +122,7 @@ static Cell cellOf(R rowKey, C columnKey, V value) { * .put(1, 'A', "foo") * .put(1, 'B', "bar") * .put(2, 'A', "baz") - * .build(); + * .buildOrThrow(); * } * *

By default, the order in which cells are added to the builder determines the iteration @@ -133,8 +133,8 @@ static Cell cellOf(R rowKey, C columnKey, V value) { *

For empty or single-cell immutable tables, {@link #of()} and {@link #of(Object, Object, * Object)} are even more convenient. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple tables in series. Each table is a superset of the tables created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple tables in series. Each table is a superset of the tables created before it. * * @since 11.0 */ @@ -216,9 +216,23 @@ Builder combine(Builder other) { /** * Returns a newly-created immutable table. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate key pairs. The {@code build()} method will + * soon be deprecated. + * * @throws IllegalArgumentException if duplicate key pairs were added */ public ImmutableTable build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable table, or throws an exception if duplicate key pairs were + * added. + * + * @throws IllegalArgumentException if duplicate key pairs were added + */ + public ImmutableTable buildOrThrow() { int size = cells.size(); switch (size) { case 0: diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableBiMap.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableBiMap.java index 0b79a50b4d2b..ff989657fa76 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableBiMap.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableBiMap.java @@ -200,11 +200,16 @@ Builder combine(Builder other) { @Override public ImmutableBiMap build() { - ImmutableMap map = super.build(); + return buildOrThrow(); + } + + @Override + public ImmutableBiMap buildOrThrow() { + ImmutableMap map = super.buildOrThrow(); if (map.isEmpty()) { return of(); } - return new RegularImmutableBiMap(super.build()); + return new RegularImmutableBiMap(super.buildOrThrow()); } @Override diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableMap.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableMap.java index 5f662cf22f67..cef789a88280 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableMap.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableMap.java @@ -307,6 +307,10 @@ Builder combine(Builder other) { } public ImmutableMap build() { + return buildOrThrow(); + } + + public ImmutableMap buildOrThrow() { if (valueComparator != null) { Collections.sort( entries, Ordering.from(valueComparator).onResultOf(Maps.valueFunction())); diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java index 31840654358b..fb626be42eaf 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java @@ -379,6 +379,11 @@ public Builder orderEntriesByValue(Comparator valueComparator) @Override public ImmutableSortedMap build() { + return buildOrThrow(); + } + + @Override + public ImmutableSortedMap buildOrThrow() { SortedMap delegate = newModifiableDelegate(comparator); for (Entry entry : entries) { putEntryWithChecks(delegate, entry); diff --git a/guava/src/com/google/common/collect/ImmutableBiMap.java b/guava/src/com/google/common/collect/ImmutableBiMap.java index bbb06102bdb3..4378a623adc0 100644 --- a/guava/src/com/google/common/collect/ImmutableBiMap.java +++ b/guava/src/com/google/common/collect/ImmutableBiMap.java @@ -316,7 +316,7 @@ public static Builder builderWithExpectedSize(int expectedSize) { * .put("one", 1) * .put("two", 2) * .put("three", 3) - * .build(); + * .buildOrThrow(); * } * *

For small immutable bimaps, the {@code ImmutableBiMap.of()} methods are even more @@ -329,8 +329,8 @@ public static Builder builderWithExpectedSize(int expectedSize) { * want a different order, consider using {@link #orderEntriesByValue(Comparator)}, which changes * this builder to sort entries by value. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple bimaps in series. Each bimap is a superset of the bimaps created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple bimaps in series. Each bimap is a superset of the bimaps created before it. * * @since 2.0 */ @@ -428,10 +428,27 @@ Builder combine(ImmutableMap.Builder builder) { * order in which entries were inserted into the builder, unless {@link #orderEntriesByValue} * was called, in which case entries are sorted by value. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate keys or values. The {@code build()} method + * will soon be deprecated. + * * @throws IllegalArgumentException if duplicate keys or values were added */ @Override public ImmutableBiMap build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable bimap, or throws an exception if any key or value was added + * more than once. The iteration order of the returned bimap is the order in which entries were + * inserted into the builder, unless {@link #orderEntriesByValue} was called, in which case + * entries are sorted by value. + * + * @throws IllegalArgumentException if duplicate keys or values were added + */ + @Override + public ImmutableBiMap buildOrThrow() { switch (size) { case 0: return of(); diff --git a/guava/src/com/google/common/collect/ImmutableMap.java b/guava/src/com/google/common/collect/ImmutableMap.java index 5cd956c02707..75a105998c6f 100644 --- a/guava/src/com/google/common/collect/ImmutableMap.java +++ b/guava/src/com/google/common/collect/ImmutableMap.java @@ -387,7 +387,7 @@ static IllegalArgumentException conflictException( * .put("one", 1) * .put("two", 2) * .put("three", 3) - * .build(); + * .buildOrThrow(); * } * *

For small immutable maps, the {@code ImmutableMap.of()} methods are even more @@ -401,8 +401,8 @@ static IllegalArgumentException conflictException( * sort by keys, or call {@link #orderEntriesByValue(Comparator)}, which changes this builder to * sort entries by value. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple maps in series. Each map is a superset of the maps created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple maps in series. Each map is a superset of the maps created before it. * * @since 2.0 */ @@ -528,9 +528,25 @@ Builder combine(Builder other) { * in which entries were inserted into the builder, unless {@link #orderEntriesByValue} was * called, in which case entries are sorted by value. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate keys. The {@code build()} method will soon be + * deprecated. + * * @throws IllegalArgumentException if duplicate keys were added */ public ImmutableMap build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable map, or throws an exception if any key was added more than + * once. The iteration order of the returned map is the order in which entries were inserted + * into the builder, unless {@link #orderEntriesByValue} was called, in which case entries are + * sorted by value. + * + * @throws IllegalArgumentException if duplicate keys were added + */ + public ImmutableMap buildOrThrow() { /* * If entries is full, or if hash flooding is detected, then this implementation may end up * using the entries array directly and writing over the entry objects with non-terminal diff --git a/guava/src/com/google/common/collect/ImmutableSortedMap.java b/guava/src/com/google/common/collect/ImmutableSortedMap.java index 1b4a0d284380..5d507464aaeb 100644 --- a/guava/src/com/google/common/collect/ImmutableSortedMap.java +++ b/guava/src/com/google/common/collect/ImmutableSortedMap.java @@ -607,14 +607,14 @@ public static , V> Builder reverseOrder() { * .put(1, "one") * .put(2, "two") * .put(3, "three") - * .build(); + * .buildOrThrow(); * } * *

For small immutable sorted maps, the {@code ImmutableSortedMap.of()} methods are even * more convenient. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple maps in series. Each map is a superset of the maps created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple maps in series. Each map is a superset of the maps created before it. * * @since 2.0 */ @@ -710,11 +710,27 @@ Builder combine(ImmutableMap.Builder other) { /** * Returns a newly-created immutable sorted map. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate keys. The {@code build()} method will soon be + * deprecated. + * * @throws IllegalArgumentException if any two keys are equal according to the comparator (which * might be the keys' natural order) */ @Override public ImmutableSortedMap build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable sorted map, or throws an exception if any two keys are + * equal. + * + * @throws IllegalArgumentException if any two keys are equal according to the comparator (which + * might be the keys' natural order) + */ + @Override + public ImmutableSortedMap buildOrThrow() { switch (size) { case 0: return emptyMap(comparator); diff --git a/guava/src/com/google/common/collect/ImmutableTable.java b/guava/src/com/google/common/collect/ImmutableTable.java index 9e3cd00faa59..fb3186162ef7 100644 --- a/guava/src/com/google/common/collect/ImmutableTable.java +++ b/guava/src/com/google/common/collect/ImmutableTable.java @@ -166,7 +166,7 @@ static Cell cellOf(R rowKey, C columnKey, V value) { * .put(1, 'A', "foo") * .put(1, 'B', "bar") * .put(2, 'A', "baz") - * .build(); + * .buildOrThrow(); * } * *

By default, the order in which cells are added to the builder determines the iteration @@ -177,8 +177,8 @@ static Cell cellOf(R rowKey, C columnKey, V value) { *

For empty or single-cell immutable tables, {@link #of()} and {@link #of(Object, Object, * Object)} are even more convenient. * - *

Builder instances can be reused - it is safe to call {@link #build} multiple times to build - * multiple tables in series. Each table is a superset of the tables created before it. + *

Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to + * build multiple tables in series. Each table is a superset of the tables created before it. * * @since 11.0 */ @@ -260,9 +260,23 @@ Builder combine(Builder other) { /** * Returns a newly-created immutable table. * + *

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method + * will throw an exception if there are duplicate key pairs. The {@code build()} method will + * soon be deprecated. + * * @throws IllegalArgumentException if duplicate key pairs were added */ public ImmutableTable build() { + return buildOrThrow(); + } + + /** + * Returns a newly-created immutable table, or throws an exception if duplicate key pairs were + * added. + * + * @throws IllegalArgumentException if duplicate key pairs were added + */ + public ImmutableTable buildOrThrow() { int size = cells.size(); switch (size) { case 0: