dataset.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 188 行

JAVA
188
字号
package net.sf.dz.controller;import java.util.Iterator;import java.util.NoSuchElementException;import java.util.SortedMap;import java.util.TreeMap;/** * Entity supporting the data sampling. * * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org"> Vadim Tkachenko</a> 2001-2002 * @version $Id: DataSet.java,v 1.5 2004/06/28 20:35:46 vtt Exp $ */public class DataSet {    /**     * The data set.     *     * The key is sampling time, the value is sample value.     */    private SortedMap dataSet = new TreeMap();    /**     * The expiration interval.     *     * Values older than the last key by this many milliseconds are expired.     */    private long expirationInterval;    /**     * Strictness.     *     * If this is set to true, the {@link #record record()} will not accept     * values for the time less than already recorded, and {@link #record     * record()} will throw <code>IllegalArgumentException</code>.     *     * <p>     *     * This is not necessarily a good thing.     */    private boolean strict = false;    /**     * Create the instance.     *     * @param expirationInterval How many milliseconds to keep the data.     *     * @exception IllegalArgumentException if the expiration interval is     * non-positive (<= 0). Be careful with the short intervals, it's going     * to be your fault, not mine.     */    public DataSet(final long expirationInterval) {        if (expirationInterval <= 0) {            throw new IllegalArgumentException("Expiration interval must be positive, value given is " + expirationInterval);        }        this.expirationInterval = expirationInterval;    }    /**     * Set strictness.     *     * @param strict If set to true, out-of-order updates will not be     * accepted.     */    public final void setStrict(final boolean strict) {        this.strict = strict;    }    /**     * Get the expiration interval.     *     * @return Expiration interval, milliseconds.     */    public final long getExpirationInterval() {        return expirationInterval;    }    /**     * Record the sample.     *     * @param millis Absolute time, milliseconds.     *     * @param value The sample value.     */    public final synchronized void record(final long millis, final double value) {        // We don't care if there was a value associated with the given key        // before, so we return nothing.        if (strict) {            try {                Long lastKey = (Long) dataSet.lastKey();                if (lastKey.longValue() >= millis) {                    throw new IllegalArgumentException("Data element out of sequence: last key is " + lastKey + ", key being added is " + millis);                }            } catch (NoSuchElementException nseex) {                // We're fine. This means that there was no previous entry,                // therefore, the current one will not be out of order.            }        }        dataSet.put(new Long(millis), new Double(value));        expire();        //System.err.println("DataSet@" + hashCode() + ": " + dataSet.size());    }    /**     * Expire all the data elements older than the last by {@link     * #expirationInterval expiration interval}.     */    protected final void expire() {        try {            Long lastKey = (Long) dataSet.lastKey();            Long expireBefore = new Long(lastKey.longValue() - expirationInterval);            SortedMap expireMap = dataSet.headMap(expireBefore);            for (Iterator i = expireMap.keySet().iterator(); i.hasNext();) {                Object found = i.next();                i.remove();                //System.err.println("Expired: " + found + ", left: " + dataSet.size());            }        } catch (NoSuchElementException nseex) {           // We're fine, the map is empty        }    }    /**     * @return Iterator on the time values for the data entries.     */    public final Iterator iterator() {        return dataSet.keySet().iterator();    }    /**     * Get the data set size.     *     * @return {@link #dataSet dataSet} size.     */    public final long size() {        return dataSet.size();    }    /**     * Get the value recorded at the given time.     *     * @param time Time to look up the data for. Must be exact, otherwise,     * exception will be thrown.     *     * @return Value recorded at the given time.     *     * @exception NoSuchElementException if the value for the given time is     * not in the set.     */    public final double get(final long time) {        Double result = (Double) dataSet.get(new Long(time));        if (result == null) {            throw new NoSuchElementException("No value for time " + time);        }        return result.doubleValue();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?