2 * (C) Copyright 2017 Pantheon Technologies, s.r.o. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.opendaylight.yangtools.triemap;
18 import static com.google.common.base.Preconditions.checkArgument;
19 import static com.google.common.base.Preconditions.checkNotNull;
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;
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.
33 * @author Robert Varga
35 final class SerializationProxy implements Externalizable {
36 private static final long serialVersionUID = 1L;
38 private TrieMap<Object, Object> map;
39 private boolean readOnly;
41 @SuppressWarnings("checkstyle:redundantModifier")
42 public SerializationProxy() {
46 @SuppressWarnings({ "unchecked", "rawtypes" })
47 SerializationProxy(final ImmutableTrieMap<?, ?> map, final boolean readOnly) {
48 this.map = (TrieMap) checkNotNull(map);
49 this.readOnly = readOnly;
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());
60 out.writeBoolean(readOnly);
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);
69 final MutableTrieMap<Object, Object> tmp = new MutableTrieMap<>(equiv);
70 final int size = in.readInt();
71 checkArgument(size >= 0);
73 for (int i = 0; i < size; ++i) {
74 tmp.add(in.readObject(), in.readObject());
77 map = in.readBoolean() ? tmp.immutableSnapshot() : tmp;
80 private Object readResolve() throws ObjectStreamException {
81 return Verify.verifyNotNull(map);