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.Area;
035import javax.measure.quantity.Length;
036import javax.measure.test.unit.AreaUnit;
037import javax.measure.test.unit.DistanceUnit;
038import javax.measure.test.unit.VolumeUnit;
039
040/**
041 * @author Werner Keil
042 * @version 0.7
043 */
044public class DistanceQuantity extends TestQuantity<Length> implements Length {
045
046    public DistanceQuantity(double val, DistanceUnit un) {
047        this();
048        value = val;
049        unit = un;
050        scalar = val * unit.getMultFactor();
051    }
052
053    public DistanceQuantity(Number val, Unit un) {
054        this(val.doubleValue(), (DistanceUnit) un);
055    }
056
057    public DistanceQuantity() {
058        super(Length.class);
059    }
060
061    /*
062     * Distance(double val) {
063     *
064     * value = val; unit = m; // reference Unit scalar = val;
065     *
066     * }
067     */
068    public DistanceQuantity add(DistanceQuantity d1) {
069        DistanceQuantity dn = new DistanceQuantity();
070        Object o = super.add(dn, this, d1, DistanceUnit.REF_UNIT);
071        return (DistanceQuantity) o;
072    }
073
074    public DistanceQuantity subtract(DistanceQuantity d1) {
075        DistanceQuantity dn = new DistanceQuantity();
076        Object o = super.subtract(dn, this, d1, DistanceUnit.REF_UNIT);
077        return (DistanceQuantity) o;
078    }
079
080    public boolean eq(DistanceQuantity d1) {
081        return super.eq(d1);
082    }
083
084    public boolean ne(DistanceQuantity d1) {
085        return super.ne(d1);
086    }
087
088    public boolean gt(DistanceQuantity d1) {
089        return super.gt(d1);
090    }
091
092    public boolean lt(DistanceQuantity d1) {
093        return super.lt(d1);
094    }
095
096    public boolean ge(DistanceQuantity d1) {
097        return super.ge(d1);
098    }
099
100    public boolean le(DistanceQuantity d1) {
101        return super.le(d1);
102    }
103
104    public DistanceQuantity multiply(double v) {
105        return new DistanceQuantity(value * v, (DistanceUnit) unit);
106    }
107
108    public DistanceQuantity divide(double v) {
109        return new DistanceQuantity(value / v, (DistanceUnit) unit);
110    }
111
112    // mixed type operations
113    public AreaQuantity multiply(DistanceQuantity d1) {
114        DistanceQuantity dq0 = convert(DistanceUnit.m);
115        DistanceQuantity dq1 = d1.convert(DistanceUnit.m);
116        return new AreaQuantity(dq0.value * dq1.value, AreaUnit.sqmetre);
117    }
118
119    public VolumeQuantity multiply(AreaQuantity a1) {
120        DistanceQuantity dq = convert(DistanceUnit.m);
121        AreaQuantity aq = a1.convert(AreaUnit.sqmetre);
122        return new VolumeQuantity(dq.value * aq.value, VolumeUnit.cumetre);
123    }
124
125    // public Speed divide(TimeInterval t1) {
126    // return new Speed(scalar /
127    // t1.scalar, Speed.refUnit);
128    // }
129    // public TimeInterval divide(Speed s1) {
130    // return new TimeInterval(scalar /
131    // s1.scalar, TimeInterval.refUnit);
132    // }
133
134    public DistanceQuantity convert(DistanceUnit newUnit) {
135        return new DistanceQuantity(scalar / newUnit.getMultFactor(), newUnit);
136    }
137
138    public String showInUnits(DistanceUnit u, int precision) {
139        return super.showInUnits(u, precision);
140    }
141
142    public Quantity<?> divide(Quantity<?> that) {
143        // TODO Auto-generated method stub
144        return null;
145    }
146
147    public Quantity<Length> to(Unit<Length> unit) {
148        // TODO Auto-generated method stub
149        return null;
150    }
151
152    public Quantity<Length> subtract(Quantity<Length> that) {
153        // TODO Auto-generated method stub
154        return null;
155    }
156
157    public Quantity<Length> add(Quantity<Length> that) {
158        return add((DistanceQuantity) that);
159    }
160
161    public Quantity<Length> divide(Number that) {
162        return divide(that.doubleValue());
163    }
164
165    public Quantity<Length> inverse() {
166        // TODO Auto-generated method stub
167        return null;
168    }
169
170    @Override
171    public Quantity<Length> negate() {
172        return new DistanceQuantity(-value, getUnit());
173    }
174
175    public Quantity<Length> multiply(Number that) {
176        // TODO Auto-generated method stub
177        return null;
178    }
179
180    public Quantity<?> multiply(Quantity<?> that) {
181        // TODO Auto-generated method stub
182        return null;
183    }
184
185    @SuppressWarnings({ "unchecked", "rawtypes" })
186    public final <T extends Quantity<T>> Quantity<T> asType(Class<T> type) throws ClassCastException {
187        this.getUnit().asType(type); // Raises ClassCastException is dimension
188        // mismatches.
189        return (Quantity) this;
190    }
191
192    public Area multiply(Length l) {
193        // TODO Auto-generated method stub
194        return null;
195    }
196}