001package jmri.jmrix.rps;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004
005
006/**
007 * Encodes a single set of input values (a "reading") for RPS.
008 * <p>
009 * The values are in time units (nominally usec), and need to be converted to
010 * space units during later calculations.
011 * <p>
012 * The values are indexed by Receiver number, as known to the RPS system. For
013 * example, getValue(2) will return the time from RPS receiver 2.
014 * <p>
015 * Objects of this class are immutable once created.
016 *
017 * @author Bob Jacobsen Copyright (C) 2006, 2008
018 */
019@javax.annotation.concurrent.Immutable
020public class Reading {
021
022    @SuppressFBWarnings(value = "EI_EXPOSE_REP2") // We accept the external access by design
023    public Reading(String id, double[] values) {
024        this.id = id;
025        this.values = values;
026        this.rawData = null;
027        this.time = 0;
028    }
029
030    @SuppressFBWarnings(value = "EI_EXPOSE_REP2") // We accept the external access by design
031    public Reading(String id, double[] values, String raw) {
032        this.id = id;
033        this.values = values;
034        this.rawData = raw;
035        this.time = 0;
036    }
037
038    @SuppressFBWarnings(value = "EI_EXPOSE_REP2") // We accept the external access by design
039    public Reading(String id, double[] values, int time) {
040        this.id = id;
041        this.values = values;
042        this.rawData = null;
043        this.time = time;
044    }
045
046    public Reading(Reading r) {
047        this.id = r.getId();
048        this.values = r.getValues();
049        this.rawData = null;
050        this.time = r.getTime();
051    }
052
053    /**
054     * Return the time at which this Reading was requested.
055     * @return read request time.
056     */
057    public int getTime() {
058        return time;
059    }
060
061    /**
062     * Return the ID int of the transmitter this reading describes.
063     * @return ID of the transmitter.
064     */
065    public String getId() {
066        return id;
067    }
068
069    /**
070     * NValues is really the highest receiver number possible.
071     * @return highest receiver number possible.
072     */
073    public int getNValues() {
074        return values.length - 1;
075    }
076
077    /**
078     * Convenience method to get a specific one of the values.
079     * @param i value index.
080     * @return value.
081     */
082    public double getValue(int i) {
083        return values[i];
084    }
085
086    /*
087     * Get the entire data array as an copy,
088     * to preserve immutability.
089     */
090    public double[] getValues() {
091        double[] retval = new double[values.length];
092        for (int i = 0; i < values.length; i++) {
093            retval[i] = values[i];
094        }
095        return retval;
096    }
097
098    final String id;
099    final double[] values;
100    final int time; // in msec since epoch
101
102    @Override
103    public String toString() {
104        StringBuilder b = new StringBuilder();
105        b.append("Reading id=").append(getId()).append(" values=");
106        for (int i = 1; i <= getNValues(); i++) {
107            b.append(getValue(i)).append(i != getNValues() ? "," : " ");
108        }
109        return b.toString();
110    }
111
112    /**
113     * Get the raw data from which this Reading was made.
114     *
115     * @return null if raw data was not preserved
116     */
117    public Object getRawData() {
118        return rawData;
119    }
120
121    final Object rawData;
122
123}