001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.hal;
006
007public final class HALValue {
008  public static final int kUnassigned = 0;
009  public static final int kBoolean = 0x01;
010  public static final int kDouble = 0x02;
011  public static final int kEnum = 0x04;
012  public static final int kInt = 0x08;
013  public static final int kLong = 0x10;
014
015  private int m_type;
016  private long m_long;
017  private double m_double;
018
019  private HALValue(double value, int type) {
020    m_type = type;
021    m_double = value;
022  }
023
024  private HALValue(long value, int type) {
025    m_type = type;
026    m_long = value;
027  }
028
029  private HALValue() {}
030
031  /**
032   * Get the type of the value.
033   *
034   * @return Type (e.g. kBoolean).
035   */
036  public int getType() {
037    return m_type;
038  }
039
040  /**
041   * Get the value as a boolean. Does not perform type checking.
042   *
043   * @return value contents
044   */
045  public boolean getBoolean() {
046    return m_long != 0;
047  }
048
049  /**
050   * Get the value as a long. Does not perform type checking.
051   *
052   * @return value contents
053   */
054  public long getLong() {
055    return m_long;
056  }
057
058  /**
059   * Get the value as a double. Does not perform type checking.
060   *
061   * @return value contents
062   */
063  public double getDouble() {
064    return m_double;
065  }
066
067  /**
068   * Get the native long value. Does not perform type checking.
069   *
070   * @return value contents
071   */
072  public long getNativeLong() {
073    return m_long;
074  }
075
076  /**
077   * Get the native double value. Does not perform type checking.
078   *
079   * @return value contents
080   */
081  public double getNativeDouble() {
082    return m_double;
083  }
084
085  /**
086   * Build a HAL boolean value.
087   *
088   * @param value value
089   * @return HAL value
090   */
091  public static HALValue makeBoolean(boolean value) {
092    return new HALValue(value ? 1 : 0, kBoolean);
093  }
094
095  /**
096   * Build a HAL enum value.
097   *
098   * @param value value
099   * @return HAL value
100   */
101  public static HALValue makeEnum(int value) {
102    return new HALValue(value, kEnum);
103  }
104
105  /**
106   * Build a HAL integer value.
107   *
108   * @param value value
109   * @return HAL value
110   */
111  public static HALValue makeInt(int value) {
112    return new HALValue(value, kInt);
113  }
114
115  /**
116   * Build a HAL long value.
117   *
118   * @param value value
119   * @return HAL value
120   */
121  public static HALValue makeLong(long value) {
122    return new HALValue(value, kLong);
123  }
124
125  /**
126   * Build a HAL double value.
127   *
128   * @param value value
129   * @return HAL value
130   */
131  public static HALValue makeDouble(double value) {
132    return new HALValue(value, kDouble);
133  }
134
135  public static HALValue makeUnassigned() {
136    return new HALValue();
137  }
138
139  /**
140   * Build a HAL value from its native components.
141   *
142   * @param type type
143   * @param value1 long value (all except double)
144   * @param value2 double value (for double only)
145   * @return HAL value
146   */
147  public static HALValue fromNative(int type, long value1, double value2) {
148    switch (type) {
149      case kBoolean:
150        return makeBoolean(value1 != 0);
151      case kDouble:
152        return makeDouble(value2);
153      case kEnum:
154        return makeEnum((int) value1);
155      case kInt:
156        return makeInt((int) value1);
157      case kLong:
158        return makeLong(value1);
159      default:
160        return makeUnassigned();
161    }
162  }
163}