KickJava   Java API By Example, From Geeks To Geeks.

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


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.PException;
23 import org.objectweb.jorm.naming.api.PExceptionNaming;
24 import org.objectweb.jorm.naming.api.PNameGetter;
25 import org.objectweb.jorm.naming.lib.AbstractInheritKeyFilteredPNamingContext;
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.jorm.type.api.PTypeSpace;
28
29 /**
30  * Is a PNamingContext managing the LongIdPName and LongIdBinder. It is based
31  * on the AbstractInheritKeyFilteredPNamingContext class. The key is the class
32  * identifier. The filter (evaluate method) is in charge to extract the class
33  * identifier from the identifier (long value). Indeed the long value is
34  * composed of two parts the high bits describe the class identifier, whereas
35  * the low bits describes the object instance identifier. The limit between both
36  * parts are defined are building time (constructor).
37  *
38  * @author S.Chassande-Barrioz
39  */

40 public class LongIdPNC extends AbstractInheritKeyFilteredPNamingContext {
41
42     long lid;
43
44     /**
45      * is the number of bits allocated for the class identifier among the
46      * number of bits of a long value (64).
47      */

48     private int cidSize;
49
50     /**
51      * Builds a LongIdPNC with a particular class identifier size
52      * @param cidsize is the number of bits allocated for the class identifier
53      * among the number of bits of a long value (64).
54      */

55     public LongIdPNC(int cidsize) throws PException {
56         super(PTypeSpace.LONG, CTLONG);
57         setNullPName(new LongIdPName(-1, new LongIdBinder()));
58         ((LongIdBinder) getNull().getPNameManager()).setNullPName(getNull());
59         setCidSize(cidsize);
60         init();
61     }
62
63     /**
64      * @see this.cidSize
65      */

66     public int getCidSize() {
67         return cidSize;
68     }
69
70     /**
71      * @see this.cidSize
72      */

73     public void setCidSize(int cidSize) {
74         this.cidSize = cidSize;
75     }
76
77     /**
78      * Calculates the class identifier from the long identifier value
79      * @param png is a PNameGetter or a Long instance
80      * @param ctx is the context used for evaluate the PNameGetter
81      * @return a Long instance containing the class identifier
82      * @throws PException if it is not possible to fetch the long value, or if
83      * the png parameter is not managed.
84      */

85     public Object JavaDoc evaluate(Object JavaDoc png, Object JavaDoc ctx) throws PException {
86         if (png instanceof LongIdPNG) {
87             long id = ((LongIdPNG) png).pnGetLid(ctx);
88             id = id >> (LongIdManager.LONG_SIZE - cidSize);
89             return new Long JavaDoc(id);
90         } else if (png instanceof PNameGetter) {
91             long id = ((PNameGetter) png).pngetLongField("lid", ctx);
92             id = id >> (LongIdManager.LONG_SIZE - cidSize);
93             return new Long JavaDoc(id);
94         } else if (png instanceof Long JavaDoc) {
95             long id = ((Long JavaDoc) png).longValue();
96             id = id >> (LongIdManager.LONG_SIZE - cidSize);
97             return new Long JavaDoc(id);
98         }
99         throw new PExceptionNaming(
100             "Impossible to evaluate without a PNameGetter: " + png);
101     }
102
103     public void setNullPName(Object JavaDoc o) throws PException {
104         super.setNullPName(o);
105         //Set the field values representing the null PName
106
PNameGetter png = (PNameGetter) nullPName.encodeAbstract();
107         lid = png.pngetLongField("lid", null);
108     }
109
110     public boolean isNull(Object JavaDoc _png, Object JavaDoc ctx) throws PException {
111         if (_png instanceof org.objectweb.jorm.facility.naming.longid.LongIdPNG) {
112             org.objectweb.jorm.facility.naming.longid.LongIdPNG png = (org.objectweb.jorm.facility.naming.longid.LongIdPNG) _png;
113             if (this.lid != png.pnGetLid(ctx)) {
114                 return false;
115             }
116         } else if (_png instanceof PNameGetter) {
117             PNameGetter png = (PNameGetter) _png;
118             if (this.lid != png.pngetLongField("lid", ctx)) {
119                 return false;
120             }
121         } else {
122             throw new PException("The specified pname getter is not supported: " + _png);
123         }
124         return true;
125     }
126     
127     public void setPType(PType pt) {
128         super.setPType(pt);
129         nullPName.getPNameManager().setPType(pt);
130     }
131
132 }
133
Popular Tags