net.solarnetwork.central.user.domain.UserNodePK Maven / Gradle / Ivy
/* ==================================================================
* UserNodePK.java - Oct 3, 2014 6:47:25 AM
*
* Copyright 2007-2014 SolarNetwork.net Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ==================================================================
*/
package net.solarnetwork.central.user.domain;
import java.io.Serializable;
/**
* Primary key based on a user ID and node ID.
*
* @author matt
* @version 1.0
*/
public class UserNodePK implements Serializable, Cloneable, Comparable {
private static final long serialVersionUID = -2661140310545544324L;
private Long nodeId;
private Long userId;
/**
* Default constructor.
*/
public UserNodePK() {
super();
}
/**
* Construct with values.
*
* @param userId
* the user ID
* @param nodeId
* the node ID
*/
public UserNodePK(Long userId, Long nodeId) {
super();
this.nodeId = nodeId;
this.userId = userId;
}
/**
* Compare two {@code UserNodePK} objects. Keys are ordered based on:
*
*
* - userId
* - nodeId
*
*
* Null values will be sorted before non-null values.
*/
@Override
public int compareTo(UserNodePK o) {
if ( o == null ) {
return 1;
}
if ( o.userId == null ) {
return 1;
} else if ( userId == null ) {
return -1;
}
int comparison = userId.compareTo(o.userId);
if ( comparison != 0 ) {
return comparison;
}
if ( o.nodeId == null ) {
return 1;
} else if ( nodeId == null ) {
return -1;
}
return nodeId.compareTo(o.nodeId);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UserNodePK{");
if ( userId != null ) {
builder.append("userId=");
builder.append(userId);
builder.append(", ");
}
if ( nodeId != null ) {
builder.append("nodeId=");
builder.append(nodeId);
}
builder.append("}");
return builder.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
result = prime * result + ((nodeId == null) ? 0 : nodeId.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;
}
UserNodePK other = (UserNodePK) obj;
if ( nodeId == null ) {
if ( other.nodeId != null ) {
return false;
}
} else if ( !nodeId.equals(other.nodeId) ) {
return false;
}
if ( userId == null ) {
if ( other.userId != null ) {
return false;
}
} else if ( !userId.equals(other.userId) ) {
return false;
}
return true;
}
@Override
protected Object clone() {
try {
return super.clone();
} catch ( CloneNotSupportedException e ) {
// shouldn't get here
throw new RuntimeException(e);
}
}
public Long getNodeId() {
return nodeId;
}
public void setNodeId(Long nodeId) {
this.nodeId = nodeId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
}