Skip to content

Commit

Permalink
[Reduce dependence]>[Maps] Replace "google.common.collect.*" API with…
Browse files Browse the repository at this point in the history
… jdk API (alibaba#6443)
  • Loading branch information
ZZQ001010 authored Jul 29, 2021
1 parent fab5ae5 commit 9beaacb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.alibaba.nacos.common.utils;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.internal.util.collections.Sets;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
Expand Down Expand Up @@ -56,20 +56,32 @@ public void testGetList3() {

@Test(expected = IndexOutOfBoundsException.class)
public void testGetMap1() {
CollectionUtils.get(ImmutableMap.of("key1", "value1"), -1);
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
CollectionUtils.get(map, -1);
}

@Test(expected = IndexOutOfBoundsException.class)
public void testGetMap2() {
CollectionUtils.get(ImmutableMap.of("key1", "value1"), 1);
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
CollectionUtils.get(map, -1);
CollectionUtils.get(map, 1);
}

@Test
public void testGetMap3() {
Assert.assertEquals(Maps.immutableEntry("key", "value"),
CollectionUtils.get(ImmutableMap.of("key", "value"), 0));
Assert.assertEquals(Maps.immutableEntry("key2", "value2"),
CollectionUtils.get(ImmutableMap.of("key1", "value1", "key2", "value2"), 1));
Map<String, String> map1 = new HashMap(1);
Map<String, String> map2 = new HashMap(2);
map1.put("key", "value");
map2.put("key1", "value1");
map2.put("key2", "value2");
Iterator<Map.Entry<String, String>> iter = map1.entrySet().iterator();
Assert.assertEquals(iter.next(), CollectionUtils.get(map1, 0));
Iterator<Map.Entry<String, String>> iter2 = map2.entrySet().iterator();
iter2.next();
Map.Entry<String, String> second = iter2.next();
Assert.assertEquals(second, CollectionUtils.get(map2, 1));
}

@Test(expected = IndexOutOfBoundsException.class)
Expand Down Expand Up @@ -172,12 +184,16 @@ public void testSize() {
Assert.assertEquals(0, CollectionUtils.size(Collections.emptyList()));
Assert.assertEquals(1, CollectionUtils.size(Collections.singletonList("")));
Assert.assertEquals(10, CollectionUtils.size(IntStream.range(0, 10).boxed().collect(Collectors.toList())));

// map
Assert.assertEquals(1, CollectionUtils.size(ImmutableMap.of("key", "value")));
Assert.assertEquals(2, CollectionUtils.size(ImmutableMap.of("key1", "value1", "key2", "value2")));
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
Assert.assertEquals(1, CollectionUtils.size(map));
map.put("key2", "value2");
Assert.assertEquals(2, CollectionUtils.size(map));
map.put("key3", "value3");
Assert.assertEquals(3,
CollectionUtils.size(ImmutableMap.of("key1", "value1", "key2", "value2", "key3", "value3")));
CollectionUtils.size(map));

// array
Assert.assertEquals(1, CollectionUtils.size(new Object[] {"1"}));
Expand Down Expand Up @@ -217,8 +233,11 @@ public void testSizeIsEmpty() {
Assert.assertFalse(CollectionUtils.sizeIsEmpty(Collections.singletonList("")));

// map
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
Assert.assertTrue(CollectionUtils.sizeIsEmpty(Collections.emptyMap()));
Assert.assertFalse(CollectionUtils.sizeIsEmpty(ImmutableMap.of("key1", "value1", "key2", "value2")));
Assert.assertFalse(CollectionUtils.sizeIsEmpty(map));

// array
Assert.assertTrue(CollectionUtils.sizeIsEmpty(new Object[] {}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.alibaba.nacos.naming.misc.Synchronizer;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.google.common.collect.Maps;
import com.alibaba.nacos.common.utils.StringUtils;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -176,7 +175,7 @@ public void run() {
final String path =
UtilsAndCommons.NACOS_NAMING_OPERATOR_CONTEXT + UtilsAndCommons.NACOS_NAMING_CLUSTER_CONTEXT
+ "/state";
final Map<String, String> params = Maps.newHashMapWithExpectedSize(2);
final Map<String, String> params = new HashMap(2);
final String server = target.getAddress();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.alibaba.nacos.naming.misc.Message;
import com.alibaba.nacos.naming.misc.Synchronizer;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -419,7 +418,7 @@ public void testGetPagedService() throws NacosException {

@Test
public void testSnowflakeInstanceId() throws Exception {
Map<String, String> metaData = Maps.newHashMap();
Map<String, String> metaData = new HashMap<>();
metaData.put(PreservedMetadataKeys.INSTANCE_ID_GENERATOR, Constants.SNOWFLAKE_INSTANCE_ID_GENERATOR);

instance.setMetadata(metaData);
Expand Down

0 comments on commit 9beaacb

Please sign in to comment.