Remove the object cache
[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 java.util.Objects.requireNonNull;
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     @SuppressWarnings("checkstyle:redundantModifier")
42     public SerializationProxy() {
43         // For Externalizable
44     }
45
46     @SuppressWarnings({ "unchecked", "rawtypes" })
47     SerializationProxy(final ImmutableTrieMap<?, ?> map, final boolean readOnly) {
48         this.map = (TrieMap) requireNonNull(map);
49         this.readOnly = readOnly;
50     }
51
52     @Override
53     public void writeExternal(final ObjectOutput out) throws IOException {
54         out.writeObject(map.equiv());
55         out.writeInt(map.size());
56         for (Entry<Object, Object> e : map.entrySet()) {
57             out.writeObject(e.getKey());
58             out.writeObject(e.getValue());
59         }
60         out.writeBoolean(readOnly);
61     }
62
63     @Override
64     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
65         @SuppressWarnings("unchecked")
66         final Equivalence<Object> equiv = (Equivalence<Object>) in.readObject();
67         checkArgument(equiv != null);
68
69         final MutableTrieMap<Object, Object> tmp = new MutableTrieMap<>(equiv);
70         final int size = in.readInt();
71         checkArgument(size >= 0);
72
73         for (int i = 0; i < size; ++i) {
74             tmp.add(in.readObject(), in.readObject());
75         }
76
77         map = in.readBoolean() ? tmp.immutableSnapshot() : tmp;
78     }
79
80     private Object readResolve() throws ObjectStreamException {
81         return Verify.verifyNotNull(map);
82     }
83 }