Skip to content

Commit

Permalink
For #150 - updated equals() and hashCode() methods of entities classe…
Browse files Browse the repository at this point in the history
…s so they will work properly with hibernate proxies, changed collections in 'User' entity from Lists (bag was actually used) to Set
  • Loading branch information
vitalidze committed Apr 27, 2015
1 parent 1e881ec commit e16a11f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/traccar/web/shared/model/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ public void setIconType(DeviceIconType iconType) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o == null || !(o instanceof Device)) return false;

Device device = (Device) o;

if (uniqueId != null ? !uniqueId.equals(device.uniqueId) : device.uniqueId != null) return false;
if (getUniqueId() != null ? !getUniqueId().equals(device.getUniqueId()) : device.getUniqueId() != null) return false;

return true;
}

@Override
public int hashCode() {
return uniqueId != null ? uniqueId.hashCode() : 0;
return getUniqueId() != null ? getUniqueId().hashCode() : 0;
}
}
22 changes: 11 additions & 11 deletions src/main/java/org/traccar/web/shared/model/DeviceEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,26 @@ public void setNotificationSent(boolean notificationSent) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o == null || !(o instanceof DeviceEvent)) return false;

DeviceEvent that = (DeviceEvent) o;

if (device != null ? !device.equals(that.device) : that.device != null) return false;
if (geoFence != null ? !geoFence.equals(that.geoFence) : that.geoFence != null) return false;
if (position != null ? !position.equals(that.position) : that.position != null) return false;
if (!time.equals(that.time)) return false;
if (type != that.type) return false;
if (getDevice() != null ? !getDevice().equals(that.getDevice()) : that.getDevice() != null) return false;
if (getGeoFence() != null ? !getGeoFence().equals(that.getGeoFence()) : that.getGeoFence() != null) return false;
if (getPosition() != null ? !getPosition().equals(that.getPosition()) : that.getPosition() != null) return false;
if (!getTime().equals(that.getTime())) return false;
if (getType() != that.getType()) return false;

return true;
}

@Override
public int hashCode() {
int result = time.hashCode();
result = 31 * result + (device != null ? device.hashCode() : 0);
result = 31 * result + type.hashCode();
result = 31 * result + (position != null ? position.hashCode() : 0);
result = 31 * result + (geoFence != null ? geoFence.hashCode() : 0);
int result = getTime().hashCode();
result = 31 * result + (getDevice() != null ? getDevice().hashCode() : 0);
result = 31 * result + getType().hashCode();
result = 31 * result + (getPosition() != null ? getPosition().hashCode() : 0);
result = 31 * result + (getGeoFence() != null ? getGeoFence().hashCode() : 0);
return result;
}
}
14 changes: 7 additions & 7 deletions src/main/java/org/traccar/web/shared/model/GeoFence.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,22 @@ public void setTransferDevices(Set<Device> transferDevices) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o == null || !(o instanceof GeoFence)) return false;

GeoFence geoFence = (GeoFence) o;

if (id != geoFence.id) return false;
if (name != null ? !name.equals(geoFence.name) : geoFence.name != null) return false;
if (type != geoFence.type) return false;
if (getId() != geoFence.getId()) return false;
if (getName() != null ? !getName().equals(geoFence.getName()) : geoFence.getName() != null) return false;
if (getType() != geoFence.getType()) return false;

return true;
}

@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (type != null ? type.hashCode() : 0);
int result = (int) (getId() ^ (getId() >>> 32));
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + (getType() != null ? getType().hashCode() : 0);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public void setType(DeviceEventType type) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o == null || !(o instanceof NotificationTemplate)) return false;

NotificationTemplate that = (NotificationTemplate) o;

if (!settings.equals(that.settings)) return false;
return type == that.type;
if (!getSettings().equals(that.getSettings())) return false;
return getType() == that.getType();

}

Expand Down
20 changes: 9 additions & 11 deletions src/main/java/org/traccar/web/shared/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package org.traccar.web.shared.model;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.persistence.*;
Expand Down Expand Up @@ -136,13 +134,13 @@ public void setManager(Boolean manager) {
foreignKey = @ForeignKey(name = "users_devices_fkey_users_id"),
joinColumns = { @JoinColumn(name = "users_id", table = "users", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "devices_id", table = "devices", referencedColumnName = "id") })
private List<Device> devices = new LinkedList<Device>();
private Set<Device> devices = new HashSet<Device>();

public void setDevices(List<Device> devices) {
public void setDevices(Set<Device> devices) {
this.devices = devices;
}

public List<Device> getDevices() {
public Set<Device> getDevices() {
return devices;
}

Expand All @@ -163,13 +161,13 @@ public Set<Device> getAllAvailableDevices() {
foreignKey = @ForeignKey(name = "users_geofences_fkey_user_id"),
joinColumns = { @JoinColumn(name = "user_id", table = "users", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "geofence_id", table = "geofences", referencedColumnName = "id") })
private List<GeoFence> geoFences;
private Set<GeoFence> geoFences;

public void setGeoFences(List<GeoFence> geoFences) {
public void setGeoFences(Set<GeoFence> geoFences) {
this.geoFences = geoFences;
}

public List<GeoFence> getGeoFences() {
public Set<GeoFence> getGeoFences() {
return geoFences;
}

Expand Down Expand Up @@ -333,17 +331,17 @@ public void setReadOnly(boolean readOnly) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o == null || !(o instanceof User)) return false;

User user = (User) o;

if (login != null ? !login.equals(user.login) : user.login != null) return false;
if (getLogin() != null ? !getLogin().equals(user.getLogin()) : user.getLogin() != null) return false;

return true;
}

@Override
public int hashCode() {
return login != null ? login.hashCode() : 0;
return getLogin() != null ? getLogin().hashCode() : 0;
}
}
12 changes: 6 additions & 6 deletions src/test/java/org/traccar/web/shared/model/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public void testAvailableGeoFences() {
GeoFence g6 = new GeoFence(6, "g6");
GeoFence g7 = new GeoFence(7, "g7");

m1.setGeoFences(Arrays.asList(g1, g7));
m2.setGeoFences(Arrays.asList(g1, g2));
m3.setGeoFences(Arrays.asList(g5));
u1.setGeoFences(Arrays.asList(g3));
u2.setGeoFences(Arrays.asList(g4));
u3.setGeoFences(Arrays.asList(g6));
m1.setGeoFences(new HashSet<GeoFence>(Arrays.asList(g1, g7)));
m2.setGeoFences(new HashSet<GeoFence>(Arrays.asList(g1, g2)));
m3.setGeoFences(new HashSet<GeoFence>(Arrays.asList(g5)));
u1.setGeoFences(new HashSet<GeoFence>(Arrays.asList(g3)));
u2.setGeoFences(new HashSet<GeoFence>(Arrays.asList(g4)));
u3.setGeoFences(new HashSet<GeoFence>(Arrays.asList(g6)));

// test
assertEquals(set(g1, g2, g3, g4, g5, g6, g7), m1.getAllAvailableGeoFences());
Expand Down

0 comments on commit e16a11f

Please sign in to comment.