// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

// THIS FILE WAS AUTO-GENERATED BY ./ntcore/generate_topics.py. DO NOT MODIFY

package org.wpilib.networktables;

import java.util.Objects;

/** A network table entry value. */
@SuppressWarnings("UnnecessaryParentheses")
public final class NetworkTableValue {
  NetworkTableValue(NetworkTableType type, Object value, long time, long serverTime) {
    m_type = type;
    m_value = value;
    m_time = time;
    m_serverTime = serverTime;
  }

  NetworkTableValue(NetworkTableType type, Object value, long time) {
    this(type, value, time, time == 0 ? 0 : 1);
  }

  NetworkTableValue(NetworkTableType type, Object value) {
    this(type, value, NetworkTablesJNI.now(), 1);
  }

  NetworkTableValue(int type, Object value, long time, long serverTime) {
    this(NetworkTableType.getFromInt(type), value, time, serverTime);
  }

  /**
   * Get the data type.
   *
   * @return The type.
   */
  public NetworkTableType getType() {
    return m_type;
  }

  /**
   * Get the data value stored.
   *
   * @return The type.
   */
  public Object getValue() {
    return m_value;
  }

  /**
   * Get the creation time of the value in local time.
   *
   * @return The time, in the units returned by NetworkTablesJNI.now().
   */
  public long getTime() {
    return m_time;
  }

  /**
   * Get the creation time of the value in server time.
   *
   * @return The server time.
   */
  public long getServerTime() {
    return m_serverTime;
  }

  /*
   * Type Checkers
   */

  /**
   * Determine if entry value contains a value or is unassigned.
   *
   * @return True if the entry value contains a value.
   */
  public boolean isValid() {
    return m_type != NetworkTableType.UNASSIGNED;
  }

  /**
   * Determine if entry value contains a boolean.
   *
   * @return True if the entry value is of boolean type.
   */
  public boolean isBoolean() {
    return m_type == NetworkTableType.BOOLEAN;
  }

  /**
   * Determine if entry value contains a long.
   *
   * @return True if the entry value is of long type.
   */
  public boolean isInteger() {
    return m_type == NetworkTableType.INTEGER;
  }

  /**
   * Determine if entry value contains a float.
   *
   * @return True if the entry value is of float type.
   */
  public boolean isFloat() {
    return m_type == NetworkTableType.FLOAT;
  }

  /**
   * Determine if entry value contains a double.
   *
   * @return True if the entry value is of double type.
   */
  public boolean isDouble() {
    return m_type == NetworkTableType.DOUBLE;
  }

  /**
   * Determine if entry value contains a String.
   *
   * @return True if the entry value is of String type.
   */
  public boolean isString() {
    return m_type == NetworkTableType.STRING;
  }

  /**
   * Determine if entry value contains a byte[].
   *
   * @return True if the entry value is of byte[] type.
   */
  public boolean isRaw() {
    return m_type == NetworkTableType.RAW;
  }

  /**
   * Determine if entry value contains a boolean[].
   *
   * @return True if the entry value is of boolean[] type.
   */
  public boolean isBooleanArray() {
    return m_type == NetworkTableType.BOOLEAN_ARRAY;
  }

  /**
   * Determine if entry value contains a long[].
   *
   * @return True if the entry value is of long[] type.
   */
  public boolean isIntegerArray() {
    return m_type == NetworkTableType.INTEGER_ARRAY;
  }

  /**
   * Determine if entry value contains a float[].
   *
   * @return True if the entry value is of float[] type.
   */
  public boolean isFloatArray() {
    return m_type == NetworkTableType.FLOAT_ARRAY;
  }

  /**
   * Determine if entry value contains a double[].
   *
   * @return True if the entry value is of double[] type.
   */
  public boolean isDoubleArray() {
    return m_type == NetworkTableType.DOUBLE_ARRAY;
  }

