blob: cece4ad344ebdf711e5b38b6254ea1b56f1b47b2 [file] [log] [blame]
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +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;
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 }
81 default: {
82 throw new DataTypeException("YANG file error : Unsupported data type");
83 }
84 }
85
86 }
87
88}