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