From bb7384639bc807da229abf6f96bbef3b20edbbfd Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Wed, 14 Sep 2016 09:04:52 -0700 Subject: [PATCH] core: Remove com.google.common.collect usages, again Commit 656e8ce (#2208) removed most usages, but missed one and one was re-added. Since all usages are removed, it should be much easier to notice regressions. --- .../java/io/grpc/ResolvedServerInfoGroup.java | 13 ++++--- .../grpc/internal/RoundRobinServerList.java | 36 +++++++++++++++++-- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/io/grpc/ResolvedServerInfoGroup.java b/core/src/main/java/io/grpc/ResolvedServerInfoGroup.java index 43f04f15aa..0536327352 100644 --- a/core/src/main/java/io/grpc/ResolvedServerInfoGroup.java +++ b/core/src/main/java/io/grpc/ResolvedServerInfoGroup.java @@ -35,7 +35,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.Objects; -import com.google.common.collect.ImmutableList; import java.net.SocketAddress; import java.util.ArrayList; @@ -64,7 +63,8 @@ public final class ResolvedServerInfoGroup { private ResolvedServerInfoGroup(List resolvedServerInfoList, Attributes attributes) { checkArgument(!resolvedServerInfoList.isEmpty(), "empty server list"); - this.resolvedServerInfoList = Collections.unmodifiableList(resolvedServerInfoList); + this.resolvedServerInfoList = + Collections.unmodifiableList(new ArrayList(resolvedServerInfoList)); this.attributes = checkNotNull(attributes, "attributes"); } @@ -148,11 +148,10 @@ public final class ResolvedServerInfoGroup { } public static class Builder { - private final ImmutableList.Builder groupBuilder; + private final List group = new ArrayList(); private final Attributes attributes; public Builder(Attributes attributes) { - this.groupBuilder = ImmutableList.builder(); this.attributes = attributes; } @@ -161,17 +160,17 @@ public final class ResolvedServerInfoGroup { } public Builder add(ResolvedServerInfo resolvedServerInfo) { - groupBuilder.add(resolvedServerInfo); + group.add(resolvedServerInfo); return this; } public Builder addAll(Collection resolvedServerInfo) { - groupBuilder.addAll(resolvedServerInfo); + group.addAll(resolvedServerInfo); return this; } public ResolvedServerInfoGroup build() { - return new ResolvedServerInfoGroup(groupBuilder.build(), attributes); + return new ResolvedServerInfoGroup(group, attributes); } } } diff --git a/core/src/main/java/io/grpc/internal/RoundRobinServerList.java b/core/src/main/java/io/grpc/internal/RoundRobinServerList.java index 9fe8212682..35336bb006 100644 --- a/core/src/main/java/io/grpc/internal/RoundRobinServerList.java +++ b/core/src/main/java/io/grpc/internal/RoundRobinServerList.java @@ -32,7 +32,6 @@ package io.grpc.internal; import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Iterators; import io.grpc.EquivalentAddressGroup; import io.grpc.Status; @@ -44,6 +43,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; import javax.annotation.Nullable; import javax.annotation.concurrent.NotThreadSafe; @@ -62,7 +62,7 @@ public class RoundRobinServerList { private RoundRobinServerList(TransportManager tm, List list) { this.tm = tm; this.list = list; - this.cyclingIter = Iterators.cycle(list); + this.cyclingIter = new CycleIterator(list); this.requestDroppingTransport = tm.createFailingTransport(Status.UNAVAILABLE.withDescription("Throttled by LB")); } @@ -136,4 +136,36 @@ public class RoundRobinServerList { Collections.unmodifiableList(new ArrayList(list))); } } + + private static final class CycleIterator implements Iterator { + private final List list; + private int index; + + public CycleIterator(List list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return !list.isEmpty(); + } + + @Override + public T next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + T val = list.get(index); + index++; + if (index >= list.size()) { + index -= list.size(); + } + return val; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + } }