blob: 1f28565c28549b66792b116d52f769d0afeaa95a [file] [log] [blame]
Bharat saraswal96dfef02016-06-16 00:29:12 +05301/*-
2 * Copyright 2016 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 */
16package org.onosproject.yangutils.datamodel.utils.builtindatatype;
17
18import java.io.Serializable;
19
20import org.onosproject.yangutils.datamodel.YangDataTypes;
21
22/**
23 * Factory to create an object of required type.
24 */
25public final class BuiltInTypeObjectFactory implements Serializable {
26
27 private static final long serialVersionUID = 8006201671L;
28
29 /**
30 * Utility factory class, hence the object creation is forbidden.
31 */
32 private BuiltInTypeObjectFactory() {
33 }
34
35 /**
36 * Given the value represented in string return the corresponding types
37 * object with the value initialized.
38 *
39 * @param valueInStr value represented in string
40 * @param builtInType built in data type
41 * @param <T> the data type of the target object
42 * @return the target data type object with the value initialized
43 */
44 public static <T extends YangBuiltInDataTypeInfo<?>> T getDataObjectFromString(String valueInStr,
45 YangDataTypes builtInType) {
46
47 switch (builtInType) {
48 case INT8: {
49 return (T) new YangInt8(valueInStr);
50 }
51 case INT16: {
52 return (T) new YangInt16(valueInStr);
53 }
54 case INT32: {
55 return (T) new YangInt32(valueInStr);
56 }
57 case INT64: {
58 return (T) new YangInt64(valueInStr);
59 }
60 case UINT8: {
61 return (T) new YangUint8(valueInStr);
62 }
63 case UINT16: {
64 return (T) new YangUint16(valueInStr);
65 }
66 case UINT32: {
67 return (T) new YangUint32(valueInStr);
68 }
69 case UINT64: {
70 return (T) new YangUint64(valueInStr);
71 }
72 default: {
73 throw new DataTypeException("YANG file error : Unsupported data type");
74 }
75 }
76
77 }
78
79}