01: package org.drools.common;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import org.drools.spi.FactHandleFactory;
20: import org.drools.util.PrimitiveLongStack;
21:
22: public abstract class AbstractFactHandleFactory implements
23: FactHandleFactory {
24:
25: /**
26: *
27: */
28: private static final long serialVersionUID = 400L;
29:
30: protected final PrimitiveLongStack factHandlePool = new PrimitiveLongStack();
31:
32: /** The fact id. */
33: private long id;
34:
35: /** The number of facts created - used for recency. */
36: private long counter;
37:
38: /* (non-Javadoc)
39: * @see org.drools.reteoo.FactHandleFactory#newFactHandle()
40: */
41: public final InternalFactHandle newFactHandle(final Object object) {
42: if (!this .factHandlePool.isEmpty()) {
43: return newFactHandle(this .factHandlePool.pop(), object);
44: }
45:
46: return newFactHandle(this .id++, object);
47: }
48:
49: /* (non-Javadoc)
50: * @see org.drools.reteoo.FactHandleFactory#newFactHandle(long)
51: */
52: protected final InternalFactHandle newFactHandle(final long id,
53: final Object object) {
54: return newFactHandle(id, object, this .counter++);
55: }
56:
57: /* (non-Javadoc)
58: * @see org.drools.reteoo.FactHandleFactory#newFactHandle(long)
59: */
60: protected abstract InternalFactHandle newFactHandle(final long id,
61: final Object object, final long recency);
62:
63: /* (non-Javadoc)
64: * @see org.drools.reteoo.FactHandleFactory#increaseFactHandleRecency(org.drools.FactHandle)
65: */
66: public final void increaseFactHandleRecency(
67: final InternalFactHandle factHandle) {
68: factHandle.setRecency(this .counter++);
69: }
70:
71: public void destroyFactHandle(final InternalFactHandle factHandle) {
72: this .factHandlePool.push(factHandle.getId());
73: factHandle.invalidate();
74: }
75:
76: /* (non-Javadoc)
77: * @see org.drools.reteoo.FactHandleFactory#newInstance()
78: */
79: public abstract FactHandleFactory newInstance();
80: }
|