Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2009 Google Inc. |
| 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 | |
| 17 | package com.google.caliper; |
| 18 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import java.lang.reflect.Method; |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 21 | import java.lang.reflect.Type; |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 22 | import java.util.Map; |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Convert objects to and from Strings. |
| 26 | */ |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 27 | final class TypeConverter { |
| 28 | private TypeConverter() {} |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 29 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 30 | public static Object fromString(String value, Type type) { |
| 31 | Class<?> c = wrap((Class<?>) type); |
| 32 | try { |
| 33 | Method m = c.getMethod("valueOf", String.class); |
| 34 | return m.invoke(null, value); |
| 35 | } catch (Exception e) { |
| 36 | throw new UnsupportedOperationException( |
| 37 | "Cannot convert " + value + " of type " + type, e); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // safe because both Long.class and long.class are of type Class<Long> |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 42 | @SuppressWarnings("unchecked") |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 43 | private static <T> Class<T> wrap(Class<T> c) { |
| 44 | return c.isPrimitive() ? (Class<T>) PRIMITIVES_TO_WRAPPERS.get(c) : c; |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 47 | private static final Map<Class<?>, Class<?>> PRIMITIVES_TO_WRAPPERS |
| 48 | = new ImmutableMap.Builder<Class<?>, Class<?>>() |
| 49 | .put(boolean.class, Boolean.class) |
| 50 | .put(byte.class, Byte.class) |
| 51 | .put(char.class, Character.class) |
| 52 | .put(double.class, Double.class) |
| 53 | .put(float.class, Float.class) |
| 54 | .put(int.class, Integer.class) |
| 55 | .put(long.class, Long.class) |
| 56 | .put(short.class, Short.class) |
| 57 | .put(void.class, Void.class) |
| 58 | .build(); |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 59 | } |