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