Add UniqueValidation
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / BinaryValue.java
1 /*
2  * Copyright (c) 2020 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.data.impl.schema.tree;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Arrays;
13 import java.util.Base64;
14 import org.opendaylight.yangtools.concepts.Immutable;
15
16 final class BinaryValue implements Immutable {
17     private final byte[] value;
18
19     private BinaryValue(final byte[] value) {
20         this.value = requireNonNull(value);
21     }
22
23     static Object wrap(final Object value) {
24         return value instanceof byte[] ? new BinaryValue((byte[]) value) : value;
25     }
26
27     static Object wrapToString(final Object value) {
28         return value instanceof byte[] ? toString((byte[]) value) : value;
29     }
30
31     @Override
32     public int hashCode() {
33         return Arrays.hashCode(value);
34     }
35
36     @Override
37     public boolean equals(final Object obj) {
38         return obj == this || obj instanceof BinaryValue && Arrays.equals(value, ((BinaryValue) obj).value);
39     }
40
41     @Override
42     public String toString() {
43         return toString(value);
44     }
45
46     private static String toString(final byte[] value) {
47         return Base64.getEncoder().encodeToString(value);
48     }
49 }