BUG-7464: Split TrieMap into read-only and read-write
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / SerializationProxy.java
1 /*
2  * (C) Copyright 2017 Pantheon Technologies, s.r.o. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import static com.google.common.base.Preconditions.checkArgument;
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.base.Verify;
22 import java.io.Externalizable;
23 import java.io.IOException;
24 import java.io.ObjectInput;
25 import java.io.ObjectOutput;
26 import java.io.ObjectStreamException;
27 import java.util.Map.Entry;
28
29 /**
30  * External serialization object for use with TrieMap objects. This hides the implementation details, such as object
31  * hierarchy. It also makes handling read-only snapshots more elegant.
32  *
33  * @author Robert Varga
34  */
35 final class SerializationProxy implements Externalizable {
36     private static final long serialVersionUID = 1L;
37
38     private TrieMap<Object, Object> map;
39     private boolean readOnly;
40
41     public SerializationProxy() {
42         // For Externalizable
43     }
44
45     @SuppressWarnings({ "unchecked", "rawtypes" })
46     SerializationProxy(final ImmutableTrieMap<?, ?> map, final boolean readOnly) {
47         this.map = (TrieMap) checkNotNull(map);
48         this.readOnly = readOnly;
49     }
50
51     @Override
52     public void writeExternal(final ObjectOutput out) throws IOException {
53         out.writeObject(map.equiv());
54         out.writeInt(map.size());
55         for (Entry<Object, Object> e : map.entrySet()) {
56             out.writeObject(e.getKey());
57             out.writeObject(e.getValue());
58         }
59         out.writeBoolean(readOnly);
60     }
61
62     @Override
63     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
64         @SuppressWarnings("unchecked")
65         final Equivalence<Object> equiv = (Equivalence<Object>) in.readObject();
66         checkArgument(equiv != null);
67
68         final MutableTrieMap<Object, Object> tmp = new MutableTrieMap<>(equiv);
69         final int size = in.readInt();
70         checkArgument(size >= 0);
71
72         for (int i = 0; i < size; ++i) {
73             tmp.add(in.readObject(), in.readObject());
74         }
75
76         map = in.readBoolean() ? tmp.immutableSnapshot() : tmp;
77     }
78
79     private Object readResolve() throws ObjectStreamException {
80         return Verify.verifyNotNull(map);
81     }
82 }