/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.server.http.dynamic;
/**
* Provides deserializing functionality for reflection.
*
* @see Path
* @see PathParam
* @see DynamicHttpServiceBuilder
*/
final class Deserializers {
/**
* Deserialize given {@code str} to {@code T} type object. e.g., "42" -> 42.
*
* @throws IllegalArgumentException if {@code str} can't be deserialized to {@code T} type object.
*/
@SuppressWarnings("unchecked")
static <T> T deserialize(String str, Class<T> clazz) {
try {
if (clazz == Byte.TYPE) {
return (T) Byte.valueOf(str);
} else if (clazz == Short.TYPE) {
return (T) Short.valueOf(str);
} else if (clazz == Boolean.TYPE) {
return (T) Boolean.valueOf(str);
} else if (clazz == Integer.TYPE) {
return (T) Integer.valueOf(str);
} else if (clazz == Long.TYPE) {
return (T) Long.valueOf(str);
} else if (clazz == Float.TYPE) {
return (T) Float.valueOf(str);
} else if (clazz == Double.TYPE) {
return (T) Double.valueOf(str);
} else if (clazz == String.class) {
return (T) str;
} else {
throw new IllegalArgumentException(
"Class " + clazz.getSimpleName() + " can't be deserialized.");
}
} catch (Exception e) {
throw new IllegalArgumentException(
"Can't deserialize " + str + " to type " + clazz.getSimpleName(), e);
}
}
private Deserializers() {}
}