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.util.datalog;
006
007/** Log double values. */
008public class DoubleLogEntry extends DataLogEntry {
009  /** The data type for double values. */
010  public static final String kDataType = "double";
011
012  /**
013   * Constructs a double log entry.
014   *
015   * @param log datalog
016   * @param name name of the entry
017   * @param metadata metadata
018   * @param timestamp entry creation timestamp (0=now)
019   */
020  public DoubleLogEntry(DataLog log, String name, String metadata, long timestamp) {
021    super(log, name, kDataType, metadata, timestamp);
022  }
023
024  /**
025   * Constructs a double log entry.
026   *
027   * @param log datalog
028   * @param name name of the entry
029   * @param metadata metadata
030   */
031  public DoubleLogEntry(DataLog log, String name, String metadata) {
032    this(log, name, metadata, 0);
033  }
034
035  /**
036   * Constructs a double log entry.
037   *
038   * @param log datalog
039   * @param name name of the entry
040   * @param timestamp entry creation timestamp (0=now)
041   */
042  public DoubleLogEntry(DataLog log, String name, long timestamp) {
043    this(log, name, "", timestamp);
044  }
045
046  /**
047   * Constructs a double log entry.
048   *
049   * @param log datalog
050   * @param name name of the entry
051   */
052  public DoubleLogEntry(DataLog log, String name) {
053    this(log, name, 0);
054  }
055
056  /**
057   * Appends a record to the log.
058   *
059   * @param value Value to record
060   * @param timestamp Time stamp (0 to indicate now)
061   */
062  public void append(double value, long timestamp) {
063    m_log.appendDouble(m_entry, value, timestamp);
064  }
065
066  /**
067   * Appends a record to the log.
068   *
069   * @param value Value to record
070   */
071  public void append(double value) {
072    m_log.appendDouble(m_entry, value, 0);
073  }
074
075  /**
076   * Updates the last value and appends a record to the log if it has changed.
077   *
078   * <p>Note: the last value is local to this class instance; using update() with two instances
079   * pointing to the same underlying log entry name will likely result in unexpected results.
080   *
081   * @param value Value to record
082   * @param timestamp Time stamp (0 to indicate now)
083   */
084  public synchronized void update(double value, long timestamp) {
085    if (!m_hasLastValue || m_lastValue != value) {
086      m_lastValue = value;
087      m_hasLastValue = true;
088      append(value, timestamp);
089    }
090  }
091
092  /**
093   * Updates the last value and appends a record to the log if it has changed.
094   *
095   * <p>Note: the last value is local to this class instance; using update() with two instances
096   * pointing to the same underlying log entry name will likely result in unexpected results.
097   *
098   * @param value Value to record
099   */
100  public void update(double value) {
101    update(value, 0);
102  }
103
104  /**
105   * Gets whether there is a last value.
106   *
107   * <p>Note: the last value is local to this class instance and updated only with update(), not
108   * append().
109   *
110   * @return True if last value exists, false otherwise.
111   */
112  public synchronized boolean hasLastValue() {
113    return m_hasLastValue;
114  }
115
116  /**
117   * Gets the last value.
118   *
119   * <p>Note: the last value is local to this class instance and updated only with update(), not
120   * append().
121   *
122   * @return Last value, or 0 if none.
123   */
124  public synchronized double getLastValue() {
125    return m_lastValue;
126  }
127
128  boolean m_hasLastValue;
129  double m_lastValue;
130}