Skip to content

Commit

Permalink
GH-1504: Clean and Test custom equals() and hashcode() method
Browse files Browse the repository at this point in the history
  • Loading branch information
nkrzykala authored and sbernard31 committed Sep 20, 2024
1 parent 18b8147 commit 70eec7d
Show file tree
Hide file tree
Showing 143 changed files with 2,957 additions and 1,775 deletions.
4 changes: 4 additions & 0 deletions leshan-lwm2m-bsserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ Contributors:
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.leshan.bsserver.BootstrapConfig.ServerSecurity;
Expand Down Expand Up @@ -104,43 +105,27 @@ public Map<String, BootstrapConfig> getAll() {
}

protected static class PskByServer {
public String serverUrl;
public String identity;
public final String serverUrl;
public final String identity;

public PskByServer(String serverUrl, String identity) {
this.serverUrl = serverUrl;
this.identity = identity;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((identity == null) ? 0 : identity.hashCode());
result = prime * result + ((serverUrl == null) ? 0 : serverUrl.hashCode());
return result;
public final boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof PskByServer))
return false;
PskByServer that = (PskByServer) o;
return Objects.equals(serverUrl, that.serverUrl) && Objects.equals(identity, that.identity);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PskByServer other = (PskByServer) obj;
if (identity == null) {
if (other.identity != null)
return false;
} else if (!identity.equals(other.identity))
return false;
if (serverUrl == null) {
if (other.serverUrl != null)
return false;
} else if (!serverUrl.equals(other.serverUrl))
return false;
return true;
public final int hashCode() {
return Objects.hash(serverUrl, identity);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2013-2015 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Natalia Krzykała Orange Polska S.A. - initial implementation
*******************************************************************************/
package org.eclipse.leshan.bsserver;

import org.junit.jupiter.api.Test;

import nl.jqno.equalsverifier.EqualsVerifier;

class InMemoryBootstrapConfigStoreTest {
@Test
public void assertEqualsHashcode() {
EqualsVerifier.forClass(InMemoryBootstrapConfigStore.PskByServer.class).verify();
}
}
4 changes: 4 additions & 0 deletions leshan-lwm2m-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ Contributors:
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.leshan.client.servers;

import java.net.URI;
import java.util.Objects;

import org.eclipse.leshan.core.peer.LwM2mPeer;

Expand All @@ -29,6 +30,22 @@ public class LwM2mServer {
*/
public final static LwM2mServer SYSTEM = new LwM2mServer(null, null, Role.SYSTEM, null);

@Override
public final boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof LwM2mServer))
return false;
LwM2mServer that = (LwM2mServer) o;
return Objects.equals(transportData, that.transportData) && Objects.equals(id, that.id) && role == that.role
&& Objects.equals(uri, that.uri);
}

@Override
public final int hashCode() {
return Objects.hash(transportData, id, role, uri);
}

public enum Role {
/**
* Indicate internal call. Enables the "system" to read protected resources (e.g. resources of the security
Expand Down Expand Up @@ -125,37 +142,4 @@ public String toString() {
return null;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((transportData == null) ? 0 : transportData.hashCode());
result = prime * result + ((role == null) ? 0 : role.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
LwM2mServer other = (LwM2mServer) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (transportData == null) {
if (other.transportData != null)
return false;
} else if (!transportData.equals(other.transportData))
return false;
if (role != other.role)
return false;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2013-2015 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Natalia Krzykała Orange Polska S.A. - initial implementation
*******************************************************************************/
package org.eclipse.leshan.client.servers;

import org.junit.jupiter.api.Test;

import nl.jqno.equalsverifier.EqualsVerifier;

class LwM2mServerTest {
@Test
public void assertEqualsHashcode() {
EqualsVerifier.forClass(LwM2mServer.class).verify();
}
}
78 changes: 37 additions & 41 deletions leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/LwM2m.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*******************************************************************************/
package org.eclipse.leshan.core;

import java.util.Objects;

public interface LwM2m {

/**
* Version of LWM2M specification.
*/

public class LwM2mVersion extends Version {

public static final LwM2mVersion V1_0 = new LwM2mVersion("1.0", true);
Expand Down Expand Up @@ -64,25 +67,24 @@ public static LwM2mVersion lastSupported() {
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + (supported ? 1231 : 1237);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
public boolean equals(Object o) {
if (this == o)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
if (!(o instanceof LwM2mVersion))
return false;
LwM2mVersion other = (LwM2mVersion) obj;
if (supported != other.supported)
if (!super.equals(o))
return false;
return true;
LwM2mVersion that = (LwM2mVersion) o;
return that.canEqual(this) && supported == that.supported;
}

public boolean canEqual(Object o) {
return (o instanceof LwM2mVersion);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), supported);
}
}

Expand Down Expand Up @@ -168,31 +170,6 @@ public static String validate(String version) {
return null;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + major;
result = prime * result + minor;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Version other = (Version) obj;
if (major != other.major)
return false;
if (minor != other.minor)
return false;
return true;
}

@Override
public int compareTo(Version other) {
if (major != other.major)
Expand All @@ -211,6 +188,25 @@ public boolean olderThan(Version version) {
public boolean newerThan(String version) {
return newerThan(new Version(version));
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Version))
return false;
Version that = (Version) o;
return that.canEqual(this) && major == that.major && minor == that.minor;
}

public boolean canEqual(Object o) {
return (o instanceof Version);
}

@Override
public int hashCode() {
return Objects.hash(major, minor);
}
}

/** The default CoAP port for unsecured CoAP communication */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package org.eclipse.leshan.core;

import java.util.Objects;

import org.eclipse.leshan.core.util.Validate;

/**
Expand Down Expand Up @@ -84,8 +86,8 @@ public class ResponseCode {
REQUEST_ENTITY_INCOMPLETE, PRECONDITION_FAILED, REQUEST_ENTITY_TOO_LARGE, UNSUPPORTED_CONTENT_FORMAT,
INTERNAL_SERVER_ERROR };

private int code;
private String name;
private final int code;
private final String name;

public ResponseCode(int code, String name) {
Validate.notNull(name);
Expand Down Expand Up @@ -145,24 +147,17 @@ public String toString() {
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + code;
return result;
public final boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof ResponseCode))
return false;
ResponseCode that = (ResponseCode) o;
return code == that.code;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ResponseCode other = (ResponseCode) obj;
if (code != other.code)
return false;
return true;
public final int hashCode() {
return Objects.hash(code);
}
}
Loading

0 comments on commit 70eec7d

Please sign in to comment.