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