Add support for nested type of query parameter
[smart-dao.git] / smart-domain / src / main / java / com / smartitengineering / domain / AbstractGenericPersistentDTO.java
blobadd6a47f40f3819c041711c6491d7aa304b1a4e4
1 /*
2 * This is a common dao with basic CRUD operations and is not limited to any
3 * persistent layer implementation
5 * Copyright (C) 2008 Imran M Yousuf (imyousuf@smartitengineering.com)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.smartitengineering.domain;
21 import java.io.Serializable;
23 /**
25 * @author imyousuf
27 public abstract class AbstractGenericPersistentDTO<Template extends PersistentDTO, IdType extends Comparable<IdType> & Serializable, VersionType extends Comparable<VersionType> & Serializable>
28 implements
29 PersistentDTO<PersistentDTO, IdType, VersionType> {
31 private IdType id;
32 private VersionType version;
34 protected AbstractGenericPersistentDTO() {
37 @Override
38 public IdType getId() {
39 return id;
42 @Override
43 public void setId(IdType id) {
44 this.id = id;
47 @Override
48 public VersionType getVersion() {
49 return version;
52 @Override
53 public void setVersion(VersionType version) {
54 this.version = version;
57 @Override
58 public int compareTo(PersistentDTO o) {
59 if (o == null) {
60 throw new IllegalArgumentException();
62 if (o.getId() == null && id == null) {
63 return -1;
65 if (o.getId() == null && id != null) {
66 return 1;
68 if (o.getId() != null && id == null) {
69 return -1;
71 if (id.getClass().isAssignableFrom(o.getId().getClass())) {
72 IdType tmp = (IdType) o.getId();
73 return tmp.compareTo(id);
75 else {
76 return o.getId().toString().compareTo(id.toString());
80 @Override
81 public int compare(PersistentDTO o1, PersistentDTO o2) {
82 if (o1 == null && o2 == null) {
83 return 0;
85 if (o1 == null && o2 != null) {
86 return -1;
88 if (o1 != null && o2 == null) {
89 return 1;
91 return o1.compareTo(o2);
94 @Override
95 public boolean equals(Object obj) {
96 if (obj == null) {
97 return false;
99 if (!PersistentDTO.class.isAssignableFrom(obj.getClass())) {
100 return false;
102 final PersistentDTO other = (PersistentDTO) obj;
103 return compareTo(other) == 0;
106 @Override
107 public int hashCode() {
108 int hash = 3;
109 hash = 23 * hash + (this.id != null ? this.id.hashCode() : 0);
110 hash = 23 * hash + (this.version != null ? this.version.hashCode() : 0);
111 return hash;
114 protected void clone(Template template) {
115 if (template == null) {
116 return;
118 template.setId(id);
119 template.setVersion(version);