1 2 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 24 public final class ClassIdTranslator { 25 26 private final ModelMetaData jmd; 27 private String message; 28 29 private final boolean stringClassIds; 31 private final Map cmdToIDString; private final Map idToCmdString; 34 private final Map cmdToIDInt; private final IntObjectHashMap idToCmdInt; 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 52 public void setMessage(String message) { 53 this.message = message; 54 } 55 56 public String getMessage() { 57 return message; 58 } 59 60 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 o1, Object 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 89 public boolean isStringClassIds() { 90 return stringClassIds; 91 } 92 93 98 public ClassMetaData getClassForIntClassId(int classId) { 99 ClassMetaData ans; 100 if (cmdToIDInt == null) { ans = jmd.getClassMetaData(classId); 102 if (ans == null) { 103 throw createUnknownClassException(Integer.toString(classId)); 104 } 105 } else { ans = (ClassMetaData)idToCmdInt.get(classId); 107 if (ans == null) { 108 ans = jmd.getClassMetaData(classId); 109 if (ans != null) { throw createInvalidClassException(ans, Integer.toString(classId)); 111 } else { throw createUnknownClassException(Integer.toString(classId)); 113 } 114 } 115 } 116 return ans; 117 } 118 119 123 public int getIntClassIdForClass(ClassMetaData cmd) { 124 if (cmdToIDInt == null) { 125 return cmd.classId; 126 } else { 127 int id = ((Integer )cmdToIDInt.get(cmd)).intValue(); 128 if (id < 0) throw createInvalidClassException(cmd); 129 return id; 130 } 131 } 132 133 138 public ClassMetaData getClassForStringClassId(String classId) { 139 ClassMetaData ans = (ClassMetaData)idToCmdString.get(classId); 140 if (ans == null) { 141 throw createUnknownClassException(classId); 142 } 143 return ans; 144 } 145 146 150 public String getStringClassIdForClass(ClassMetaData cmd) { 151 if (cmdToIDString == null) { 152 return cmd.classIdString; 153 } else { 154 String id = (String )cmdToIDString.get(cmd); 155 if (id == null) throw createInvalidClassException(cmd); 156 return id; 157 } 158 } 159 160 private RuntimeException createUnknownClassException(String classId) { 161 return BindingSupportImpl.getInstance().datastore( 162 "Unknown class-id value (" + classId + ") for " + message); 163 } 164 165 private RuntimeException createInvalidClassException(ClassMetaData cmd, 166 String 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 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 |