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     */
022    package org.granite.messaging.persistence;
023    
024    import java.lang.reflect.ParameterizedType;
025    import java.lang.reflect.Type;
026    import java.util.Comparator;
027    import java.util.HashSet;
028    import java.util.Set;
029    import java.util.SortedSet;
030    import java.util.TreeSet;
031    
032    import org.granite.config.GraniteConfig;
033    import org.granite.context.GraniteContext;
034    import org.granite.messaging.amf.io.convert.Converters;
035    import org.granite.util.TypeUtil;
036    
037    /**
038     * @author Franck WOLFF
039     */
040    public class ExternalizablePersistentSet extends AbstractExternalizablePersistentCollection {
041    
042        private static final long serialVersionUID = 1L;
043    
044        public ExternalizablePersistentSet() {
045        }
046        
047            public ExternalizablePersistentSet(Set<?> content, boolean initialized, boolean dirty) {
048                    super(null, initialized, dirty);
049                    setContentFromSet(content);
050            }
051        
052            public ExternalizablePersistentSet(Object[] content, boolean initialized, boolean dirty) {
053                    super(content, initialized, dirty);
054            }
055    
056            public Set<?> getContentAsSet(Type target) {
057                    return getContentAsSet(target, null);
058            }
059            
060            @SuppressWarnings({ "unchecked", "rawtypes" })
061            public Set<?> getContentAsSet(Type target, Comparator comparator) {
062            Set set = null;
063            if (content != null) {
064                    if (SortedSet.class.isAssignableFrom(TypeUtil.classOfType(target))) {
065                            if (comparator != null)
066                                    set = new TreeSet(comparator);
067                            else
068                                    set = new TreeSet();
069                    }
070                    else
071                            set = new HashSet(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 (Object o : content) {
080                            if (typeArguments != null)
081                                    set.add(converters.convert(o, typeArguments[0]));
082                            else
083                                    set.add(o);
084                    }
085            }
086                    return set;
087            }
088            
089            public void setContentFromSet(Set<?> set) {
090                    content = (set != null ? set.toArray() : null);
091            }
092    }