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