You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/12/17 01:55:25 UTC
[james-project] 04/24: [Refactoring] NamespaceResponse: simplify &
test equals & hashCode
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 464fcbf2b92c0933b58d0f4f274f88aa4e92f1fb
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Dec 13 06:54:38 2019 +0100
[Refactoring] NamespaceResponse: simplify & test equals & hashCode
---
.../imap/message/response/NamespaceResponse.java | 97 +++++-----------------
.../message/response/NamespaceResponseTest.java | 38 +++++++++
2 files changed, 61 insertions(+), 74 deletions(-)
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/message/response/NamespaceResponse.java b/protocols/imap/src/main/java/org/apache/james/imap/message/response/NamespaceResponse.java
index 274e078..92191f9 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/message/response/NamespaceResponse.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/message/response/NamespaceResponse.java
@@ -19,6 +19,7 @@
package org.apache.james.imap.message.response;
import java.util.List;
+import java.util.Objects;
import org.apache.james.imap.api.message.response.ImapResponseMessage;
@@ -28,13 +29,10 @@ import org.apache.james.imap.api.message.response.ImapResponseMessage;
public class NamespaceResponse implements ImapResponseMessage {
private final List<Namespace> personal;
-
private final List<Namespace> users;
-
private final List<Namespace> shared;
public NamespaceResponse(List<Namespace> personal, List<Namespace> users, List<Namespace> shared) {
- super();
this.personal = personal;
this.users = users;
this.shared = shared;
@@ -72,11 +70,9 @@ public class NamespaceResponse implements ImapResponseMessage {
*/
public static final class Namespace {
private final String prefix;
-
private final char delimiter;
public Namespace(String prefix, char delimiter) {
- super();
this.prefix = prefix;
this.delimiter = delimiter;
}
@@ -100,37 +96,19 @@ public class NamespaceResponse implements ImapResponseMessage {
}
@Override
- public int hashCode() {
- final int PRIME = 31;
- int result = 1;
- result = PRIME * result + delimiter;
- result = PRIME * result + ((prefix == null) ? 0 : prefix.hashCode());
- return result;
+ public final boolean equals(Object o) {
+ if (o instanceof Namespace) {
+ Namespace namespace = (Namespace) o;
+
+ return Objects.equals(this.delimiter, namespace.delimiter)
+ && Objects.equals(this.prefix, namespace.prefix);
+ }
+ return false;
}
@Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final Namespace other = (Namespace) obj;
- if (delimiter != other.delimiter) {
- return false;
- }
- if (prefix == null) {
- if (other.prefix != null) {
- return false;
- }
- } else if (!prefix.equals(other.prefix)) {
- return false;
- }
- return true;
+ public final int hashCode() {
+ return Objects.hash(prefix, delimiter);
}
@Override
@@ -140,49 +118,20 @@ public class NamespaceResponse implements ImapResponseMessage {
}
@Override
- public int hashCode() {
- final int PRIME = 31;
- int result = 1;
- result = PRIME * result + ((personal == null) ? 0 : personal.hashCode());
- result = PRIME * result + ((shared == null) ? 0 : shared.hashCode());
- result = PRIME * result + ((users == null) ? 0 : users.hashCode());
- return result;
+ public final boolean equals(Object o) {
+ if (o instanceof NamespaceResponse) {
+ NamespaceResponse that = (NamespaceResponse) o;
+
+ return Objects.equals(this.personal, that.personal)
+ && Objects.equals(this.users, that.users)
+ && Objects.equals(this.shared, that.shared);
+ }
+ return false;
}
@Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final NamespaceResponse other = (NamespaceResponse) obj;
- if (personal == null) {
- if (other.personal != null) {
- return false;
- }
- } else if (!personal.equals(other.personal)) {
- return false;
- }
- if (shared == null) {
- if (other.shared != null) {
- return false;
- }
- } else if (!shared.equals(other.shared)) {
- return false;
- }
- if (users == null) {
- if (other.users != null) {
- return false;
- }
- } else if (!users.equals(other.users)) {
- return false;
- }
- return true;
+ public final int hashCode() {
+ return Objects.hash(personal, users, shared);
}
/**
@@ -191,6 +140,6 @@ public class NamespaceResponse implements ImapResponseMessage {
* @return a <code>String</code> representation of this object.
*/
public String toString() {
- return "NamespaceResponse [" + "personal = " + this.personal + " " + "users = " + this.users + " " + "shared = " + this.shared + " " + " ]";
+ return "NamespaceResponse [personal = " + this.personal + " users = " + this.users + " shared = " + this.shared + " ]";
}
}
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/message/response/NamespaceResponseTest.java b/protocols/imap/src/test/java/org/apache/james/imap/message/response/NamespaceResponseTest.java
new file mode 100644
index 0000000..85179b8
--- /dev/null
+++ b/protocols/imap/src/test/java/org/apache/james/imap/message/response/NamespaceResponseTest.java
@@ -0,0 +1,38 @@
+/****************************************************************
+ * 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.james.imap.message.response;
+
+import org.junit.jupiter.api.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+class NamespaceResponseTest {
+ @Test
+ void namespaceResponseShouldMatchBeanContract() {
+ EqualsVerifier.forClass(NamespaceResponse.class)
+ .verify();
+ }
+
+ @Test
+ void namespaceShouldMatchBeanContract() {
+ EqualsVerifier.forClass(NamespaceResponse.Namespace.class)
+ .verify();
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org