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.
This commit is contained in:
Eric Anderson 2016-09-14 09:04:52 -07:00 committed by GitHub
parent 1623063143
commit bb7384639b
2 changed files with 40 additions and 9 deletions

View File

@ -35,7 +35,6 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
@ -64,7 +63,8 @@ public final class ResolvedServerInfoGroup {
private ResolvedServerInfoGroup(List<ResolvedServerInfo> resolvedServerInfoList, private ResolvedServerInfoGroup(List<ResolvedServerInfo> resolvedServerInfoList,
Attributes attributes) { Attributes attributes) {
checkArgument(!resolvedServerInfoList.isEmpty(), "empty server list"); checkArgument(!resolvedServerInfoList.isEmpty(), "empty server list");
this.resolvedServerInfoList = Collections.unmodifiableList(resolvedServerInfoList); this.resolvedServerInfoList =
Collections.unmodifiableList(new ArrayList<ResolvedServerInfo>(resolvedServerInfoList));
this.attributes = checkNotNull(attributes, "attributes"); this.attributes = checkNotNull(attributes, "attributes");
} }
@ -148,11 +148,10 @@ public final class ResolvedServerInfoGroup {
} }
public static class Builder { public static class Builder {
private final ImmutableList.Builder<ResolvedServerInfo> groupBuilder; private final List<ResolvedServerInfo> group = new ArrayList<ResolvedServerInfo>();
private final Attributes attributes; private final Attributes attributes;
public Builder(Attributes attributes) { public Builder(Attributes attributes) {
this.groupBuilder = ImmutableList.builder();
this.attributes = attributes; this.attributes = attributes;
} }
@ -161,17 +160,17 @@ public final class ResolvedServerInfoGroup {
} }
public Builder add(ResolvedServerInfo resolvedServerInfo) { public Builder add(ResolvedServerInfo resolvedServerInfo) {
groupBuilder.add(resolvedServerInfo); group.add(resolvedServerInfo);
return this; return this;
} }
public Builder addAll(Collection<ResolvedServerInfo> resolvedServerInfo) { public Builder addAll(Collection<ResolvedServerInfo> resolvedServerInfo) {
groupBuilder.addAll(resolvedServerInfo); group.addAll(resolvedServerInfo);
return this; return this;
} }
public ResolvedServerInfoGroup build() { public ResolvedServerInfoGroup build() {
return new ResolvedServerInfoGroup(groupBuilder.build(), attributes); return new ResolvedServerInfoGroup(group, attributes);
} }
} }
} }

View File

@ -32,7 +32,6 @@
package io.grpc.internal; package io.grpc.internal;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterators;
import io.grpc.EquivalentAddressGroup; import io.grpc.EquivalentAddressGroup;
import io.grpc.Status; import io.grpc.Status;
@ -44,6 +43,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe; import javax.annotation.concurrent.NotThreadSafe;
@ -62,7 +62,7 @@ public class RoundRobinServerList<T> {
private RoundRobinServerList(TransportManager<T> tm, List<EquivalentAddressGroup> list) { private RoundRobinServerList(TransportManager<T> tm, List<EquivalentAddressGroup> list) {
this.tm = tm; this.tm = tm;
this.list = list; this.list = list;
this.cyclingIter = Iterators.cycle(list); this.cyclingIter = new CycleIterator<EquivalentAddressGroup>(list);
this.requestDroppingTransport = this.requestDroppingTransport =
tm.createFailingTransport(Status.UNAVAILABLE.withDescription("Throttled by LB")); tm.createFailingTransport(Status.UNAVAILABLE.withDescription("Throttled by LB"));
} }
@ -136,4 +136,36 @@ public class RoundRobinServerList<T> {
Collections.unmodifiableList(new ArrayList<EquivalentAddressGroup>(list))); Collections.unmodifiableList(new ArrayList<EquivalentAddressGroup>(list)));
} }
} }
private static final class CycleIterator<T> implements Iterator<T> {
private final List<T> list;
private int index;
public CycleIterator(List<T> 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();
}
}
} }