Skip to content

Commit 003ee8a

Browse files
authored
Replace guava immutable collections with java immutable collections (#2454)
1 parent d3e8a80 commit 003ee8a

25 files changed

+108
-167
lines changed

src/main/java/org/spongepowered/api/data/DataManipulator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.api.data;
2626

27-
import com.google.common.collect.ImmutableList;
2827
import org.spongepowered.api.Sponge;
2928
import org.spongepowered.api.data.value.CopyableValueContainer;
3029
import org.spongepowered.api.data.value.MergeFunction;
@@ -34,11 +33,14 @@
3433
import org.spongepowered.api.util.annotation.eventgen.TransformWith;
3534
import org.spongepowered.api.world.World;
3635

36+
import java.util.Arrays;
3737
import java.util.Objects;
3838
import java.util.function.Consumer;
3939
import java.util.function.Function;
4040
import java.util.function.Predicate;
4141
import java.util.function.Supplier;
42+
import java.util.stream.Collectors;
43+
import java.util.stream.Stream;
4244

4345
/**
4446
* Represents a changelist of data that can be applied to a {@link DataHolder.Mutable}.
@@ -367,7 +369,7 @@ default Mutable copyFrom(final ValueContainer valueContainer, final Key<?> first
367369
* given {@link ValueContainer}
368370
*/
369371
default Mutable copyFrom(final ValueContainer valueContainer, final MergeFunction overlap, final Key<?> first, final Key<?>... more) {
370-
return this.copyFrom(valueContainer, overlap, ImmutableList.<Key<?>>builder().add(first).add(more).build());
372+
return this.copyFrom(valueContainer, overlap, Stream.concat(Stream.of(first), Arrays.stream(more)).collect(Collectors.toUnmodifiableList()));
371373
}
372374

373375
/**

src/main/java/org/spongepowered/api/data/DataTransactionResult.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
*/
2525
package org.spongepowered.api.data;
2626

27-
import com.google.common.collect.ImmutableList;
28-
import com.google.common.collect.ImmutableSet;
2927
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
3028
import org.checkerframework.checker.nullness.qual.Nullable;
3129
import org.spongepowered.api.data.value.Value;
@@ -79,7 +77,7 @@ public Function<Builder, DataTransactionResult> finisher() {
7977

8078
@Override
8179
public Set<Characteristics> characteristics() {
82-
return ImmutableSet.of();
80+
return Set.of();
8381
}
8482
};
8583

@@ -281,26 +279,26 @@ public enum Type {
281279
}
282280

283281
final Type type;
284-
private final ImmutableList<Value.Immutable<?>> rejected;
285-
private final ImmutableList<Value.Immutable<?>> replaced;
286-
private final ImmutableList<Value.Immutable<?>> success;
282+
private final List<Value.Immutable<?>> rejected;
283+
private final List<Value.Immutable<?>> replaced;
284+
private final List<Value.Immutable<?>> success;
287285

288286
DataTransactionResult(final Builder builder) {
289287
this.type = builder.resultType;
290288
if (builder.rejected != null) {
291-
this.rejected = ImmutableList.copyOf(builder.rejected);
289+
this.rejected = List.copyOf(builder.rejected);
292290
} else {
293-
this.rejected = ImmutableList.of();
291+
this.rejected = List.of();
294292
}
295293
if (builder.replaced != null) {
296-
this.replaced = ImmutableList.copyOf(builder.replaced);
294+
this.replaced = List.copyOf(builder.replaced);
297295
} else {
298-
this.replaced = ImmutableList.of();
296+
this.replaced = List.of();
299297
}
300298
if (builder.successful != null) {
301-
this.success = ImmutableList.copyOf(builder.successful);
299+
this.success = List.copyOf(builder.successful);
302300
} else {
303-
this.success = ImmutableList.of();
301+
this.success = List.of();
304302
}
305303
}
306304

src/main/java/org/spongepowered/api/data/persistence/DataQuery.java

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.api.data.persistence;
2626

27-
import com.google.common.collect.ImmutableList;
2827
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
2928

3029
import java.util.Iterator;
@@ -34,6 +33,8 @@
3433
import java.util.StringJoiner;
3534
import java.util.function.Consumer;
3635
import java.util.regex.Pattern;
36+
import java.util.stream.Collectors;
37+
import java.util.stream.Stream;
3738

3839
/**
3940
* Represents a query that can be done on views. Queries do not depend on
@@ -46,9 +47,9 @@ public final class DataQuery implements Iterable<String> {
4647
/**
4748
* The parts that make up this query.
4849
*/
49-
private final ImmutableList<String> parts;
50+
private final List<String> parts;
5051

51-
private @MonotonicNonNull ImmutableList<DataQuery> queryParts; //lazy loaded
52+
private @MonotonicNonNull List<DataQuery> queryParts; //lazy loaded
5253

5354
/**
5455
* Constructs a query using the given separator character and path.
@@ -70,7 +71,7 @@ private DataQuery(final char separator, final String path) {
7071
* @param parts The parts
7172
*/
7273
private DataQuery(final String... parts) {
73-
this.parts = ImmutableList.copyOf(parts);
74+
this.parts = List.of(parts);
7475
}
7576

7677
/**
@@ -79,7 +80,7 @@ private DataQuery(final String... parts) {
7980
* @param parts The parts
8081
*/
8182
private DataQuery(final List<String> parts) {
82-
this.parts = ImmutableList.copyOf(parts);
83+
this.parts = List.copyOf(parts);
8384
}
8485

8586
/**
@@ -151,12 +152,8 @@ public List<String> parts() {
151152
* @return The constructed query
152153
*/
153154
public DataQuery then(final DataQuery that) {
154-
final ImmutableList.Builder<String> builder = new ImmutableList.Builder<>();
155-
156-
builder.addAll(this.parts);
157-
builder.addAll(that.parts);
158-
159-
return new DataQuery(builder.build());
155+
final var parts = Stream.concat(this.parts.stream(), that.parts.stream()).collect(Collectors.toUnmodifiableList());
156+
return new DataQuery(parts);
160157
}
161158

162159
/**
@@ -167,13 +164,8 @@ public DataQuery then(final DataQuery that) {
167164
* @return The constructed query
168165
*/
169166
public DataQuery then(final String that) {
170-
final ImmutableList.Builder<String> builder =
171-
new ImmutableList.Builder<>();
172-
173-
builder.addAll(this.parts);
174-
builder.add(that);
175-
176-
return new DataQuery(builder.build());
167+
final var parts = Stream.concat(this.parts.stream(), Stream.of(that)).collect(Collectors.toUnmodifiableList());
168+
return new DataQuery(parts);
177169
}
178170

179171
/**
@@ -184,11 +176,7 @@ public DataQuery then(final String that) {
184176
*/
185177
public List<DataQuery> queryParts() {
186178
if (this.queryParts == null) {
187-
final ImmutableList.Builder<DataQuery> builder = ImmutableList.builder();
188-
for (final String part : this.parts()) {
189-
builder.add(new DataQuery(part));
190-
}
191-
this.queryParts = builder.build();
179+
this.queryParts = this.parts().stream().map(DataQuery::new).collect(Collectors.toUnmodifiableList());
192180
}
193181
return this.queryParts;
194182
}
@@ -204,11 +192,8 @@ public DataQuery pop() {
204192
if (this.parts.size() <= 1) {
205193
return DataQuery.of();
206194
}
207-
final ImmutableList.Builder<String> builder = ImmutableList.builder();
208-
for (int i = 0; i < this.parts.size() - 1; i++) {
209-
builder.add(this.parts.get(i));
210-
}
211-
return new DataQuery(builder.build());
195+
final var parts = this.parts.stream().limit(this.parts.size() - 1L).collect(Collectors.toUnmodifiableList());
196+
return new DataQuery(parts);
212197
}
213198

214199
/**
@@ -222,11 +207,8 @@ public DataQuery popFirst() {
222207
if (this.parts.size() <= 1) {
223208
return DataQuery.of();
224209
}
225-
final ImmutableList.Builder<String> builder = ImmutableList.builder();
226-
for (int i = 1; i < this.parts.size(); i++) {
227-
builder.add(this.parts.get(i));
228-
}
229-
return new DataQuery(builder.build());
210+
final var parts = this.parts.stream().skip(1L).collect(Collectors.toUnmodifiableList());
211+
return new DataQuery(parts);
230212
}
231213

232214
/**

src/main/java/org/spongepowered/api/data/value/ValueContainer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.api.data.value;
2626

27-
import com.google.common.collect.ImmutableSet;
2827
import org.checkerframework.checker.nullness.qual.Nullable;
2928
import org.spongepowered.api.data.DataHolder;
3029
import org.spongepowered.api.data.DataManipulator;
@@ -210,7 +209,7 @@ default boolean supports(final Value<?> value) {
210209
* Gets all applicable {@link Key}s for this {@link ValueContainer}.
211210
* Changes can not be made to the set to alter the {@link ValueContainer},
212211
* nor can the {@link Value}s be changed with the provided
213-
* {@link ImmutableSet}.
212+
* {@link Set}.
214213
*
215214
* @return An immutable set of known {@link Key}s
216215
*/

src/main/java/org/spongepowered/api/event/Cause.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.api.event;
2626

27-
import com.google.common.collect.ImmutableList;
2827
import org.checkerframework.checker.nullness.qual.Nullable;
2928
import org.spongepowered.api.util.CopyableBuilder;
3029
import org.spongepowered.api.util.annotation.DoNotStore;
@@ -37,6 +36,7 @@
3736
import java.util.NoSuchElementException;
3837
import java.util.Optional;
3938
import java.util.StringJoiner;
39+
import java.util.stream.Collectors;
4040

4141
/**
4242
* A cause represents the reason or initiator of an event.
@@ -116,7 +116,7 @@ public static Cause of(final EventContext ctx, final Iterable<Object> iterable)
116116
private final EventContext context;
117117

118118
// lazy load
119-
@Nullable private ImmutableList<Object> immutableCauses;
119+
@Nullable private List<Object> immutableCauses;
120120

121121
/**
122122
* Constructs a new cause.
@@ -281,21 +281,15 @@ public boolean contains(final Object object) {
281281
}
282282

283283
/**
284-
* Gets an {@link ImmutableList} of all objects that are instances of the
284+
* Gets an immutable {@link List} of all objects that are instances of the
285285
* given {@link Class} type <code>T</code>.
286286
*
287287
* @param <T> The type of objects to query for
288288
* @param target The class of the target type
289289
* @return An immutable list of the objects queried
290290
*/
291291
public <T> List<T> allOf(final Class<T> target) {
292-
final ImmutableList.Builder<T> builder = ImmutableList.builder();
293-
for (final Object aCause : this.cause) {
294-
if (target.isInstance(aCause)) {
295-
builder.add((T) aCause);
296-
}
297-
}
298-
return builder.build();
292+
return Arrays.stream(this.cause).filter(target::isInstance).map(entry -> (T) entry).collect(Collectors.toUnmodifiableList());
299293
}
300294

301295
/**
@@ -306,13 +300,7 @@ public <T> List<T> allOf(final Class<T> target) {
306300
* @return The list of objects not an instance of the provided class
307301
*/
308302
public List<Object> noneOf(final Class<?> ignoredClass) {
309-
final ImmutableList.Builder<Object> builder = ImmutableList.builder();
310-
for (final Object cause : this.cause) {
311-
if (!ignoredClass.isInstance(cause)) {
312-
builder.add(cause);
313-
}
314-
}
315-
return builder.build();
303+
return Arrays.stream(this.cause).filter(entry -> !ignoredClass.isInstance(entry)).collect(Collectors.toUnmodifiableList());
316304
}
317305

318306
/**
@@ -322,7 +310,7 @@ public List<Object> noneOf(final Class<?> ignoredClass) {
322310
*/
323311
public List<Object> all() {
324312
if (this.immutableCauses == null) {
325-
this.immutableCauses = ImmutableList.copyOf(this.cause);
313+
this.immutableCauses = List.of(this.cause);
326314
}
327315
return this.immutableCauses;
328316
}

src/main/java/org/spongepowered/api/event/EventContext.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
*/
2525
package org.spongepowered.api.event;
2626

27-
28-
import com.google.common.collect.ImmutableMap;
29-
import com.google.common.collect.Maps;
3027
import org.checkerframework.checker.nullness.qual.Nullable;
3128
import org.spongepowered.api.util.CopyableBuilder;
3229
import org.spongepowered.api.util.annotation.DoNotStore;
3330

31+
import java.util.HashMap;
3432
import java.util.Map;
3533
import java.util.NoSuchElementException;
3634
import java.util.Objects;
@@ -46,7 +44,7 @@
4644
@DoNotStore
4745
public final class EventContext {
4846

49-
private static final EventContext EMPTY_CONTEXT = new EventContext(ImmutableMap.of());
47+
private static final EventContext EMPTY_CONTEXT = new EventContext(Map.of());
5048

5149
/**
5250
* Gets an empty context.
@@ -83,7 +81,7 @@ public static Builder builder() {
8381
private final Map<EventContextKey<?>, Object> entries;
8482

8583
EventContext(Map<EventContextKey<?>, Object> values) {
86-
this.entries = ImmutableMap.copyOf(values);
84+
this.entries = Map.copyOf(values);
8785
}
8886

8987
/**
@@ -226,7 +224,7 @@ public String toString() {
226224
public static final class Builder implements org.spongepowered.api.util.Builder<EventContext, Builder>, CopyableBuilder<EventContext,
227225
Builder> {
228226

229-
private final Map<EventContextKey<?>, Object> entries = Maps.newHashMap();
227+
private final Map<EventContextKey<?>, Object> entries = new HashMap<>();
230228

231229
Builder() {
232230

src/main/java/org/spongepowered/api/event/entity/AttackEntityEvent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.api.event.entity;
2626

27-
import com.google.common.collect.ImmutableMap;
2827
import org.spongepowered.api.block.entity.carrier.Dispenser;
2928
import org.spongepowered.api.effect.potion.PotionEffect;
3029
import org.spongepowered.api.entity.Entity;
@@ -252,7 +251,7 @@ public interface AttackEntityEvent extends Event, Cancellable {
252251
double originalFinalDamage();
253252

254253
/**
255-
* Gets an {@link ImmutableMap} of all original {@link DamageModifier}s
254+
* Gets an immutable {@link Map} of all original {@link DamageModifier}s
256255
* and their associated "modified" damage. Note that ordering is not
257256
* retained.
258257
*

src/main/java/org/spongepowered/api/event/entity/DamageEntityEvent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.api.event.entity;
2626

27-
import com.google.common.collect.ImmutableMap;
2827
import org.spongepowered.api.block.entity.carrier.Dispenser;
2928
import org.spongepowered.api.entity.Entity;
3029
import org.spongepowered.api.entity.living.monster.skeleton.Skeleton;
@@ -171,7 +170,7 @@ public interface DamageEntityEvent extends Event, Cancellable {
171170
double originalFinalDamage();
172171

173172
/**
174-
* Gets an {@link ImmutableMap} of all original {@link DamageModifier}s
173+
* Gets an immutable {@link Map} of all original {@link DamageModifier}s
175174
* and their associated "modified" damage. Note that ordering is not
176175
* retained.
177176
*

src/main/java/org/spongepowered/api/event/impl/entity/AbstractAffectEntityEvent.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.api.event.impl.entity;
2626

27-
import com.google.common.collect.ImmutableList;
2827
import org.spongepowered.api.entity.Entity;
2928
import org.spongepowered.api.entity.EntitySnapshot;
3029
import org.spongepowered.api.event.Order;
@@ -35,6 +34,7 @@
3534
import java.util.Collections;
3635
import java.util.List;
3736
import java.util.function.Predicate;
37+
import java.util.stream.Collectors;
3838

3939
public abstract class AbstractAffectEntityEvent extends AbstractEvent implements AffectEntityEvent {
4040

@@ -47,11 +47,7 @@ public abstract class AbstractAffectEntityEvent extends AbstractEvent implements
4747
public List<EntitySnapshot> entitySnapshots() {
4848
if (this.entitySnapshots == null) {
4949
if (this.currentOrder == Order.PRE) {
50-
final ImmutableList.Builder<EntitySnapshot> builder = ImmutableList.builder();
51-
for (Entity entity : this.entities) {
52-
builder.add(entity.createSnapshot());
53-
}
54-
this.entitySnapshots = builder.build();
50+
this.entitySnapshots = this.entities.stream().map(Entity::createSnapshot).collect(Collectors.toUnmodifiableList());
5551
} else {
5652
throw new IllegalStateException("Can't initialize entity snapshots after PRE!");
5753
}

0 commit comments

Comments
 (0)