01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.resource;
19:
20: import de.finix.contelligent.exception.ComponentPersistenceException;
21: import de.finix.contelligent.persistence.ComponentPersistenceAdapter;
22:
23: public class ResourceReference implements Cloneable {
24:
25: private long resourceId = -1L;
26:
27: private ComponentPersistenceAdapter persistence;
28:
29: private Resource resource;
30:
31: private boolean isCloned = false;
32:
33: public ResourceReference(long resourceId,
34: ComponentPersistenceAdapter persistence) {
35: this .resourceId = resourceId;
36: this .persistence = persistence;
37: }
38:
39: public ResourceReference(Resource resource) {
40: this .resource = resource;
41: }
42:
43: public Resource getResource() throws ComponentPersistenceException {
44: if (resource == null) {
45: resource = persistence.loadResource(resourceId);
46: }
47: return resource;
48: }
49:
50: public boolean isPersistent() {
51: return persistence != null;
52: }
53:
54: public long getResourceId() {
55: return resourceId;
56: }
57:
58: public boolean isCloned() {
59: return isCloned;
60: }
61:
62: public Object clone() throws CloneNotSupportedException {
63: ResourceReference clone;
64:
65: if (persistence != null) {
66: clone = new ResourceReference(resourceId, persistence);
67: } else {
68: clone = new ResourceReference(resource);
69: }
70: clone.isCloned = true;
71: return clone;
72: }
73:
74: public int hashCode() {
75: if (isPersistent()) {
76: return (int) resourceId;
77: } else {
78: return resource.hashCode();
79: }
80: }
81:
82: public boolean equals(Object obj) {
83: if (!(obj instanceof ResourceReference)) {
84: return false;
85: }
86: ResourceReference ref = (ResourceReference) obj;
87:
88: if (ref.isPersistent()) {
89: return ref.getResourceId() == getResourceId();
90: } else {
91: try {
92: return ref.getResource().equals(getResource());
93: } catch (ComponentPersistenceException e) {
94: return false;
95: }
96: }
97: }
98: }
|