001/* 002 * Units of Measurement Implementation for Java SE 003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tec.uom.se.quantity.time; 031 032import java.time.Instant; 033import java.util.Objects; 034import java.util.function.Supplier; 035 036import tec.uom.lib.common.function.Nameable; 037 038/** 039 * TimedData is a container for a data value that keeps track of its age. This class keeps track of the birth time of a bit of data, i.e. time the 040 * object is instantiated.<br/> 041 * The TimedData MUST be immutable. 042 * 043 * @param <T> 044 * The data value. 045 * 046 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 047 * @version 0.5 048 * @see <a href="http://en.wikipedia.org/wiki/Time_series"> Wikipedia: Time Series</a> 049 */ 050public class TimedData<T> implements Nameable, Supplier<T> { 051 private final T value; 052 private final long timestamp; 053 private final Instant instant; 054 private String name; 055 056 /** 057 * Construct an instance of TimedData with a value and timestamp. 058 * 059 * @param data 060 * The value of the TimedData. 061 * @param time 062 * The timestamp of the TimedData. 063 */ 064 protected TimedData(T value, long time) { 065 this.value = value; 066 this.timestamp = time; 067 this.instant = Instant.ofEpochMilli(time); 068 } 069 070 /** 071 * Returns an {@code MeasurementRange} with the specified values. 072 * 073 * @param <T> 074 * the class of the value 075 * @param val 076 * The minimum value for the measurement range. 077 * @param time 078 * The maximum value for the measurement range. 079 * @return an {@code MeasurementRange} with the given values 080 */ 081 public static <T> TimedData<T> of(T val, long time) { 082 return new TimedData<>(val, time); 083 } 084 085 /** 086 * Returns the time with which this TimedData was created. 087 * 088 * @return the time of creation 089 */ 090 public long getTimestamp() { 091 return timestamp; 092 } 093 094 public String getName() { 095 return name; 096 } 097 098 // @Override 099 public T get() { 100 return value; 101 } 102 103 /* 104 * (non-Javadoc) 105 * 106 * @see java.lang.Object#equals() 107 */ 108 @Override 109 public boolean equals(Object obj) { 110 if (this == obj) { 111 return true; 112 } 113 if (obj instanceof TimedData<?>) { 114 @SuppressWarnings("unchecked") 115 final TimedData<T> other = (TimedData<T>) obj; 116 return Objects.equals(get(), other.get()) && Objects.equals(getTimestamp(), other.getTimestamp()) && Objects.equals(getName(), other.getName()); 117 118 } 119 return false; 120 } 121 122 /* 123 * (non-Javadoc) 124 * 125 * @see java.lang.Object#hashCode() 126 */ 127 @Override 128 public int hashCode() { 129 return Objects.hash(value, name); 130 } 131 132 /* 133 * (non-Javadoc) 134 * 135 * @see java.lang.Object#toString() 136 */ 137 @Override 138 public String toString() { 139 final StringBuilder sb = new StringBuilder().append("data= ").append(get()).append(", timestamp= ").append(getTimestamp()); 140 if (name != null && name.length() > 0) { 141 sb.append(", name= ").append(getName()); 142 } 143 return sb.toString(); 144 } 145 146 public Instant getInstant() { 147 return instant; 148 } 149 150}