remove guava Iterables (#6563)
This commit is contained in:
parent
ea864c8c5a
commit
ca89efb620
@ -26,6 +26,7 @@ import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -278,4 +279,39 @@ public final class CollectionUtils {
|
||||
return new LinkedHashSet(Arrays.asList(elements));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return the first element, if the iterator contains multiple elements,
|
||||
* will throw {@code IllegalArgumentException}.
|
||||
* @throws NoSuchElementException if the iterator is empty
|
||||
* @throws IllegalArgumentException if the iterator contains multiple elements.
|
||||
* The state of the iterator is unspecified.
|
||||
*/
|
||||
public static <T> T getOnlyElement(Iterable<T> iterable) {
|
||||
if (iterable == null) {
|
||||
throw new IllegalArgumentException("iterable cannot be null.");
|
||||
}
|
||||
Iterator<T> iterator = iterable.iterator();
|
||||
T first = iterator.next();
|
||||
if (!iterator.hasNext()) {
|
||||
return first;
|
||||
}
|
||||
throw new IllegalArgumentException(buildExceptionMessage(iterator, first));
|
||||
}
|
||||
|
||||
@SuppressWarnings("PMD.UndefineMagicConstantRule")
|
||||
private static <T> String buildExceptionMessage(Iterator<T> iterator, T first) {
|
||||
String msg = "";
|
||||
msg += "expected one element but was: <";
|
||||
msg += first;
|
||||
for (int i = 0; i < 4 && iterator.hasNext(); i++) {
|
||||
msg += ", ";
|
||||
msg += iterator.next();
|
||||
}
|
||||
if (iterator.hasNext()) {
|
||||
msg += ", ...";
|
||||
}
|
||||
msg += '>';
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ package com.alibaba.nacos.common.remote.client;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.common.remote.ConnectionType;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.alibaba.nacos.common.utils.CollectionUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
@ -84,7 +84,7 @@ public class RpcClientFactoryTest {
|
||||
|
||||
RpcClientFactory.destroyClient("notExistClientName");
|
||||
|
||||
Map.Entry<String, RpcClient> element = Iterables.getOnlyElement(RpcClientFactory.getAllClientEntries());
|
||||
Map.Entry<String, RpcClient> element = CollectionUtils.getOnlyElement(RpcClientFactory.getAllClientEntries());
|
||||
Assert.assertEquals("testClient", element.getKey());
|
||||
Assert.assertEquals(rpcClient, element.getValue());
|
||||
verify(rpcClient, times(0)).shutdown();
|
||||
@ -106,7 +106,7 @@ public class RpcClientFactoryTest {
|
||||
|
||||
Assert.assertEquals(Collections.singletonMap("labelKey", "labelValue"), client.labels);
|
||||
Assert.assertEquals(ConnectionType.GRPC, client.getConnectionType());
|
||||
Assert.assertEquals("testClient", Iterables.getOnlyElement(RpcClientFactory.getAllClientEntries()).getKey());
|
||||
Assert.assertEquals("testClient", CollectionUtils.getOnlyElement(RpcClientFactory.getAllClientEntries()).getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -136,7 +136,7 @@ public class RpcClientFactoryTest {
|
||||
|
||||
Assert.assertEquals(Collections.singletonMap("labelKey", "labelValue"), client.labels);
|
||||
Assert.assertEquals(ConnectionType.GRPC, client.getConnectionType());
|
||||
Assert.assertEquals("testClient", Iterables.getOnlyElement(RpcClientFactory.getAllClientEntries()).getKey());
|
||||
Assert.assertEquals("testClient", CollectionUtils.getOnlyElement(RpcClientFactory.getAllClientEntries()).getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -164,4 +164,5 @@ public class RpcClientFactoryTest {
|
||||
Collections.singletonMap("labelKey", "labelValue")
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -23,11 +23,13 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
import java.util.stream.Collectors;
|
||||
@ -335,4 +337,23 @@ public class CollectionUtilsTest {
|
||||
public void testSetNullPointerException() {
|
||||
CollectionUtils.set(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetOnlyElementIllegalArgumentException() {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
CollectionUtils.getOnlyElement(list);
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void testGetOnlyElementNoSuchElementException() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
CollectionUtils.getOnlyElement(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOnly() {
|
||||
List<Integer> list = Arrays.asList(1);
|
||||
int element = CollectionUtils.getOnlyElement(list);
|
||||
Assert.assertEquals(1, element);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user