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