You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by cp...@apache.org on 2020/02/18 16:10:57 UTC

[lucene-solr] 03/03: SOLR-13041: Add hashCode for autoscaling.Condition to accompany the already present equals.

This is an automated email from the ASF dual-hosted git repository.

cpoerschke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git

commit 003303a9cc8e1d5471448b538b611984b5e669a7
Author: Christine Poerschke <cp...@apache.org>
AuthorDate: Tue Feb 18 14:47:44 2020 +0000

    SOLR-13041: Add hashCode for autoscaling.Condition to accompany the already present equals.
    
    (Zsolt Gyulavari via Christine Poerschke)
---
 solr/CHANGES.txt                                   |  3 +
 .../client/solrj/cloud/autoscaling/Condition.java  |  5 ++
 .../solrj/cloud/autoscaling/ConditionTest.java     | 80 ++++++++++++++++++++++
 3 files changed, 88 insertions(+)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 5472bfe..c5b1446 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -156,6 +156,9 @@ Bug Fixes
 * SOLR-14058: Fix IndexOutOfBoundsException in PeerSync that can prevent nodes from recovering
   under certain circumstances. (yonik)
 
+* SOLR-13041: Add hashCode for autoscaling.Condition to accompany the already present equals.
+  (Zsolt Gyulavari via Christine Poerschke)
+
 Other Changes
 ---------------------
 
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Condition.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Condition.java
index 5b60ef0..dd59087 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Condition.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Condition.java
@@ -77,6 +77,11 @@ public class Condition implements MapWriter {
   }
 
   @Override
+  public int hashCode() {
+    return Objects.hash(name, val, op);
+  }
+
+  @Override
   public boolean equals(Object that) {
     if (that instanceof Condition) {
       Condition c = (Condition) that;
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/ConditionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/ConditionTest.java
new file mode 100644
index 0000000..2b455f1
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/ConditionTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.solr.client.solrj.cloud.autoscaling;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class ConditionTest {
+  @Test
+  public void testEqualsHashCode() {
+    assertHashMatchesEquals("equals should match hash (names are equal)",
+        new Condition("node", null, null, null, null),
+        new Condition("node", null, null, null, null));
+    assertHashMatchesEquals("equals should match hash (names aren't equal)",
+        new Condition("node", null, null, null, null),
+        new Condition("host", null, null, null, null));
+    assertHashMatchesEquals("equals should match hash (values are equal)",
+        new Condition("node", "localhost", null, null, null),
+        new Condition("node", "localhost", null, null, null));
+    assertHashMatchesEquals("equals should match hash (values aren't equal)",
+        new Condition("node", "localhost", null, null, null),
+        new Condition("node", "lucene.apache.org", null, null, null));
+    assertHashMatchesEquals("equals should match hash (operands are equal)",
+        new Condition("node", null, Operand.EQUAL, null, null),
+        new Condition("node", null, Operand.EQUAL, null, null));
+    assertHashMatchesEquals("equals should match hash (operands aren't equal)",
+        new Condition("node", null, Operand.EQUAL, null, null),
+        new Condition("node", null, Operand.NOT_EQUAL, null, null));
+
+    Condition condition = new Condition("host", "localhost", Operand.EQUAL, null, null);
+    assertHashMatchesEquals("equals should match hash when compared to self", condition, condition);
+    assertTrue("equals should be true when compared to self", condition.equals(condition));
+  }
+
+  @Test
+  public void testEqualsInvertible() {
+    assertEqualsInvertible("equals should be invertible (names are equal)",
+        new Condition("node", null, null, null, null),
+        new Condition("node", null, null, null, null));
+    assertEqualsInvertible("equals should be invertible (names aren't equal)",
+        new Condition("node", null, null, null, null),
+        new Condition("host", null, null, null, null));
+    assertEqualsInvertible("equals should be invertible (values are equal)",
+        new Condition("node", "localhost", null, null, null),
+        new Condition("node", "localhost", null, null, null));
+    assertEqualsInvertible("equals should be invertible (values aren't equal)",
+        new Condition("node", "localhost", null, null, null),
+        new Condition("node", "lucene.apache.org", null, null, null));
+    assertEqualsInvertible("equals should be invertible (operands are equal)",
+        new Condition("node", null, Operand.EQUAL, null, null),
+        new Condition("node", null, Operand.EQUAL, null, null));
+    assertEqualsInvertible("equals should be invertible (operands aren't equal)",
+        new Condition("node", null, Operand.EQUAL, null, null),
+        new Condition("node", null, Operand.NOT_EQUAL, null, null));
+  }
+
+  private void assertEqualsInvertible(String message, Condition a, Condition b) {
+    assertEquals(message, a != null && a.equals(b), b != null && b.equals(a));
+  }
+
+  private void assertHashMatchesEquals(String message, Condition a, Condition b) {
+    assertTrue(message, (a.hashCode() == b.hashCode()) || (!a.equals(b) && !b.equals(a)));
+  }
+}