001package org.nuxeo.box.api.marshalling.jsonparsing; 002 003import org.nuxeo.box.api.marshalling.dao.BoxObject; 004import org.nuxeo.box.api.marshalling.interfaces.IBoxResourceHub; 005import org.nuxeo.box.api.marshalling.interfaces.IBoxType; 006 007import java.util.HashMap; 008import java.util.Locale; 009import java.util.Map; 010 011public abstract class BaseBoxResourceHub implements IBoxResourceHub { 012 013 // As a performance optimization, set up string values for all types. 014 private static final Map<String, IBoxType> lowercaseStringToType = new HashMap<String, IBoxType>(); 015 016 public BaseBoxResourceHub() { 017 initializeTypes(); 018 } 019 020 @Override 021 @SuppressWarnings("rawtypes") 022 public Class getClass(IBoxType type) { 023 return BoxObject.class; 024 } 025 026 protected Map<String, IBoxType> getLowerCaseStringToTypeMap() { 027 return lowercaseStringToType; 028 } 029 030 /** 031 * Get the concrete class for IBoxType 032 * 033 * @return 034 */ 035 @SuppressWarnings("rawtypes") 036 protected abstract Class getConcreteClassForIBoxType(); 037 038 /** 039 * Get class for a certain type, assuming the input type is an object of the concrete class of IBoxType defined in 040 * this resource hub. 041 * 042 * @param type 043 * @return 044 */ 045 @SuppressWarnings("rawtypes") 046 protected abstract Class getObjectClassGivenConcreteIBoxType(IBoxType type); 047 048 /** 049 * Do call super.initializeTypes() when overriding this. 050 */ 051 protected void initializeTypes() { 052 // Make it non-abstract so children can call super. This way makes it 053 // more explicit that super should be called. 054 } 055 056 protected void initializeEnumTypes(Class<? extends Enum> cls) { 057 Map<String, IBoxType> map = getLowerCaseStringToTypeMap(); 058 Enum[] types = cls.getEnumConstants(); 059 for (Enum type : types) { 060 String str = type.name().toLowerCase(Locale.ENGLISH); 061 map.put(str, (IBoxType) type); 062 } 063 } 064}