package de.dhbwstuttgart.server.packet.dataContainers.serialized;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class SerialValue<T> implements ISerialNode {
  public T value;
  public static final SerialValue<Object> NULL = new SerialValue<>(null);

  public SerialValue() {}

  public SerialValue(T value) {
    this.value = value;
  }

  @JsonIgnore
  public <A> SerialValue<A> assertValueOf(Class<A> targetClass) {
    if (this.value == null || targetClass.isInstance(this.value)) {
      return (SerialValue<A>) this;
    }
    throw new RuntimeException("Required Value to contain " + targetClass.getName() + " value but condition failed on" +
      " type " + this.value.getClass().getName());
  }

  @JsonIgnore
  public <A> A getOf(Class<A> targetClass) {
    return this.assertValueOf(targetClass).value;
  }
}