  /**
   * Determine if entry value contains a String[].
   *
   * @return True if the entry value is of String[] type.
   */
  public boolean isStringArray() {
    return m_type == NetworkTableType.STRING_ARRAY;
  }

  /*
   * Type-Safe Getters
   */

  /**
   * Get the boolean value.
   *
   * @return The boolean value.
   * @throws ClassCastException if the entry value is not of boolean type.
   */
  public boolean getBoolean() {
    if (m_type != NetworkTableType.BOOLEAN) {
      throw new ClassCastException("cannot convert " + m_type + " to boolean");
    }
    return (Boolean) m_value;
  }

  /**
   * Get the long value.
   *
   * @return The long value.
   * @throws ClassCastException if the entry value is not of long type.
   */
  public long getInteger() {
    if (m_type != NetworkTableType.INTEGER) {
      throw new ClassCastException("cannot convert " + m_type + " to long");
    }
    return ((Number) m_value).longValue();
  }

  /**
   * Get the float value.
   *
   * @return The float value.
   * @throws ClassCastException if the entry value is not of float type.
   */
  public float getFloat() {
    if (m_type != NetworkTableType.FLOAT) {
      throw new ClassCastException("cannot convert " + m_type + " to float");
    }
    return ((Number) m_value).floatValue();
  }

  /**
   * Get the double value.
   *
   * @return The double value.
   * @throws ClassCastException if the entry value is not of double type.
   */
  public double getDouble() {
    if (m_type != NetworkTableType.DOUBLE) {
      throw new ClassCastException("cannot convert " + m_type + " to double");
    }
    return ((Number) m_value).doubleValue();
  }

  /**
   * Get the String value.
   *
   * @return The String value.
   * @throws ClassCastException if the entry value is not of String type.
   */
  public String getString() {
    if (m_type != NetworkTableType.STRING) {
      throw new ClassCastException("cannot convert " + m_type + " to String");
    }
    return (String) m_value;
  }

  /**
   * Get the byte[] value.
   *
   * @return The byte[] value.
   * @throws ClassCastException if the entry value is not of byte[] type.
   */
  public byte[] getRaw() {
    if (m_type != NetworkTableType.RAW) {
      throw new ClassCastException("cannot convert " + m_type + " to byte[]");
    }
    return (byte[]) m_value;
  }

  /**
   * Get the boolean[] value.
   *
   * @return The boolean[] value.
   * @throws ClassCastException if the entry value is not of boolean[] type.
   */
  public boolean[] getBooleanArray() {
    if (m_type != NetworkTableType.BOOLEAN_ARRAY) {
      throw new ClassCastException("cannot convert " + m_type + " to boolean[]");
    }
    return (boolean[]) m_value;
  }

  /**
   * Get the long[] value.
   *
   * @return The long[] value.
   * @throws ClassCastException if the entry value is not of long[] type.
   */
  public long[] getIntegerArray() {
    if (m_type != NetworkTableType.INTEGER_ARRAY) {
      throw new ClassCastException("cannot convert " + m_type + " to long[]");
    }
    return (long[]) m_value;
  }

  /**
   * Get the float[] value.
   *
   * @return The float[] value.
   * @throws ClassCastException if the entry value is not of float[] type.
   */
  public float[] getFloatArray() {
    if (m_type != NetworkTableType.FLOAT_ARRAY) {
      throw new ClassCastException("cannot convert " + m_type + " to float[]");
    }
    return (float[]) m_value;
  }

  /**
   * Get the double[] value.
   *
   * @return The double[] value.
   * @throws ClassCastException if the entry value is not of double[] type.
   */
  public double[] getDoubleArray() {
    if (m_type != NetworkTableType.DOUBLE_ARRAY) {
      throw new ClassCastException("cannot convert " + m_type + " to double[]");
    }
    return (double[]) m_value;
  }

  /**
   * Get the String[] value.
   *
   * @return The String[] value.
   * @throws ClassCastException if the entry value is not of String[] type.
   */
  public String[] getStringArray() {
    if (m_type != NetworkTableType.STRING_ARRAY) {
      throw new ClassCastException("cannot convert " + m_type + " to String[]");
    }
    return (String[]) m_value;
  }

