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

com.almondtools.testrecorder.values.SerializedLiteral Maven / Gradle / Ivy

package com.almondtools.testrecorder.values;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.almondtools.testrecorder.SerializedValue;
import com.almondtools.testrecorder.SerializedValueVisitor;
import com.almondtools.testrecorder.visitors.SerializedValuePrinter;

public class SerializedLiteral implements SerializedValue {

	public static Set> LITERAL_TYPES = new HashSet<>(Arrays.asList(
		boolean.class, char.class, byte.class, short.class, int.class, float.class, long.class, double.class,
		Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Float.class, Long.class, Double.class,
		String.class));

	private static final Map KNOWN_LITERALS = new HashMap<>();

	private Type type;
	private Object value;

	private SerializedLiteral(Type type, Object value) {
		this.type = type;
		this.value = value;
	}

	public static boolean isLiteral(Type type) {
		return LITERAL_TYPES.contains(type);
	}

	public static SerializedLiteral literal(Type type, Object value) {
		return KNOWN_LITERALS.computeIfAbsent(value, val -> new SerializedLiteral(type, val));
	}

	@Override
	public Type getType() {
		return type;
	}
	
	@Override
	public Class getValueType() {
		return value.getClass();
	}

	public Object getValue() {
		return value;
	}

	@Override
	public  T accept(SerializedValueVisitor visitor) {
		return visitor.visitLiteral(this);
	}

	@Override
	public String toString() {
		return accept(new SerializedValuePrinter());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy