Replace LangUtils with Objects
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java
index dc3179d..28ae747 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java
@@ -35,12 +35,12 @@
import java.nio.charset.CoderResult;
import java.nio.charset.StandardCharsets;
import java.util.List;
+import java.util.Objects;
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.ByteArrayBuffer;
-import org.apache.hc.core5.util.LangUtils;
/**
* HPACK encoder.
@@ -252,7 +252,7 @@
}
for (int i = 0; i < entries.size(); i++) {
final HPackEntry entry = entries.get(i);
- if (LangUtils.equals(value, entry.getHeader().getValue())) {
+ if (Objects.equals(value, entry.getHeader().getValue())) {
return entry.getIndex();
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java b/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
index a58f440..bcd5e1f 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
@@ -32,6 +32,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
+import java.util.Objects;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
@@ -39,7 +40,6 @@
import org.apache.hc.core5.net.Ports;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.LangUtils;
import org.apache.hc.core5.util.TextUtils;
/**
@@ -375,7 +375,7 @@
return this.lcHostname.equals(that.lcHostname)
&& this.port == that.port
&& this.schemeName.equals(that.schemeName)
- && LangUtils.equals(this.address, that.address);
+ && Objects.equals(this.address, that.address);
}
return false;
}
@@ -385,12 +385,7 @@
*/
@Override
public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, this.lcHostname);
- hash = LangUtils.hashCode(hash, this.port);
- hash = LangUtils.hashCode(hash, this.schemeName);
- hash = LangUtils.hashCode(hash, address);
- return hash;
+ return Objects.hash(lcHostname, port, schemeName, address);
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicNameValuePair.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicNameValuePair.java
index 28e6407..fd0eb35 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicNameValuePair.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicNameValuePair.java
@@ -29,12 +29,12 @@
import java.io.Serializable;
import java.util.Locale;
+import java.util.Objects;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.LangUtils;
/**
* Basic implementation of {@link NameValuePair}.
@@ -93,17 +93,14 @@
}
if (obj instanceof BasicNameValuePair) {
final BasicNameValuePair that = (BasicNameValuePair) obj;
- return this.name.equalsIgnoreCase(that.name) && LangUtils.equals(this.value, that.value);
+ return this.name.equalsIgnoreCase(that.name) && Objects.equals(this.value, that.value);
}
return false;
}
@Override
public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, this.name.toLowerCase(Locale.ROOT));
- hash = LangUtils.hashCode(hash, this.value);
- return hash;
+ return Objects.hash(name.toLowerCase(Locale.ROOT), value);
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java
index 9cf09fb..e94eeee 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java
@@ -33,12 +33,12 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.Objects;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.MessageHeaders;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.util.CharArrayBuffer;
-import org.apache.hc.core5.util.LangUtils;
/**
* A class for combining a set of headers. This class allows for multiple headers with the same name
@@ -104,7 +104,7 @@
private boolean headerEquals(final Header header1, final Header header2) {
return header2 == header1 || header2.getName().equalsIgnoreCase(header1.getName())
- && LangUtils.equals(header1.getValue(), header2.getValue());
+ && Objects.equals(header1.getValue(), header2.getValue());
}
/**
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java b/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java
index f03b790..7a0b64f 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java
@@ -29,11 +29,11 @@
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.Locale;
+import java.util.Objects;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.LangUtils;
import org.apache.hc.core5.util.TextUtils;
/**
@@ -100,10 +100,7 @@
@Override
public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, this.lcName);
- hash = LangUtils.hashCode(hash, this.port);
- return hash;
+ return Objects.hash(lcName, port);
}
@Override
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java b/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
index 01feb0f..2d674f9 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
@@ -30,11 +30,11 @@
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.Locale;
+import java.util.Objects;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.LangUtils;
import org.apache.hc.core5.util.TextUtils;
/**
@@ -159,8 +159,8 @@
}
if (obj instanceof URIAuthority) {
final URIAuthority that = (URIAuthority) obj;
- return LangUtils.equals(this.userInfo, that.userInfo) &&
- LangUtils.equals(this.hostname, that.hostname) &&
+ return Objects.equals(this.userInfo, that.userInfo) &&
+ Objects.equals(this.hostname, that.hostname) &&
this.port == that.port;
}
return false;
@@ -168,11 +168,7 @@
@Override
public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, userInfo);
- hash = LangUtils.hashCode(hash, hostname);
- hash = LangUtils.hashCode(hash, port);
- return hash;
+ return Objects.hash(userInfo, hostname, port);
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java
index 82d2119..3aa6094 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java
@@ -29,6 +29,7 @@
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
@@ -52,7 +53,6 @@
import org.apache.hc.core5.util.Asserts;
import org.apache.hc.core5.util.Deadline;
import org.apache.hc.core5.util.DeadlineTimeoutException;
-import org.apache.hc.core5.util.LangUtils;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
@@ -452,7 +452,7 @@
if (entry.getExpiryDeadline().isExpired()) {
entry.discardConnection(CloseMode.GRACEFUL);
}
- if (!LangUtils.equals(entry.getState(), state)) {
+ if (!Objects.equals(entry.getState(), state)) {
entry.discardConnection(CloseMode.GRACEFUL);
}
return entry;
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
index dfd5b36..0765e56 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
@@ -32,6 +32,7 @@
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Future;
@@ -50,7 +51,6 @@
import org.apache.hc.core5.util.Asserts;
import org.apache.hc.core5.util.Deadline;
import org.apache.hc.core5.util.DeadlineTimeoutException;
-import org.apache.hc.core5.util.LangUtils;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
@@ -517,7 +517,7 @@
final PerRoutePool<T, C> pool = getPool(route);
int pendingCount = 0;
for (final LeaseRequest<T, C> request: leasingRequests) {
- if (LangUtils.equals(route, request.getRoute())) {
+ if (Objects.equals(route, request.getRoute())) {
pendingCount++;
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/LangUtils.java b/httpcore5/src/main/java/org/apache/hc/core5/util/LangUtils.java
deleted file mode 100644
index f38d32d..0000000
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/LangUtils.java
+++ /dev/null
@@ -1,98 +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.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.hc.core5.util;
-
-/**
- * A set of utility methods to help produce consistent
- * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods.
- *
- *
- * @since 4.0
- */
-public final class LangUtils {
-
- public static final int HASH_SEED = 17;
- public static final int HASH_OFFSET = 37;
-
- /** Disabled default constructor. */
- private LangUtils() {
- }
-
- public static int hashCode(final int seed, final int hashcode) {
- return seed * HASH_OFFSET + hashcode;
- }
-
- public static int hashCode(final int seed, final boolean b) {
- return hashCode(seed, b ? 1 : 0);
- }
-
- public static int hashCode(final int seed, final Object obj) {
- return hashCode(seed, obj != null ? obj.hashCode() : 0);
- }
-
- /**
- * Check if two objects are equal.
- *
- * @param obj1 first object to compare, may be {@code null}
- * @param obj2 second object to compare, may be {@code null}
- * @return {@code true} if the objects are equal or both null
- */
- public static boolean equals(final Object obj1, final Object obj2) {
- return obj1 == null ? obj2 == null : obj1.equals(obj2);
- }
-
- /**
- * Check if two object arrays are equal.
- * <ul>
- * <li>If both parameters are null, return {@code true}</li>
- * <li>If one parameter is null, return {@code false}</li>
- * <li>If the array lengths are different, return {@code false}</li>
- * <li>Compare array elements using .equals(); return {@code false} if any comparisons fail.</li>
- * <li>Return {@code true}</li>
- * </ul>
- *
- * @param a1 first array to compare, may be {@code null}
- * @param a2 second array to compare, may be {@code null}
- * @return {@code true} if the arrays are equal or both null
- */
- public static boolean equals(final Object[] a1, final Object[] a2) {
- if (a1 == null) {
- return a2 == null;
- }
- if (a2 != null && a1.length == a2.length) {
- for (int i = 0; i < a1.length; i++) {
- if (!equals(a1[i], a2[i])) {
- return false;
- }
- }
- return true;
- }
- return false;
- }
-
-}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java b/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
index 576d875..68c0539 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
@@ -30,6 +30,7 @@
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
+import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.apache.hc.core5.annotation.Contract;
@@ -229,7 +230,7 @@
}
if (obj instanceof TimeValue) {
final TimeValue that = (TimeValue) obj;
- return this.duration == that.duration && LangUtils.equals(this.timeUnit, that.timeUnit);
+ return this.duration == that.duration && Objects.equals(this.timeUnit, that.timeUnit);
}
return false;
}
@@ -273,10 +274,7 @@
@Override
public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, duration);
- hash = LangUtils.hashCode(hash, timeUnit);
- return hash;
+ return Objects.hash(duration, timeUnit);
}
public TimeValue min(final TimeValue other) {
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestLangUtils.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestLangUtils.java
deleted file mode 100644
index b9aa571..0000000
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestLangUtils.java
+++ /dev/null
@@ -1,88 +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.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.hc.core5.util;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * Unit tests for {@link LangUtils}.
- *
- */
-public class TestLangUtils {
-
- @Test
- public void testBasicHash() {
- final Integer i = Integer.valueOf(1234);
- final int h1 = LangUtils.hashCode(LangUtils.HASH_SEED, i.hashCode());
- final int h2 = LangUtils.hashCode(LangUtils.HASH_SEED, i);
- Assert.assertTrue(h1 == h2);
- }
-
- @Test
- public void testNullObjectHash() {
- final int h1 = LangUtils.hashCode(LangUtils.HASH_SEED, null);
- final int h2 = LangUtils.hashCode(LangUtils.HASH_SEED, 0);
- Assert.assertTrue(h1 == h2);
- }
-
- @Test
- public void testBooleanHash() {
- final int h1 = LangUtils.hashCode(LangUtils.HASH_SEED, true);
- final int h2 = LangUtils.hashCode(LangUtils.HASH_SEED, false);
- final int h3 = LangUtils.hashCode(LangUtils.HASH_SEED, true);
- final int h4 = LangUtils.hashCode(LangUtils.HASH_SEED, false);
- Assert.assertTrue(h1 != h2);
- Assert.assertTrue(h1 == h3);
- Assert.assertTrue(h2 == h4);
- }
-
- @Test
- public void testBasicEquality() {
- Assert.assertTrue(LangUtils.equals(null, null));
- Assert.assertFalse(LangUtils.equals(null, "abc"));
- Assert.assertFalse(LangUtils.equals("abc", null));
- Assert.assertTrue(LangUtils.equals("abc", "abc"));
- }
-
- @Test
- public void testArrayEquals() {
- Assert.assertFalse(LangUtils.equals(null, new Object[] {}));
- Assert.assertFalse(LangUtils.equals(new Object[] {}, null));
- Assert.assertTrue(LangUtils.equals(new Object[] {}, new Object[] {}));
- Assert.assertFalse(LangUtils.equals(
- new Object[] {Integer.valueOf(1), Integer.valueOf(2)},
- new Object[] {Integer.valueOf(1)}));
- Assert.assertFalse(LangUtils.equals(
- new Object[] {Integer.valueOf(1), Integer.valueOf(2)},
- new Object[] {Integer.valueOf(1), Integer.valueOf(3)}));
- Assert.assertTrue(LangUtils.equals(
- new Object[] {Integer.valueOf(1), Integer.valueOf(2)},
- new Object[] {Integer.valueOf(1), Integer.valueOf(2)}));
- }
-}