From 38472e1c07bc48079f4b9766d45e6b64163bd0d4 Mon Sep 17 00:00:00 2001 From: BELUGA BEHR Date: Mon, 25 Dec 2017 16:11:37 +0800 Subject: [PATCH] HBASE-19487 Remove IterablesUtil Class Signed-off-by: Chia-Ping Tsai --- .../apache/hadoop/hbase/KeyValueTestUtil.java | 5 +-- .../org/apache/hadoop/hbase/KeyValueUtil.java | 4 +- .../org/apache/hadoop/hbase/util/Bytes.java | 22 ++++++---- .../hadoop/hbase/util/IterableUtils.java | 44 ------------------- 4 files changed, 17 insertions(+), 58 deletions(-) delete mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/util/IterableUtils.java diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java index 3d9b4f92ea1..efea7405b1d 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueTestUtil.java @@ -22,11 +22,10 @@ import java.nio.ByteBuffer; import java.util.Collection; import java.util.List; +import org.apache.commons.collections4.IterableUtils; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.IterableUtils; import org.apache.hadoop.hbase.util.Strings; import org.apache.yetus.audience.InterfaceAudience; - import org.apache.hadoop.hbase.shaded.com.google.common.collect.Lists; @InterfaceAudience.Private @@ -64,7 +63,7 @@ public class KeyValueTestUtil { boolean includeMemstoreTS) { int totalBytes = KeyValueUtil.totalLengthWithMvccVersion(kvs, includeMemstoreTS); ByteBuffer bb = ByteBuffer.allocate(totalBytes); - for (KeyValue kv : IterableUtils.nullSafe(kvs)) { + for (KeyValue kv : IterableUtils.emptyIfNull(kvs)) { KeyValueUtil.appendToByteBuffer(bb, kv, includeMemstoreTS); } bb.rewind(); diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java index 60e404925b7..9d8a6084ac4 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java @@ -28,11 +28,11 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; +import org.apache.commons.collections4.IterableUtils; import org.apache.hadoop.hbase.KeyValue.Type; import org.apache.hadoop.hbase.io.util.StreamUtils; import org.apache.hadoop.hbase.util.ByteBufferUtils; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.IterableUtils; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.WritableUtils; import org.apache.yetus.audience.InterfaceAudience; @@ -90,7 +90,7 @@ public class KeyValueUtil { public static int totalLengthWithMvccVersion(final Iterable kvs, final boolean includeMvccVersion) { int length = 0; - for (KeyValue kv : IterableUtils.nullSafe(kvs)) { + for (KeyValue kv : IterableUtils.emptyIfNull(kvs)) { length += lengthWithMvccVersion(kv, includeMvccVersion); } return length; diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java index 159a7905b96..12e478830b2 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java @@ -30,8 +30,10 @@ import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -47,7 +49,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.hbase.shaded.com.google.common.annotations.VisibleForTesting; -import org.apache.hadoop.hbase.shaded.com.google.common.collect.Lists; import com.google.protobuf.ByteString; @@ -2339,21 +2340,24 @@ public class Bytes implements Comparable { } public static boolean isSorted(Collection arrays) { - byte[] previous = new byte[0]; - for (byte[] array : IterableUtils.nullSafe(arrays)) { - if (Bytes.compareTo(previous, array) > 0) { - return false; + if (!CollectionUtils.isEmpty(arrays)) { + byte[] previous = new byte[0]; + for (byte[] array : arrays) { + if (Bytes.compareTo(previous, array) > 0) { + return false; + } + previous = array; } - previous = array; } return true; } public static List getUtf8ByteArrays(List strings) { - List byteArrays = Lists.newArrayListWithCapacity(CollectionUtils.nullSafeSize(strings)); - for (String s : IterableUtils.nullSafe(strings)) { - byteArrays.add(Bytes.toBytes(s)); + if (CollectionUtils.isEmpty(strings)) { + return Collections.emptyList(); } + List byteArrays = new ArrayList<>(strings.size()); + strings.forEach(s -> byteArrays.add(Bytes.toBytes(s))); return byteArrays; } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/IterableUtils.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/IterableUtils.java deleted file mode 100644 index e0bca84be24..00000000000 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/IterableUtils.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.hbase.util; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.apache.yetus.audience.InterfaceAudience; - -/** - * Utility methods for Iterable including null-safe handlers. - */ -@InterfaceAudience.Private -public class IterableUtils { - - private static final List EMPTY_LIST = Collections - .unmodifiableList(new ArrayList<>(0)); - - @SuppressWarnings("unchecked") - public static Iterable nullSafe(Iterable in) { - if (in == null) { - return (List) EMPTY_LIST; - } - return in; - } - -}