001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.messaging.persistence; 023 024import java.lang.reflect.Type; 025import java.lang.reflect.ParameterizedType; 026import java.util.Comparator; 027import java.util.HashMap; 028import java.util.Map; 029import java.util.SortedMap; 030import java.util.TreeMap; 031 032import org.granite.config.GraniteConfig; 033import org.granite.context.GraniteContext; 034import org.granite.messaging.amf.io.convert.Converters; 035import org.granite.util.TypeUtil; 036 037/** 038 * @author Franck WOLFF 039 */ 040public class ExternalizablePersistentMap extends AbstractExternalizablePersistentCollection { 041 042 private static final long serialVersionUID = 1L; 043 044 public ExternalizablePersistentMap() { 045 } 046 047 public ExternalizablePersistentMap(Map<?, ?> content, boolean initialized, boolean dirty) { 048 super(null, initialized, dirty); 049 setContentFromMap(content); 050 } 051 052 public ExternalizablePersistentMap(Object[] content, boolean initialized, boolean dirty) { 053 super(content, initialized, dirty); 054 } 055 056 public Map<?, ?> getContentAsMap(Type target) { 057 return getContentAsMap(target, null); 058 } 059 060 @SuppressWarnings({ "unchecked", "rawtypes" }) 061 public Map<?, ?> getContentAsMap(Type target, Comparator comparator) { 062 Map map = null; 063 if (content != null) { 064 if (SortedMap.class.isAssignableFrom(TypeUtil.classOfType(target))) { 065 if (comparator != null) 066 map = new TreeMap(comparator); 067 else 068 map = new TreeMap(); 069 } 070 else 071 map = new HashMap(content.length); 072 073 GraniteConfig config = GraniteContext.getCurrentInstance().getGraniteConfig(); 074 Converters converters = config.getConverters(); 075 Type[] typeArguments = null; 076 if (target instanceof ParameterizedType) 077 typeArguments = ((ParameterizedType)target).getActualTypeArguments(); 078 079 for (int i = 0; i < content.length; i++) { 080 Object[] entry = (Object[])content[i]; 081 082 if (typeArguments != null) 083 map.put(converters.convert(entry[0], typeArguments[0]), converters.convert(entry[1], typeArguments[1])); 084 else 085 map.put(entry[0], entry[1]); 086 } 087 } 088 return map; 089 } 090 091 public void setContentFromMap(Map<?, ?> map) { 092 if (map == null) 093 content = null; 094 else { 095 content = new Object[map.size()]; 096 int index = 0; 097 for (Map.Entry<?, ?> entry : map.entrySet()) 098 content[index++] = new Object[]{entry.getKey(), entry.getValue()}; 099 } 100 } 101}