Skip to content

Commit e9d0935

Browse files
committed
#2310 Add CollectionUtils to common module
1 parent 3f71bd1 commit e9d0935

File tree

4 files changed

+115
-7
lines changed

4 files changed

+115
-7
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.navercorp.pinpoint.common.util;
19+
20+
import java.util.Collection;
21+
22+
/**
23+
* @author Woonduk Kang(emeroad)
24+
*/
25+
public final class CollectionUtils {
26+
27+
private CollectionUtils() {
28+
}
29+
30+
public static <T> int nullSafeSize(Collection<T> collection) {
31+
return nullSafeSize(collection, 0);
32+
}
33+
34+
public static <T> int nullSafeSize(Collection<T> collection, int nullValue) {
35+
if (collection == null) {
36+
return nullValue;
37+
}
38+
return collection.size();
39+
}
40+
41+
public static <T> boolean isEmpty(Collection<T> collection) {
42+
return collection == null || collection.isEmpty();
43+
}
44+
45+
public static <T> boolean isNotEmpty(Collection<T> collection) {
46+
return !isEmpty(collection);
47+
}
48+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2016 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.navercorp.pinpoint.common.util;
19+
20+
import com.google.common.collect.Lists;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
import java.util.Collections;
25+
26+
27+
/**
28+
* @author Woonduk Kang(emeroad)
29+
*/
30+
public class CollectionUtilsTest {
31+
32+
@Test
33+
public void nullSafeSize() throws Exception {
34+
Assert.assertEquals(CollectionUtils.nullSafeSize(Lists.newArrayList(123)), 1);
35+
36+
Assert.assertEquals(CollectionUtils.nullSafeSize(Collections.emptyList()), 0);
37+
Assert.assertEquals(CollectionUtils.nullSafeSize(null), 0);
38+
}
39+
40+
@Test
41+
public void nullSafeSize_nullValue() throws Exception {
42+
Assert.assertEquals(CollectionUtils.nullSafeSize(null, -1), -1);
43+
}
44+
45+
@Test
46+
public void isEmpty() throws Exception {
47+
Assert.assertTrue(CollectionUtils.isEmpty(null));
48+
Assert.assertTrue(CollectionUtils.isEmpty(Collections.emptyList()));
49+
}
50+
51+
@Test
52+
public void isNotEmpty() throws Exception {
53+
Assert.assertFalse(CollectionUtils.isNotEmpty(Collections.emptyList()));
54+
Assert.assertFalse(CollectionUtils.isNotEmpty(null));
55+
}
56+
57+
}

rpc/src/main/java/com/navercorp/pinpoint/rpc/util/ListUtils.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.navercorp.pinpoint.rpc.util;
1818

19+
import com.navercorp.pinpoint.common.util.CollectionUtils;
20+
21+
import java.util.Collection;
1922
import java.util.List;
2023

2124
/**
@@ -26,12 +29,12 @@ public final class ListUtils {
2629
private ListUtils() {
2730
}
2831

32+
/**
33+
* @deprecated Since 1.6.1. Use {@link CollectionUtils#isEmpty(Collection)}
34+
*/
35+
@Deprecated
2936
public static boolean isEmpty(List list) {
30-
if (list == null || list.isEmpty()) {
31-
return true;
32-
}
33-
34-
return false;
37+
return CollectionUtils.isEmpty(list);
3538
}
3639

3740
public static <V> boolean addIfValueNotNull(List<V> list, V value) {

web/src/main/java/com/navercorp/pinpoint/web/dao/hbase/HbaseApplicationTraceIndexDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.navercorp.pinpoint.common.server.util.SpanUtils;
2929
import com.navercorp.pinpoint.common.util.TimeUtils;
3030
import com.navercorp.pinpoint.common.util.TransactionId;
31-
import com.navercorp.pinpoint.rpc.util.ListUtils;
3231
import com.navercorp.pinpoint.web.dao.ApplicationTraceIndexDao;
3332
import com.navercorp.pinpoint.web.mapper.TraceIndexScatterMapper2;
3433
import com.navercorp.pinpoint.web.mapper.TraceIndexScatterMapper3;
@@ -40,6 +39,7 @@
4039
import com.navercorp.pinpoint.web.vo.SelectedScatterArea;
4140
import com.navercorp.pinpoint.web.vo.scatter.Dot;
4241
import com.sematext.hbase.wd.AbstractRowKeyDistributor;
42+
import org.apache.commons.collections.CollectionUtils;
4343
import org.apache.hadoop.hbase.Cell;
4444
import org.apache.hadoop.hbase.CellUtil;
4545
import org.apache.hadoop.hbase.client.Result;
@@ -296,7 +296,7 @@ public ScatterData scanTraceScatterData(String applicationName, Range range, int
296296
TraceIndexScatterMapper3 mapper = new TraceIndexScatterMapper3(range.getFrom(), range.getTo(), xGroupUnit, yGroupUnit);
297297
List<ScatterData> dotGroupList = hbaseOperations2.findParallel(HBaseTables.APPLICATION_TRACE_INDEX, scan, traceIdRowKeyDistributor, limit, mapper, APPLICATION_TRACE_INDEX_NUM_PARTITIONS);
298298

299-
if (ListUtils.isEmpty(dotGroupList)) {
299+
if (CollectionUtils.isEmpty(dotGroupList)) {
300300
return new ScatterData(range.getFrom(), range.getTo(), xGroupUnit, yGroupUnit);
301301
} else {
302302
ScatterData firstScatterData = dotGroupList.get(0);

0 commit comments

Comments
 (0)