001/* 002 * Copyright 2013 Box, Inc. All rights reserved. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.nuxeo.box.api.marshalling.jsonparsing; 017 018import org.nuxeo.box.api.marshalling.dao.BoxObject; 019import org.nuxeo.box.api.marshalling.interfaces.IBoxResourceHub; 020import org.nuxeo.box.api.marshalling.interfaces.IBoxType; 021 022import java.util.HashMap; 023import java.util.Locale; 024import java.util.Map; 025 026public abstract class BaseBoxResourceHub implements IBoxResourceHub { 027 028 // As a performance optimization, set up string values for all types. 029 private static final Map<String, IBoxType> lowercaseStringToType = new HashMap<String, IBoxType>(); 030 031 public BaseBoxResourceHub() { 032 initializeTypes(); 033 } 034 035 @Override 036 @SuppressWarnings("rawtypes") 037 public Class getClass(IBoxType type) { 038 return BoxObject.class; 039 } 040 041 protected Map<String, IBoxType> getLowerCaseStringToTypeMap() { 042 return lowercaseStringToType; 043 } 044 045 /** 046 * Get the concrete class for IBoxType 047 * 048 * @return 049 */ 050 @SuppressWarnings("rawtypes") 051 protected abstract Class getConcreteClassForIBoxType(); 052 053 /** 054 * Get class for a certain type, assuming the input type is an object of the concrete class of IBoxType defined in 055 * this resource hub. 056 * 057 * @param type 058 * @return 059 */ 060 @SuppressWarnings("rawtypes") 061 protected abstract Class getObjectClassGivenConcreteIBoxType(IBoxType type); 062 063 /** 064 * Do call super.initializeTypes() when overriding this. 065 */ 066 protected void initializeTypes() { 067 // Make it non-abstract so children can call super. This way makes it 068 // more explicit that super should be called. 069 } 070 071 protected void initializeEnumTypes(Class<? extends Enum> cls) { 072 Map<String, IBoxType> map = getLowerCaseStringToTypeMap(); 073 Enum[] types = cls.getEnumConstants(); 074 for (Enum type : types) { 075 String str = type.name().toLowerCase(Locale.ENGLISH); 076 map.put(str, (IBoxType) type); 077 } 078 } 079}