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.io.Externalizable;
024    import java.io.IOException;
025    import java.io.ObjectInput;
026    import java.io.ObjectOutput;
027    import java.util.Collection;
028    
029    /**
030     * @author Franck WOLFF
031     */
032    public abstract class AbstractExternalizablePersistentCollection implements Externalizable {
033    
034        private static final long serialVersionUID = 1L;
035        
036        private static final Object[] OBJECT_0 = new Object[0]; 
037    
038        protected boolean initialized;
039        protected String metadata;
040        protected boolean dirty;
041        protected Object[] content;
042    
043        public AbstractExternalizablePersistentCollection() {
044            this(OBJECT_0, true, false);
045        }
046        
047        public AbstractExternalizablePersistentCollection(Object[] content, boolean initialized, boolean dirty) {
048            this.content = content;
049            this.initialized = initialized;
050            this.dirty = dirty;
051        }
052    
053            public boolean isInitialized() {
054            return initialized;
055        }
056        protected void setInitialized(boolean initialized) {
057            this.initialized = initialized;
058        }
059    
060        public String getMetadata() {
061                    return metadata;
062            }
063    
064            public void setMetadata(String metadata) {
065                    this.metadata = metadata;
066            }
067    
068            public boolean isDirty() {
069            return dirty;
070        }
071        public void setDirty(boolean dirty) {
072                    this.dirty = dirty;
073            }
074    
075        public Object[] getContent() {
076                    return content;
077            }
078            public void setContent(Object[] content) {
079                    this.content = content;
080            }
081    
082            public void writeExternal(ObjectOutput out) throws IOException {
083            out.writeObject(Boolean.valueOf(initialized));
084            out.writeObject(metadata);
085            if (initialized) {
086                out.writeObject(Boolean.valueOf(dirty));
087                out.writeObject(content);
088            }
089        }
090    
091        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
092            initialized = ((Boolean)in.readObject()).booleanValue();
093            metadata = (String)in.readObject();
094            if (initialized) {
095                dirty = ((Boolean)in.readObject()).booleanValue();
096                
097                Object o = in.readObject();
098                if (o != null) {
099                        if (o instanceof Collection<?>)
100                            content = ((Collection<?>)o).toArray();
101                        else if (o.getClass().isArray())
102                            content = (Object[])o;
103                        else
104                            content = new Object[]{o}; // should never happened...
105                }
106            }
107        }
108    }