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
007import edu.wpi.first.util.struct.Struct;
008import edu.wpi.first.util.struct.StructBuffer;
009import java.nio.ByteBuffer;
010import java.util.Collection;
011
012/**
013 * Log struct-encoded array values.
014 *
015 * @param <T> value class
016 */
017public final class StructArrayLogEntry<T> extends DataLogEntry {
018  private StructArrayLogEntry(
019      DataLog log, String name, Struct<T> struct, String metadata, long timestamp) {
020    super(log, name, struct.getTypeString() + "[]", metadata, timestamp);
021    m_buf = StructBuffer.create(struct);
022    log.addSchema(struct, timestamp);
023  }
024
025  /**
026   * Creates a struct-encoded array log entry.
027   *
028   * @param <T> value class (inferred from struct)
029   * @param log datalog
030   * @param name name of the entry
031   * @param struct struct serialization implementation
032   * @param metadata metadata
033   * @param timestamp entry creation timestamp (0=now)
034   * @return StructArrayLogEntry
035   */
036  public static <T> StructArrayLogEntry<T> create(
037      DataLog log, String name, Struct<T> struct, String metadata, long timestamp) {
038    return new StructArrayLogEntry<>(log, name, struct, metadata, timestamp);
039  }
040
041  /**
042   * Creates a struct-encoded array log entry.
043   *
044   * @param <T> value class (inferred from struct)
045   * @param log datalog
046   * @param name name of the entry
047   * @param struct struct serialization implementation
048   * @param metadata metadata
049   * @return StructArrayLogEntry
050   */
051  public static <T> StructArrayLogEntry<T> create(
052      DataLog log, String name, Struct<T> struct, String metadata) {
053    return create(log, name, struct, metadata, 0);
054  }
055
056  /**
057   * Creates a struct-encoded array log entry.
058   *
059   * @param <T> value class (inferred from struct)
060   * @param log datalog
061   * @param name name of the entry
062   * @param struct struct serialization implementation
063   * @param timestamp entry creation timestamp (0=now)
064   * @return StructArrayLogEntry
065   */
066  public static <T> StructArrayLogEntry<T> create(
067      DataLog log, String name, Struct<T> struct, long timestamp) {
068    return create(log, name, struct, "", timestamp);
069  }
070
071  /**
072   * Creates a struct-encoded array log entry.
073   *
074   * @param <T> value class (inferred from struct)
075   * @param log datalog
076   * @param name name of the entry
077   * @param struct struct serialization implementation
078   * @return StructArrayLogEntry
079   */
080  public static <T> StructArrayLogEntry<T> create(DataLog log, String name, Struct<T> struct) {
081    return create(log, name, struct, 0);
082  }
083
084  /**
085   * Ensures sufficient buffer space is available for the given number of elements.
086   *
087   * @param nelem number of elements
088   */
089  public void reserve(int nelem) {
090    synchronized (m_buf) {
091      m_buf.reserve(nelem);
092    }
093  }
094
095  /**
096   * Appends a record to the log.
097   *
098   * @param value Value to record
099   * @param timestamp Time stamp (0 to indicate now)
100   */
101  public void append(T[] value, long timestamp) {
102    synchronized (this) {
103      ByteBuffer bb = m_buf.writeArray(value);
104      m_log.appendRaw(m_entry, bb, 0, bb.position(), timestamp);
105    }
106  }
107
108  /**
109   * Appends a record to the log.
110   *
111   * @param value Value to record
112   */
113  public void append(T[] value) {
114    append(value, 0);
115  }
116
117  /**
118   * Appends a record to the log.
119   *
120   * @param value Value to record
121   * @param timestamp Time stamp (0 to indicate now)
122   */
123  public void append(Collection<T> value, long timestamp) {
124    synchronized (m_buf) {
125      ByteBuffer bb = m_buf.writeArray(value);
126      m_log.appendRaw(m_entry, bb, 0, bb.position(), timestamp);
127    }
128  }
129
130  /**
131   * Appends a record to the log.
132   *
133   * @param value Value to record
134   */
135  public void append(Collection<T> value) {
136    append(value, 0);
137  }
138
139  private final StructBuffer<T> m_buf;
140}