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