KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > facility > naming > longid > CompositePLongGen


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
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  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.jorm.facility.naming.longid;
21
22 import org.objectweb.jorm.api.PClassMapping;
23 import org.objectweb.jorm.api.PException;
24 import org.objectweb.jorm.facility.naming.generator.LongGenMgr;
25 import org.objectweb.jorm.facility.naming.generator.PLongGen;
26
27 /**
28  * This object long identifier generates identifier divided in two parts. The
29  * first part is the value representing a class persistent class. The second
30  * part is a value representing the instance of the class. In fact this
31  * generator adds a fixed value (class id) with another long values choosen
32  * by a sub PLongGen.
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class CompositePLongGen implements PLongGen {
37
38     /**
39      * The inner generator of object identifier
40      */

41     protected PLongGen objectIdGenerator;
42
43     /**
44      * The identifier of the persitent class
45      */

46     protected long cid;
47     protected int cidSize;
48
49     public CompositePLongGen(PLongGen objectIdGenerator, long cid, int cidSize) {
50         this.objectIdGenerator = objectIdGenerator;
51         this.cid = cid;
52         this.cidSize = cidSize;
53     }
54
55     public long getCid() {
56         return cid;
57     }
58
59     // IMPLEMENTATION OF THE PLongGen INTERFACE //
60
//------------------------------------------//
61

62     /**
63      * Initializes the underlying object identifier generator
64      */

65     public void init(String JavaDoc name, PClassMapping pcm, LongGenMgr mgr) throws PException {
66         objectIdGenerator.init(name, pcm, mgr);
67     }
68
69     /**
70      * Allocates a new composite identifier
71      */

72     public long genId() throws PException {
73         return genId(null);
74     }
75
76     /**
77      * Allocates a new composite identifier
78      * @param conn is a way to access the data support
79      */

80     public long genId(Object JavaDoc conn) throws PException {
81         long oid;
82         if (conn != null) {
83             oid = objectIdGenerator.genId(conn);
84         } else {
85             oid = objectIdGenerator.genId();
86         }
87         return (cid << (LongIdManager.LONG_SIZE - cidSize)) + oid;
88     }
89 }
90
Popular Tags