net.wpm.codegen.utils.Primitives Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of records Show documentation
Show all versions of records Show documentation
C-Struct like features for Java 6+ to improve performance.
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;
}
}