001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.xbean.propertyeditor;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022
023public final class Primitives {
024    private static final Map<Class, Class> PRIMITIVE_TO_WRAPPER;
025    private static final Map<Class, Class> WRAPPER_TO_PRIMITIVE;
026
027    static {
028        {
029            final Map<Class, Class> map = new HashMap<Class, Class>();
030            map.put(boolean.class, Boolean.class);
031            map.put(char.class, Character.class);
032            map.put(byte.class, Byte.class);
033            map.put(short.class, Short.class);
034            map.put(int.class, Integer.class);
035            map.put(long.class, Long.class);
036            map.put(float.class, Float.class);
037            map.put(double.class, Double.class);
038            PRIMITIVE_TO_WRAPPER = Collections.unmodifiableMap(map);
039        }
040
041        {
042            final Map<Class, Class> map = new HashMap<Class, Class>();
043            map.put(Boolean.class, boolean.class);
044            map.put(Character.class, char.class);
045            map.put(Byte.class, byte.class);
046            map.put(Short.class, short.class);
047            map.put(Integer.class, int.class);
048            map.put(Long.class, long.class);
049            map.put(Float.class, float.class);
050            map.put(Double.class, double.class);
051            WRAPPER_TO_PRIMITIVE = Collections.unmodifiableMap(map);
052        }
053    }
054
055    public static Class<?> findSibling(final Class<?> wrapperOrPrimitive) {
056        final Class aClass = PRIMITIVE_TO_WRAPPER.get(wrapperOrPrimitive);
057        return aClass != null ? aClass : WRAPPER_TO_PRIMITIVE.get(wrapperOrPrimitive);
058    }
059
060    public static Class<?> toWrapper(final Class<?> primitive) {
061        return PRIMITIVE_TO_WRAPPER.get(primitive);
062    }
063
064    public static Class<?> toPrimitive(final Class<?> wrapper) {
065        return WRAPPER_TO_PRIMITIVE.get(wrapper);
066    }
067
068    private Primitives() {
069        // no-op
070    }
071}