  /*
   * Factory functions.
   */

  /**
   * Creates a boolean value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeBoolean(boolean value) {
    return new NetworkTableValue(NetworkTableType.BOOLEAN, Boolean.valueOf(value));
  }

  /**
   * Creates a boolean value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeBoolean(boolean value, long time) {
    return new NetworkTableValue(NetworkTableType.BOOLEAN, Boolean.valueOf(value), time);
  }

  /**
   * Creates a long value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeInteger(long value) {
    return new NetworkTableValue(NetworkTableType.INTEGER, Long.valueOf(value));
  }

  /**
   * Creates a long value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeInteger(long value, long time) {
    return new NetworkTableValue(NetworkTableType.INTEGER, Long.valueOf(value), time);
  }

  /**
   * Creates a float value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeFloat(float value) {
    return new NetworkTableValue(NetworkTableType.FLOAT, Float.valueOf(value));
  }

  /**
   * Creates a float value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeFloat(float value, long time) {
    return new NetworkTableValue(NetworkTableType.FLOAT, Float.valueOf(value), time);
  }

  /**
   * Creates a double value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeDouble(double value) {
    return new NetworkTableValue(NetworkTableType.DOUBLE, Double.valueOf(value));
  }

  /**
   * Creates a double value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeDouble(double value, long time) {
    return new NetworkTableValue(NetworkTableType.DOUBLE, Double.valueOf(value), time);
  }

  /**
   * Creates a String value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeString(String value) {
    return new NetworkTableValue(NetworkTableType.STRING, (value));
  }

  /**
   * Creates a String value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeString(String value, long time) {
    return new NetworkTableValue(NetworkTableType.STRING, (value), time);
  }

  /**
   * Creates a byte[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeRaw(byte[] value) {
    return new NetworkTableValue(NetworkTableType.RAW, (value));
  }

  /**
   * Creates a byte[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeRaw(byte[] value, long time) {
    return new NetworkTableValue(NetworkTableType.RAW, (value), time);
  }

  /**
   * Creates a boolean[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeBooleanArray(boolean[] value) {
    return new NetworkTableValue(NetworkTableType.BOOLEAN_ARRAY, (value));
  }

  /**
   * Creates a boolean[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeBooleanArray(boolean[] value, long time) {
    return new NetworkTableValue(NetworkTableType.BOOLEAN_ARRAY, (value), time);
  }

  /**
   * Creates a boolean[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeBooleanArray(Boolean[] value) {
    return new NetworkTableValue(NetworkTableType.BOOLEAN_ARRAY, toNativeBooleanArray(value));
  }

  /**
   * Creates a boolean[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeBooleanArray(Boolean[] value, long time) {
    return new NetworkTableValue(NetworkTableType.BOOLEAN_ARRAY, toNativeBooleanArray(value), time);
  }

  /**
   * Creates a long[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeIntegerArray(long[] value) {
    return new NetworkTableValue(NetworkTableType.INTEGER_ARRAY, (value));
  }

  /**
   * Creates a long[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeIntegerArray(long[] value, long time) {
    return new NetworkTableValue(NetworkTableType.INTEGER_ARRAY, (value), time);
  }

  /**
   * Creates a long[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeIntegerArray(Long[] value) {
    return new NetworkTableValue(NetworkTableType.INTEGER_ARRAY, toNativeIntegerArray(value));
  }

  /**
   * Creates a long[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeIntegerArray(Long[] value, long time) {
    return new NetworkTableValue(NetworkTableType.INTEGER_ARRAY, toNativeIntegerArray(value), time);
  }

  /**
   * Creates a float[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeFloatArray(float[] value) {
    return new NetworkTableValue(NetworkTableType.FLOAT_ARRAY, (value));
  }

  /**
   * Creates a float[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeFloatArray(float[] value, long time) {
    return new NetworkTableValue(NetworkTableType.FLOAT_ARRAY, (value), time);
  }

  /**
   * Creates a float[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeFloatArray(Float[] value) {
    return new NetworkTableValue(NetworkTableType.FLOAT_ARRAY, toNativeFloatArray(value));
  }

  /**
   * Creates a float[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeFloatArray(Float[] value, long time) {
    return new NetworkTableValue(NetworkTableType.FLOAT_ARRAY, toNativeFloatArray(value), time);
  }

  /**
   * Creates a double[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeDoubleArray(double[] value) {
    return new NetworkTableValue(NetworkTableType.DOUBLE_ARRAY, (value));
  }

  /**
   * Creates a double[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeDoubleArray(double[] value, long time) {
    return new NetworkTableValue(NetworkTableType.DOUBLE_ARRAY, (value), time);
  }

  /**
   * Creates a double[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeDoubleArray(Double[] value) {
    return new NetworkTableValue(NetworkTableType.DOUBLE_ARRAY, toNativeDoubleArray(value));
  }

  /**
   * Creates a double[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeDoubleArray(Double[] value, long time) {
    return new NetworkTableValue(NetworkTableType.DOUBLE_ARRAY, toNativeDoubleArray(value), time);
  }

  /**
   * Creates a String[] value.
   *
   * @param value the value
   * @return The entry value
   */
  public static NetworkTableValue makeStringArray(String[] value) {
    return new NetworkTableValue(NetworkTableType.STRING_ARRAY, (value));
  }

