blob: 663ddc5bcf76a8ceef550f33178b39d1a536c607 [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
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053017package org.onosproject.yangutils.datamodel.utils.builtindatatype;
Bharat saraswal33dfa012016-05-17 19:59:16 +053018
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Bharat saraswal33dfa012016-05-17 19:59:16 +053020import java.util.Objects;
Bharat saraswal96dfef02016-06-16 00:29:12 +053021
Bharat saraswal33dfa012016-05-17 19:59:16 +053022import com.google.common.base.MoreObjects;
23
24/**
25 * Represents binary data type.
26 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053027public final class YangBinary implements Serializable {
28
29 private static final long serialVersionUID = 8006201670L;
Bharat saraswal33dfa012016-05-17 19:59:16 +053030
31 private byte[] byteArray;
32
33 /**
34 * Creates an instance of YANG binary.
35 */
36 private YangBinary() {
37 }
38
39 /**
40 * Creates an instance of YANG binary.
41 *
42 * @param bytes byte array
43 */
44 public YangBinary(byte[] bytes) {
Bharat saraswal96dfef02016-06-16 00:29:12 +053045 byteArray = bytes;
Bharat saraswal33dfa012016-05-17 19:59:16 +053046 }
47
48 /**
49 * Returns object of YANG binary.
50 *
51 * @param bytes byte array
52 * @return object of YANG binary
53 */
54 public static YangBinary of(byte[] bytes) {
55 return new YangBinary(bytes);
56 }
57
58 /**
59 * Returns byte array.
60 *
61 * @return byte array
62 */
63 public byte[] byteArray() {
64 return byteArray;
65 }
66
67 @Override
68 public int hashCode() {
69 return Objects.hash(byteArray);
70 }
71
72 @Override
73 public boolean equals(Object obj) {
74 if (this == obj) {
75 return true;
76 }
77 if (obj instanceof YangBinary) {
78 YangBinary other = (YangBinary) obj;
79 return Objects.equals(byteArray, other.byteArray);
80 }
81 return false;
82 }
83
84 @Override
85 public String toString() {
86 return MoreObjects.toStringHelper(getClass())
87 .omitNullValues()
88 .add("byteArray", byteArray)
89 .toString();
90 }
91
92 /**
93 * Returns the object of YANG binary fromString input String.
94 *
95 * @param valInString input String
96 * @return Object of YANG binary
97 */
98 public static YangBinary fromString(String valInString) {
99 try {
100 byte[] tmpVal = valInString.getBytes();
101 return of(tmpVal);
102 } catch (Exception e) {
103 }
104 return null;
105 }
106
107}