Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 1 | /*- |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 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 | package org.onosproject.yangutils.datamodel; |
| 17 | |
| 18 | import java.io.Serializable; |
| 19 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; |
| 20 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
| 21 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
| 22 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt16; |
| 23 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt32; |
| 24 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt64; |
| 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt8; |
| 26 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint16; |
| 27 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint32; |
| 28 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64; |
| 29 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint8; |
| 30 | |
| 31 | /** |
| 32 | * Factory to create an object of required type. |
| 33 | */ |
| 34 | public 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 Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 81 | case DECIMAL64: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 82 | return (T) new YangDecimal64(valueInStr); |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 83 | } |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 84 | default: { |
| 85 | throw new DataTypeException("YANG file error : Unsupported data type"); |
| 86 | } |
| 87 | } |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 88 | } |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 89 | } |