001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2013 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.jmf.persistence;
022    
023    import java.io.IOException;
024    import java.io.ObjectInput;
025    import java.io.ObjectOutput;
026    import java.lang.reflect.InvocationTargetException;
027    import java.util.Arrays;
028    import java.util.Collection;
029    import java.util.Comparator;
030    import java.util.Iterator;
031    import java.util.Map;
032    import java.util.Map.Entry;
033    import java.util.NoSuchElementException;
034    import java.util.Set;
035    import java.util.SortedMap;
036    import java.util.SortedSet;
037    
038    import org.granite.messaging.jmf.ExtendedObjectInput;
039    import org.granite.messaging.persistence.PersistentCollectionSnapshot;
040    
041    /**
042     * @author Franck WOLFF
043     */
044    public class JMFPersistentCollectionSnapshot implements PersistentCollectionSnapshot {
045            
046            protected boolean initialized = false;
047            protected boolean dirty = false;
048            protected Object[] elements = null;
049            protected boolean sorted = false;
050            protected String comparatorClassName = null;
051            
052            public JMFPersistentCollectionSnapshot(String detachedState) {
053            }
054            
055            public JMFPersistentCollectionSnapshot(boolean sorted, String detachedState) {
056                    this.sorted = sorted;
057            }
058    
059            public JMFPersistentCollectionSnapshot(boolean initialized, String detachedState, boolean dirty, Collection<?> collection) {
060                    this.initialized = initialized;
061                    if (initialized) {
062                            this.dirty = dirty;
063                            this.elements = collection.toArray();
064                            
065                            if (collection instanceof SortedSet) {
066                                    this.sorted = true;
067                                    
068                                    Comparator<?> comparator = ((SortedSet<?>)collection).comparator();
069                                    if (comparator != null)
070                                            this.comparatorClassName = comparator.getClass().getName();
071                            }
072                    }
073            }
074    
075            public JMFPersistentCollectionSnapshot(boolean initialized, String detachedState, boolean dirty, Map<?, ?> collection) {
076                    this.initialized = initialized;
077                    if (initialized) {
078                            this.dirty = dirty;
079                            
080                            Object[] entries = collection.entrySet().toArray();
081                            this.elements = new Object[entries.length * 2];
082                            
083                            int elementIndex = 0;
084                            for (int entryIndex = 0; entryIndex < entries.length; entryIndex++) {
085                                    Map.Entry<?, ?> entry = (Map.Entry<?, ?>)entries[entryIndex];
086                                    this.elements[elementIndex++] = entry.getKey();
087                                    this.elements[elementIndex++] = entry.getValue();
088                            }
089                            
090                            if (collection instanceof SortedMap) {
091                                    this.sorted = true;
092                                    
093                                    Comparator<?> comparator = ((SortedMap<?, ?>)collection).comparator();
094                                    if (comparator != null)
095                                            this.comparatorClassName = comparator.getClass().getName();
096                            }
097                    }
098            }
099            
100            public boolean isInitialized() {
101                    return initialized;
102            }
103            
104            public String getDetachedState() {
105                    return null;
106            }
107    
108            public boolean isDirty() {
109                    return dirty;
110            }
111    
112            public boolean isSorted() {
113                    return sorted;
114            }
115    
116            public String getComparatorClassName() {
117                    return comparatorClassName;
118            }
119            
120            public <T> Comparator<T> newComparator(ObjectInput in)
121                    throws ClassNotFoundException, InstantiationException, IllegalAccessException,
122                    InvocationTargetException, SecurityException, NoSuchMethodException {
123                    
124                    if (comparatorClassName == null)
125                            return null;
126                    
127                    return ((ExtendedObjectInput)in).getReflection().newInstance(comparatorClassName);
128            }
129    
130            @SuppressWarnings("unchecked")
131            public <T> Collection<T> getElementsAsCollection() {
132                    return (Collection<T>)Arrays.asList(elements);
133            }
134            
135            public <K, V> Map<K, V> getElementsAsMap() {
136                    return new SnapshotMap<K, V>(elements);
137            }
138            
139            public void writeExternal(ObjectOutput out) throws IOException {
140                    out.writeBoolean(initialized);
141                    if (initialized) {
142                            if (sorted)
143                                    out.writeUTF(comparatorClassName);
144                            out.writeBoolean(dirty);
145                            out.writeObject(elements);
146                    }
147            }
148    
149            public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
150                    readInitializationData(in);
151                    if (initialized)
152                            readCoreData(in);
153            }
154            
155            public void readInitializationData(ObjectInput in) throws IOException {
156                    initialized = in.readBoolean();
157                    
158                    if (initialized && sorted)
159                            comparatorClassName = in.readUTF();
160            }
161            
162            public void readCoreData(ObjectInput in) throws IOException, ClassNotFoundException {
163                    this.dirty = in.readBoolean();
164                    this.elements = (Object[])in.readObject();
165            }
166            
167            static class SnapshotMap<K, V> implements Map<K, V> {
168                    
169                    private final Object[] elements;
170                    
171                    public SnapshotMap(Object[] elements) {
172                            if ((elements.length % 2) != 0)
173                                    throw new IllegalArgumentException("Elements must have an even length: " + elements.length);
174                            this.elements = elements;
175                    }
176                    
177                    public int size() {
178                            return elements.length / 2;
179                    }
180    
181                    public boolean isEmpty() {
182                            return elements.length == 0;
183                    }
184    
185                    public Set<Entry<K, V>> entrySet() {
186                            return new Set<Entry<K, V>>() {
187    
188                                    public int size() {
189                                            return elements.length / 2;
190                                    }
191    
192                                    public boolean isEmpty() {
193                                            return elements.length == 0;
194                                    }
195    
196                                    public Iterator<Entry<K, V>> iterator() {
197                                            
198                                            return new Iterator<Entry<K, V>>() {
199    
200                                                    private int cursor = 0;
201                                                    
202                                                    public boolean hasNext() {
203                                                            return cursor < elements.length;
204                                                    }
205    
206                                                    @SuppressWarnings("unchecked")
207                                                    public Entry<K, V> next() {
208                                                            if (cursor >= elements.length)
209                                                                    throw new NoSuchElementException();
210                                                            
211                                                            K key = (K)elements[cursor++];
212                                                            V value = (V)elements[cursor++];
213                                                            return new SnapshotMapEntry<K, V>(key, value);
214                                                    }
215    
216                                                    public void remove() {
217                                                            throw new UnsupportedOperationException();
218                                                    }
219                                            };
220                                    }
221    
222                                    public boolean contains(Object o) {
223                                            throw new UnsupportedOperationException();
224                                    }
225    
226                                    public Object[] toArray() {
227                                            throw new UnsupportedOperationException();
228                                    }
229    
230                                    public <T> T[] toArray(T[] a) {
231                                            throw new UnsupportedOperationException();
232                                    }
233    
234                                    public boolean add(Entry<K, V> e) {
235                                            throw new UnsupportedOperationException();
236                                    }
237    
238                                    public boolean remove(Object o) {
239                                            throw new UnsupportedOperationException();
240                                    }
241    
242                                    public boolean containsAll(Collection<?> c) {
243                                            throw new UnsupportedOperationException();
244                                    }
245    
246                                    public boolean addAll(Collection<? extends Entry<K, V>> c) {
247                                            throw new UnsupportedOperationException();
248                                    }
249    
250                                    public boolean retainAll(Collection<?> c) {
251                                            throw new UnsupportedOperationException();
252                                    }
253    
254                                    public boolean removeAll(Collection<?> c) {
255                                            throw new UnsupportedOperationException();
256                                    }
257    
258                                    public void clear() {
259                                            throw new UnsupportedOperationException();
260                                    }
261    
262                                    @Override
263                                    public int hashCode() {
264                                            throw new UnsupportedOperationException();
265                                    }
266    
267                                    @Override
268                                    public boolean equals(Object obj) {
269                                            throw new UnsupportedOperationException();
270                                    }
271                            };
272                    }
273    
274                    public boolean containsKey(Object key) {
275                            throw new UnsupportedOperationException();
276                    }
277    
278                    public boolean containsValue(Object value) {
279                            throw new UnsupportedOperationException();
280                    }
281    
282                    public V get(Object key) {
283                            throw new UnsupportedOperationException();
284                    }
285    
286                    public V put(K key, V value) {
287                            throw new UnsupportedOperationException();
288                    }
289    
290                    public V remove(Object key) {
291                            throw new UnsupportedOperationException();
292                    }
293    
294                    public void putAll(Map<? extends K, ? extends V> m) {
295                            throw new UnsupportedOperationException();
296                    }
297    
298                    public void clear() {
299                            throw new UnsupportedOperationException();
300                    }
301    
302                    public Set<K> keySet() {
303                            throw new UnsupportedOperationException();
304                    }
305    
306                    public Collection<V> values() {
307                            throw new UnsupportedOperationException();
308                    }
309    
310                    @Override
311                    public int hashCode() {
312                            throw new UnsupportedOperationException();
313                    }
314    
315                    @Override
316                    public boolean equals(Object obj) {
317                            throw new UnsupportedOperationException();
318                    }
319            }
320            
321            static class SnapshotMapEntry<K, V> implements Entry<K, V> {
322    
323                    private final K key;
324                    private final V value;
325                    
326                    public SnapshotMapEntry(K key, V value) {
327                            this.key = key;
328                            this.value = value;
329                    }
330    
331                    public K getKey() {
332                            return key;
333                    }
334    
335                    public V getValue() {
336                            return value;
337                    }
338    
339                    public V setValue(V value) {
340                            throw new UnsupportedOperationException();
341                    }
342    
343                    @Override
344                    public int hashCode() {
345                            return (key == null   ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
346                    }
347    
348                    @Override
349                    public boolean equals(Object obj) {
350                            if (!(obj instanceof Entry))
351                                    return false;
352                            Entry<?, ?> e = (Entry<?, ?>)obj;
353                            return (
354                                    (key == null ? e.getKey() == null : key.equals(e.getKey()))  &&
355                                    (value == null ? e.getValue() == null : value.equals(e.getValue()))
356                            );
357                    }
358            }
359    }