001/*
002 * Units of Measurement API
003 * Copyright (c) 2014-2023, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
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-385 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 javax.measure.test.quantity;
031
032import javax.measure.Quantity;
033import javax.measure.Unit;
034import javax.measure.quantity.Time;
035import javax.measure.test.unit.DistanceUnit;
036import javax.measure.test.unit.TimeUnit;
037
038/**
039 * @author Werner Keil
040 * @version 0.6
041 */
042public class TimeQuantity extends TestQuantity<Time> {
043
044    public TimeQuantity(double val, TimeUnit un) {
045        this();
046        value = val;
047        unit = un;
048        scalar = val * unit.getMultFactor();
049    }
050
051    public TimeQuantity(Number val, @SuppressWarnings("rawtypes") Unit un) {
052        this(val.doubleValue(), (TimeUnit) un);
053    }
054
055    public TimeQuantity() {
056        super(Time.class);
057    }
058
059    /*
060     * Time(double val) {
061     *
062     * value = val; unit = m; // reference Unit scalar = val;
063     *
064     * }
065     */
066    public TimeQuantity add(TimeQuantity d1) {
067        TimeQuantity dn = new TimeQuantity();
068        Object o = super.add(dn, this, d1, TimeUnit.REF_UNIT);
069        return (TimeQuantity) o;
070    }
071
072    public TimeQuantity subtract(TimeQuantity d1) {
073        TimeQuantity dn = new TimeQuantity();
074        Object o = super.subtract(dn, this, d1, TimeUnit.REF_UNIT);
075        return (TimeQuantity) o;
076    }
077
078    public boolean eq(TimeQuantity d1) {
079        return super.eq(d1);
080    }
081
082    public boolean ne(TimeQuantity d1) {
083        return super.ne(d1);
084    }
085
086    public boolean gt(TimeQuantity d1) {
087        return super.gt(d1);
088    }
089
090    public boolean lt(TimeQuantity d1) {
091        return super.lt(d1);
092    }
093
094    public boolean ge(TimeQuantity d1) {
095        return super.ge(d1);
096    }
097
098    public boolean le(TimeQuantity d1) {
099        return super.le(d1);
100    }
101
102    public TimeQuantity multiply(double v) {
103        return new TimeQuantity(value * v, (TimeUnit) unit);
104    }
105
106    public TimeQuantity divide(double v) {
107        return new TimeQuantity(value / v, (TimeUnit) unit);
108    }
109
110    public TimeQuantity convert(TimeUnit newUnit) {
111        return new TimeQuantity(scalar / newUnit.getMultFactor(), newUnit);
112    }
113
114    public String showInUnits(DistanceUnit u, int precision) {
115        return super.showInUnits(u, precision);
116    }
117
118    public Quantity<?> divide(Quantity<?> that) {
119        // TODO Auto-generated method stub
120        return null;
121    }
122
123    public Quantity<Time> subtract(Quantity<Time> that) {
124        // TODO Auto-generated method stub
125        return null;
126    }
127
128    public Quantity<Time> add(Quantity<Time> that) {
129        return add((TimeQuantity) that);
130    }
131
132    public Quantity<Time> divide(Number that) {
133        return divide(that.doubleValue());
134    }
135
136    public Quantity<Time> inverse() {
137        // TODO Auto-generated method stub
138        return null;
139    }
140
141    public Quantity<Time> multiply(Number that) {
142        return multiply(that.doubleValue());
143    }
144
145    public Quantity<Time> to(Unit<Time> unit) {
146        // TODO Auto-generated method stub
147        return null;
148    }
149
150    public Quantity<?> multiply(Quantity<?> that) {
151        // TODO Auto-generated method stub
152        return null;
153    }
154
155    @Override
156    public Quantity<Time> negate() {
157        return new TimeQuantity(-value, getUnit());
158    }
159
160    @SuppressWarnings({ "unchecked", "rawtypes" })
161    public final <T extends Quantity<T>> Quantity<T> asType(Class<T> type) throws ClassCastException {
162        this.getUnit().asType(type); // Raises ClassCastException is dimension
163        // mismatches.
164        return (Quantity) this;
165    }
166}