001: package org.drools.common;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.drools.FactHandle;
020:
021: /**
022: * Implementation of <code>FactHandle</code>.
023: * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
024: * @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
025: */
026: public class DefaultFactHandle implements InternalFactHandle {
027: // ----------------------------------------------------------------------
028: // Instance members
029: // ----------------------------------------------------------------------
030:
031: /**
032: *
033: */
034: private static final long serialVersionUID = 400L;
035: /** Handle id. */
036: private long id;
037: private long recency;
038: private Object object;
039: private EqualityKey key;
040: private int objectHashCode;
041: private boolean shadowFact;
042:
043: // ----------------------------------------------------------------------
044: // Constructors
045: // ----------------------------------------------------------------------
046:
047: public DefaultFactHandle() {
048:
049: }
050:
051: public DefaultFactHandle(final long id, final Object object) {
052: this .id = id;
053: this .recency = id;
054: this .object = object;
055: this .objectHashCode = object.hashCode();
056: }
057:
058: /**
059: * Construct.
060: *
061: * @param id
062: * Handle id.
063: */
064: public DefaultFactHandle(final long id, final Object object,
065: final long recency) {
066: this .id = id;
067: this .recency = recency;
068: this .object = object;
069: this .objectHashCode = object.hashCode();
070: }
071:
072: // ----------------------------------------------------------------------
073: // Instance members
074: // ----------------------------------------------------------------------
075:
076: /**
077: * @see Object
078: */
079: public boolean equals(final Object object) {
080: if (this == object) {
081: return true;
082: }
083:
084: if (object == null || !(object instanceof DefaultFactHandle)) {
085: return false;
086: }
087:
088: return this .id == ((DefaultFactHandle) object).id;
089: }
090:
091: public int getObjectHashCode() {
092: return this .objectHashCode;
093: }
094:
095: /**
096: * @see Object
097: */
098: public int hashCode() {
099: return (int) (this .id ^ (this .id >>> 32));
100: }
101:
102: /**
103: * @see FactHandle
104: */
105: public String toExternalForm() {
106: return "[fid:" + this .id + ":" + this .recency + ":"
107: + this .object + "]";
108: }
109:
110: /**
111: * @see Object
112: */
113: public String toString() {
114: return toExternalForm();
115: }
116:
117: public long getRecency() {
118: return this .recency;
119: }
120:
121: public void setRecency(final long recency) {
122: this .recency = recency;
123: }
124:
125: public long getId() {
126: return this .id;
127: }
128:
129: public void invalidate() {
130: this .id = -1;
131: this .object = null;
132: }
133:
134: public boolean isShadowFact() {
135: return this .shadowFact;
136: }
137:
138: public void setShadowFact(final boolean shadowFact) {
139: this .shadowFact = shadowFact;
140: }
141:
142: public Object getObject() {
143: return this .object;
144: }
145:
146: public void setObject(final Object object) {
147: this .object = object;
148: }
149:
150: /**
151: * @return the key
152: */
153: public EqualityKey getEqualityKey() {
154: return this .key;
155: }
156:
157: /**
158: * @param key the key to set
159: */
160: public void setEqualityKey(final EqualityKey key) {
161: this.key = key;
162: }
163: }
|