001: /**********************************************************************
002: Copyright (c) 2005 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.identity;
018:
019: import java.util.Map;
020: import java.util.WeakHashMap;
021:
022: import org.jpox.ClassConstants;
023: import org.jpox.ObjectManager;
024: import org.jpox.util.ClassUtils;
025:
026: /**
027: * Factory for OID instances.
028: * This factory holds a cache of weak references to OID.
029: *
030: * @version $Revision: 1.4 $
031: */
032: public class OIDFactory {
033: private static Map oids = new WeakHashMap();
034:
035: private OIDFactory() {
036: // Private constructor to prevent instantiation
037: }
038:
039: /**
040: * Factory method for OID instances using class name and key value.
041: * @param om The ObjectManager
042: * @param className the pc class name
043: * @param value the id value
044: * @return an OID instance
045: */
046: public static OID getInstance(ObjectManager om, String className,
047: Object value) {
048: // Get the OID class to use for this ObjectManager
049: Class oidClass = om.getOMFContext().getDatastoreIdentityClass();
050:
051: // Try to find a cached OID suitable
052: String key = oidClass.getName() + "#" + className + "#"
053: + value.toString();
054: OID oid = (OID) oids.get(key);
055: if (oid == null) {
056: if (oidClass == ClassConstants.OID_IMPL) {
057: //we hard code OIDImpl to improve performance
058: oid = new OIDImpl(className, value);
059: } else {
060: //others are pluggable
061: oid = (OID) ClassUtils.newInstance(oidClass,
062: new Class[] { String.class, Object.class },
063: new Object[] { className, value });
064: }
065: oids.put(key, oid);
066: }
067: return oid;
068: }
069:
070: /**
071: * Factory method for OID instances using long key value.
072: * @param om The ObjectManager
073: * @param value the id value
074: * @return an OID instance
075: */
076: public static OID getInstance(ObjectManager om, long value) {
077: // Get the OID class to use for this ObjectManager
078: Class oidClass = om.getOMFContext().getDatastoreIdentityClass();
079:
080: // Try to find a cached OID suitable
081: String key = oidClass.getName() + "#" + value;
082: OID oid = (OID) oids.get(key);
083: if (oid == null) {
084: if (oidClass == DatastoreUniqueOID.class) {
085: //we hard code DatastoreUniqueOID to improve performance
086: oid = new DatastoreUniqueOID(value);
087: } else {
088: //others are pluggable
089: oid = (OID) ClassUtils.newInstance(oidClass,
090: new Class[] { Long.class },
091: new Object[] { new Long(value) });
092: }
093: oids.put(key, oid);
094: }
095: return oid;
096: }
097:
098: /**
099: * Factory method for OID instances using toString() output.
100: * @param om The ObjectManager
101: * @param oidString result of toString on an OID
102: * @return an OID instance
103: */
104: public static OID getInstance(ObjectManager om, String oidString) {
105: // Get the OID class to use for this ObjectManager
106: Class oidClass = om.getOMFContext().getDatastoreIdentityClass();
107:
108: // Try to find a cached OID suitable
109: String key = oidClass.getName() + "#" + oidString;
110: OID oid = (OID) oids.get(key);
111: if (oid == null) {
112: if (oidClass == ClassConstants.OID_IMPL) {
113: //we hard code OIDImpl to improve performance
114: oid = new OIDImpl(oidString);
115: } else {
116: //others are pluggable
117: oid = (OID) ClassUtils.newInstance(oidClass,
118: new Class[] { String.class },
119: new Object[] { oidString });
120: }
121: oids.put(key, oid);
122: }
123: return oid;
124: }
125: }
|