KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metadata > ClassIdTranslator


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metadata;
13
14 import com.versant.core.util.IntObjectHashMap;
15
16 import java.util.*;
17
18 import com.versant.core.common.BindingSupportImpl;
19
20 /**
21  * This maintains translation tables to translate ClassMetaData to/from
22  * String and int IDs.
23  */

24 public final class ClassIdTranslator {
25
26     private final ModelMetaData jmd;
27     private String JavaDoc message;
28
29     private final boolean stringClassIds; // are the class-id's Strings?
30

31     private final Map cmdToIDString; // ClassMetaData -> String ID
32
private final Map idToCmdString; // String ID -> ClassMetaData
33

34     private final Map cmdToIDInt; // ClassMetaData -> int ID
35
private final IntObjectHashMap idToCmdInt; // int ID -> ClassMetaData
36

37     public ClassIdTranslator(ModelMetaData jmd, boolean stringClassIds,
38             Map cmdToIDString, Map idToCmdString,
39             Map cmdToIDInt, IntObjectHashMap idToCmdInt) {
40         this.jmd = jmd;
41         this.stringClassIds = stringClassIds;
42         this.cmdToIDString = cmdToIDString;
43         this.idToCmdString = idToCmdString;
44         this.cmdToIDInt = cmdToIDInt;
45         this.idToCmdInt = idToCmdInt;
46     }
47
48     /**
49      * Set the message used to constuct exceptions for invalid classes and
50      * so on (e.g. 'field com.acme.model.Company.data').
51      */

52     public void setMessage(String JavaDoc message) {
53         this.message = message;
54     }
55
56     public String JavaDoc getMessage() {
57         return message;
58     }
59
60     /**
61      * Get all of the ClassMetaData's we have in alphabetical order.
62      */

63     public List getClassList() {
64         if (cmdToIDInt != null) {
65             return createArrayList(cmdToIDInt.keySet().iterator());
66         }
67         if (cmdToIDString != null) {
68             return createArrayList(cmdToIDString.keySet().iterator());
69         }
70         return Collections.EMPTY_LIST;
71     }
72
73     private static ArrayList createArrayList(Iterator i) {
74         ArrayList a = new ArrayList();
75         for (; i.hasNext(); ) a.add(i.next());
76         Collections.sort(a, new Comparator() {
77             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
78                 ClassMetaData a = (ClassMetaData)o1;
79                 ClassMetaData b = (ClassMetaData)o2;
80                 return a.index - b.index;
81             }
82         });
83         return a;
84     }
85
86     /**
87      * Are the class-id's Strings (i.e. not ints)?
88      */

89     public boolean isStringClassIds() {
90         return stringClassIds;
91     }
92
93     /**
94      * Convert an int class-id value read from our class-id column into
95      * ClassMetaData. Throws a JDODataStoreException if the classId is
96      * invalid.
97      */

98     public ClassMetaData getClassForIntClassId(int classId) {
99         ClassMetaData ans;
100         if (cmdToIDInt == null) { // use global class-id's
101
ans = jmd.getClassMetaData(classId);
102             if (ans == null) {
103                 throw createUnknownClassException(Integer.toString(classId));
104             }
105         } else { // use local class-id's
106
ans = (ClassMetaData)idToCmdInt.get(classId);
107             if (ans == null) {
108                 ans = jmd.getClassMetaData(classId);
109                 if (ans != null) { // not a valid class for this field
110
throw createInvalidClassException(ans, Integer.toString(classId));
111                 } else { // dodgy classId
112
throw createUnknownClassException(Integer.toString(classId));
113                 }
114             }
115         }
116         return ans;
117     }
118
119     /**
120      * Convert a class into an int class-id for setting on our class-id column.
121      * Throws a JDODataStoreException if the class is invalid.
122      */

123     public int getIntClassIdForClass(ClassMetaData cmd) {
124         if (cmdToIDInt == null) {
125             return cmd.classId;
126         } else {
127             int id = ((Integer JavaDoc)cmdToIDInt.get(cmd)).intValue();
128             if (id < 0) throw createInvalidClassException(cmd);
129             return id;
130         }
131     }
132
133     /**
134      * Convert a String class-id value read from our class-id column into
135      * ClassMetaData. Throws a JDODataStoreException if the classId is
136      * invalid.
137      */

138     public ClassMetaData getClassForStringClassId(String JavaDoc classId) {
139         ClassMetaData ans = (ClassMetaData)idToCmdString.get(classId);
140         if (ans == null) {
141             throw createUnknownClassException(classId);
142         }
143         return ans;
144     }
145
146     /**
147      * Convert a class into an String class-id for setting on our class-id
148      * column. Throws a JDODataStoreException if the class is invalid.
149      */

150     public String JavaDoc getStringClassIdForClass(ClassMetaData cmd) {
151         if (cmdToIDString == null) {
152             return cmd.classIdString;
153         } else {
154             String JavaDoc id = (String JavaDoc)cmdToIDString.get(cmd);
155             if (id == null) throw createInvalidClassException(cmd);
156             return id;
157         }
158     }
159
160     private RuntimeException JavaDoc createUnknownClassException(String JavaDoc classId) {
161         return BindingSupportImpl.getInstance().datastore(
162             "Unknown class-id value (" + classId + ") for " + message);
163     }
164
165     private RuntimeException JavaDoc createInvalidClassException(ClassMetaData cmd,
166             String JavaDoc classId) {
167         return BindingSupportImpl.getInstance().datastore(
168             "Instances of class " + cmd.qname + " for ID " + classId +
169             " are not allowed for " + message);
170     }
171
172     private RuntimeException JavaDoc createInvalidClassException(ClassMetaData cmd) {
173         return BindingSupportImpl.getInstance().datastore("Instances of class " + cmd.qname +
174             " are not allowed for " + message);
175     }
176
177 }
178
Popular Tags