  /**
   * Creates a String[] value.
   *
   * @param value the value
   * @param time the creation time to use (instead of the current time)
   * @return The entry value
   */
  public static NetworkTableValue makeStringArray(String[] value, long time) {
    return new NetworkTableValue(NetworkTableType.STRING_ARRAY, (value), time);
  }

  @Override
  public boolean equals(Object other) {
    return other == this
        || other instanceof NetworkTableValue ntOther
            && m_type == ntOther.m_type
            && m_value.equals(ntOther.m_value);
  }

  @Override
  public int hashCode() {
    return Objects.hash(m_type, m_value);
  }

  // arraycopy() doesn't know how to unwrap boxed values; this is a false positive in PMD
  // (see https://sourceforge.net/p/pmd/bugs/804/)
  @SuppressWarnings("PMD.AvoidArrayLoops")
  static boolean[] toNativeBooleanArray(Boolean[] arr) {
    boolean[] out = new boolean[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i];
    }
    return out;
  }

  static double[] toNativeDoubleArray(Number[] arr) {
    double[] out = new double[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i].doubleValue();
    }
    return out;
  }

  static long[] toNativeIntegerArray(Number[] arr) {
    long[] out = new long[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i].longValue();
    }
    return out;
  }

  static float[] toNativeFloatArray(Number[] arr) {
    float[] out = new float[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i].floatValue();
    }
    return out;
  }

  @SuppressWarnings("PMD.AvoidArrayLoops")
  static Boolean[] fromNativeBooleanArray(boolean[] arr) {
    Boolean[] out = new Boolean[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i];
    }
    return out;
  }

  @SuppressWarnings("PMD.AvoidArrayLoops")
  static Long[] fromNativeIntegerArray(long[] arr) {
    Long[] out = new Long[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i];
    }
    return out;
  }

  @SuppressWarnings("PMD.AvoidArrayLoops")
  static Float[] fromNativeFloatArray(float[] arr) {
    Float[] out = new Float[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i];
    }
    return out;
  }

  @SuppressWarnings("PMD.AvoidArrayLoops")
  static Double[] fromNativeDoubleArray(double[] arr) {
    Double[] out = new Double[arr.length];
    for (int i = 0; i < arr.length; i++) {
      out[i] = arr[i];
    }
    return out;
  }

  private NetworkTableType m_type;
  private Object m_value;
  private long m_time;
  private long m_serverTime;
}
