Monday 22 May 2023

Utility class to get primitive type from wrapper type and vice versa

 

Define an utility class to work with the following features.

1.   Get all the primitive types

2.   Get all the wrapper types

3.   Check whether the given class is a primitive or not

4.   Check whether the given class is a wrapper or not

5.   Convert given primitive type to wrapper type

6.   Convert given wrapper type to the primitive type

 

Primitive types

Java language supports 8 primitive types of data.

1.   boolean

2.   char

3.   byte

4.   short

5.   int

6.   long

7.   float

8.   double

 

Wrapper types

Every primitive type has a correspondent wrapper type in java.

 

Primitive Type

Wrapper Type

boolean

Boolean

char

Character

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

 

Find the below working application.


 

PrimitiveUtil.java

package com.sample.app.util;

import static java.util.Objects.requireNonNull;

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

public class PrimitiveUtil {
	private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE_MAP;
	private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE_MAP;

	private PrimitiveUtil() throws InstantiationException {
		throw new InstantiationException("Object creation is restricted");
	}

	static {
		Map<Class<?>, Class<?>> primitiveToWrapperMap = new HashMap<>();
		Map<Class<?>, Class<?>> wrapperToPrimitiveMap = new HashMap<>();

		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, boolean.class, Boolean.class);

		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, char.class, Character.class);

		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, byte.class, Byte.class);
		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, short.class, Short.class);
		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, int.class, Integer.class);
		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, long.class, Long.class);

		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, float.class, Float.class);
		populateMaps(primitiveToWrapperMap, wrapperToPrimitiveMap, double.class, Double.class);

		PRIMITIVE_TO_WRAPPER_TYPE_MAP = Collections.unmodifiableMap(primitiveToWrapperMap);
		WRAPPER_TO_PRIMITIVE_TYPE_MAP = Collections.unmodifiableMap(wrapperToPrimitiveMap);
	}

	private static void populateMaps(Map<Class<?>, Class<?>> primitiveToWrapperMap,
			Map<Class<?>, Class<?>> wrapperToPrimitiveMap, Class<?> primitiveClass, Class<?> wrapperClass) {
		primitiveToWrapperMap.put(primitiveClass, wrapperClass);
		wrapperToPrimitiveMap.put(wrapperClass, primitiveClass);
	}

	public static Set<Class<?>> primitiveTypes() {
		return PRIMITIVE_TO_WRAPPER_TYPE_MAP.keySet();
	}

	public static Set<Class<?>> wrapperTypes() {
		return WRAPPER_TO_PRIMITIVE_TYPE_MAP.keySet();
	}

	public static boolean isPrimitiveType(Class<?> type) {
		return type.isPrimitive();
	}

	public static boolean isWrapperType(Class<?> type) {
		return WRAPPER_TO_PRIMITIVE_TYPE_MAP.containsKey(requireNonNull(type));
	}

	@SuppressWarnings("unchecked")
	public static <T> Class<T> toWrapperType(Class<T> type) {
		Class<T> wrapperType = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE_MAP.get(requireNonNull(type));
		return (wrapperType == null) ? type : wrapperType;
	}

	@SuppressWarnings("unchecked")
	public static <T> Class<T> toPrimitiveType(Class<T> type) {
		Class<T> primitiveType = (Class<T>) WRAPPER_TO_PRIMITIVE_TYPE_MAP.get(requireNonNull(type));
		return (primitiveType == null) ? type : primitiveType;
	}
}

 

PrimitiveUtilDemo.java

package com.sample.app;

import com.sample.app.util.PrimitiveUtil;

public class PrimitiveUtilDemo {

	public static void main(String[] args) {
		// 1. Get all the primitive types
		System.out.println("Primitive types");
		PrimitiveUtil.primitiveTypes().forEach(System.out::println);

		// 2. Get all the wrapper types
		System.out.println("\nWrapper types");
		PrimitiveUtil.wrapperTypes().forEach(System.out::println);

		// 3. Check whether the given class is a primitive or not
		System.out.println("\nIs " + long.class + " primitive ? " + PrimitiveUtil.isPrimitiveType(long.class));
		System.out.println("Is " + Long.class + " primitive ? " + PrimitiveUtil.isPrimitiveType(Long.class));

		// 4. Check whether the given class is a wrapper or not
		System.out.println("\nIs " + long.class + " wrapper ? " + PrimitiveUtil.isWrapperType(long.class));
		System.out.println("Is " + Long.class + " wrapper ? " + PrimitiveUtil.isWrapperType(Long.class));

		// 5. Convert given primitive type to wrapper type
		System.out.println("\ntoWrapperType(long.class) : " + PrimitiveUtil.toWrapperType(long.class));

		// 6. Convert given wrapper type to the primitive type
		System.out.println("\ntoPrimitiveType(Long.class) : " + PrimitiveUtil.toPrimitiveType(Long.class));
	}

}

 

Output

Primitive types
int
short
long
boolean
float
byte
double
char

Wrapper types
class java.lang.Byte
class java.lang.Short
class java.lang.Integer
class java.lang.Long
class java.lang.Double
class java.lang.Character
class java.lang.Float
class java.lang.Boolean

Is long primitive ? true
Is class java.lang.Long primitive ? false

Is long wrapper ? false
Is class java.lang.Long wrapper ? true

toWrapperType(long.class) : class java.lang.Long

toPrimitiveType(Long.class) : long

 

 


You may like

Interview Questions

Implementation of Bag data structure in Java

Scanner throws java.util.NoSuchElementException while reading the input

How to get all the enum values in Java?

Extend the thread to check it’s running status

Implement retry handler for a task in Java

Design an utility class to capture application metrics summary in Java

No comments:

Post a Comment