001    package org.cumulus4j.store.model;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    public abstract class DetachedClassMetaModel {
007    
008            private static final ThreadLocal<DetachedClassMetaModel> threadLocal = new ThreadLocal<DetachedClassMetaModel>();
009    
010            public static DetachedClassMetaModel getInstance() {
011                    return threadLocal.get();
012            }
013    
014            public static void setInstance(DetachedClassMetaModel detachedClassMetaModel) {
015                    if (detachedClassMetaModel == null)
016                            threadLocal.remove();
017                    else
018                            threadLocal.set(detachedClassMetaModel);
019            }
020    
021            private Map<Long, ClassMeta> classID2ClassMeta = new HashMap<Long, ClassMeta>();
022            private Map<Long, FieldMeta> fieldID2FieldMeta = new HashMap<Long, FieldMeta>();
023    
024            public DetachedClassMetaModel() { }
025    
026            public final ClassMeta getClassMeta(long classID, boolean throwExceptionIfNotFound) {
027                    ClassMeta result = classID2ClassMeta.get(classID);
028                    if (result != null)
029                            return result;
030    
031                    return getClassMetaImpl(classID, throwExceptionIfNotFound);
032            }
033    
034            public final FieldMeta getFieldMeta(long fieldID, boolean throwExceptionIfNotFound) {
035                    FieldMeta result = fieldID2FieldMeta.get(fieldID);
036                    if (result != null)
037                            return result;
038    
039                    return getFieldMetaImpl(fieldID, throwExceptionIfNotFound);
040            }
041    
042            protected abstract ClassMeta getClassMetaImpl(long classID, boolean throwExceptionIfNotFound);
043    
044            protected abstract FieldMeta getFieldMetaImpl(long fieldID, boolean throwExceptionIfNotFound);
045    
046            public void registerClassMetaCurrentlyDetaching(ClassMeta classMeta) {
047                    classID2ClassMeta.put(classMeta.getClassID(), classMeta);
048            }
049    
050            public void registerFieldMetaCurrentlyDetaching(FieldMeta fieldMeta) {
051                    fieldID2FieldMeta.put(fieldMeta.getFieldID(), fieldMeta);
052            }
053    
054    }