blob: 51747f3d16b235d61e32b67dd74b3eb09b1de3e7 [file] [log] [blame]
Bharat saraswal33dfa012016-05-17 19:59:16 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.yangutils.utils.builtindatatype;
18
19import java.util.Objects;
20
21import com.google.common.base.MoreObjects;
22
23/**
24 * Represents binary data type.
25 */
26public final class YangBinary {
27
28 private byte[] byteArray;
29
30 /**
31 * Creates an instance of YANG binary.
32 */
33 private YangBinary() {
34 }
35
36 /**
37 * Creates an instance of YANG binary.
38 *
39 * @param bytes byte array
40 */
41 public YangBinary(byte[] bytes) {
42 this.byteArray = bytes;
43 }
44
45 /**
46 * Returns object of YANG binary.
47 *
48 * @param bytes byte array
49 * @return object of YANG binary
50 */
51 public static YangBinary of(byte[] bytes) {
52 return new YangBinary(bytes);
53 }
54
55 /**
56 * Returns byte array.
57 *
58 * @return byte array
59 */
60 public byte[] byteArray() {
61 return byteArray;
62 }
63
64 @Override
65 public int hashCode() {
66 return Objects.hash(byteArray);
67 }
68
69 @Override
70 public boolean equals(Object obj) {
71 if (this == obj) {
72 return true;
73 }
74 if (obj instanceof YangBinary) {
75 YangBinary other = (YangBinary) obj;
76 return Objects.equals(byteArray, other.byteArray);
77 }
78 return false;
79 }
80
81 @Override
82 public String toString() {
83 return MoreObjects.toStringHelper(getClass())
84 .omitNullValues()
85 .add("byteArray", byteArray)
86 .toString();
87 }
88
89 /**
90 * Returns the object of YANG binary fromString input String.
91 *
92 * @param valInString input String
93 * @return Object of YANG binary
94 */
95 public static YangBinary fromString(String valInString) {
96 try {
97 byte[] tmpVal = valInString.getBytes();
98 return of(tmpVal);
99 } catch (Exception e) {
100 }
101 return null;
102 }
103
104}