621bc13684a89b0ef7c542b2168b6b950c5f35c8
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / XNv1.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17
18 /**
19  * Externalizable proxy for {@link XMLNamespace}.
20  */
21 final class XNv1 implements Externalizable {
22
23     private XMLNamespace namespace;
24
25     @SuppressWarnings("checkstyle:redundantModifier")
26     public XNv1() {
27         // For Externalizable
28     }
29
30     XNv1(final XMLNamespace namespace) {
31         this.namespace = requireNonNull(namespace);
32     }
33
34     @Override
35     public void writeExternal(final ObjectOutput out) throws IOException {
36         out.writeUTF(namespace.toString());
37     }
38
39     @Override
40     public void readExternal(final ObjectInput in) throws IOException {
41         try {
42             namespace = XMLNamespace.of(in.readUTF());
43         } catch (IllegalArgumentException e) {
44             throw new IOException("Invalid namespace", e);
45         }
46     }
47
48     Object readResolve() {
49         return verifyNotNull(namespace);
50     }
51 }