All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.wpm.codegen.utils.Primitives Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * Licensed 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 net.wpm.codegen.utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import net.wpm.codegen.utils.Preconditions;

public class Primitives {
	private static final Map, Class> PRIMITIVE;
	private static final Map, Class> WRAPPER;

	private Primitives() {}

	static {
		Map, Class> primToWrap = new HashMap<>();
		Map, Class> wrapToPrim = new HashMap<>();

		add(primToWrap, wrapToPrim, Boolean.TYPE, Boolean.class);
		add(primToWrap, wrapToPrim, Byte.TYPE, Byte.class);
		add(primToWrap, wrapToPrim, Character.TYPE, Character.class);
		add(primToWrap, wrapToPrim, Double.TYPE, Double.class);
		add(primToWrap, wrapToPrim, Float.TYPE, Float.class);
		add(primToWrap, wrapToPrim, Integer.TYPE, Integer.class);
		add(primToWrap, wrapToPrim, Long.TYPE, Long.class);
		add(primToWrap, wrapToPrim, Short.TYPE, Short.class);
		add(primToWrap, wrapToPrim, Void.TYPE, Void.class);

		PRIMITIVE = Collections.unmodifiableMap(primToWrap);
		WRAPPER = Collections.unmodifiableMap(wrapToPrim);
	}

	private static void add(Map, Class> forward, Map, Class> backward, Class key, Class value) {
		forward.put(key, value);
		backward.put(value, key);
	}

	public static Set> allPrimitiveTypes() {
		return PRIMITIVE.keySet();
	}

	public static Set> allWrapperTypes() {
		return WRAPPER.keySet();
	}

	public static boolean isWrapperType(Class type) {
		return WRAPPER.containsKey(Preconditions.checkNotNull(type));
	}

	@SuppressWarnings("unchecked")
	public static  Class wrap(Class type) {
		Preconditions.checkNotNull(type);
		Class wrapped = (Class) PRIMITIVE.get(type);
		return wrapped == null ? type : wrapped;
	}

	@SuppressWarnings("unchecked")
	public static  Class unwrap(Class type) {
		Preconditions.checkNotNull(type);
		Class unwrapped = (Class) WRAPPER.get(type);
		return unwrapped == null ? type : unwrapped;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy