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    
021    package org.granite.messaging.persistence;
022    
023    import java.lang.reflect.ParameterizedType;
024    import java.lang.reflect.Type;
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    import org.granite.config.GraniteConfig;
029    import org.granite.context.GraniteContext;
030    import org.granite.messaging.amf.io.convert.Converters;
031    
032    /**
033     * @author Franck WOLFF
034     */
035    public class ExternalizablePersistentList extends AbstractExternalizablePersistentCollection {
036    
037        private static final long serialVersionUID = 1L;
038    
039        public ExternalizablePersistentList() {
040        }
041    
042            public ExternalizablePersistentList(List<?> content, boolean initialized, boolean dirty) {
043                    super(null, initialized, dirty);
044                    setContentFromList(content);
045            }
046    
047            public ExternalizablePersistentList(Object[] content, boolean initialized, boolean dirty) {
048                    super(content, initialized, dirty);
049            }
050            
051            public List<?> getContentAsList(Type target) {
052                    List<Object> list = null;
053                    if (content != null) {
054                            list = new ArrayList<Object>(content.length);
055                            
056                GraniteConfig config = GraniteContext.getCurrentInstance().getGraniteConfig();
057                Converters converters = config.getConverters();
058                            Type[] typeArguments = null;
059                            if (target instanceof ParameterizedType)
060                                    typeArguments = ((ParameterizedType)target).getActualTypeArguments();
061                            
062                    for (Object o : content) {
063                            if (typeArguments != null)
064                                    list.add(converters.convert(o, typeArguments[0]));
065                            else
066                                    list.add(o);
067                    }
068                    }
069            return list;
070            }
071            
072            public void setContentFromList(List<?> list) {
073                    content = (list != null ? list.toArray() : null);
074            }
075    }