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.function; 031 032import javax.measure.UnitConverter; 033 034import tec.uom.lib.common.function.DoubleFactorSupplier; 035import tec.uom.lib.common.function.ValueSupplier; 036import tec.uom.se.AbstractConverter; 037 038import java.math.BigDecimal; 039import java.math.MathContext; 040import java.util.Objects; 041 042/** 043 * <p> 044 * This class represents a converter multiplying numeric values by a constant scaling factor (<code>double</code> based). 045 * </p> 046 * 047 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 048 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 049 * @version 1.0, Oct 11, 2016 050 * @since 1.0 051 */ 052public final class MultiplyConverter extends AbstractConverter implements ValueSupplier<Double>, DoubleFactorSupplier { 053 054 /** 055 * 056 */ 057 private static final long serialVersionUID = 6588759878444545649L; 058 059 /** 060 * Holds the scale factor. 061 */ 062 private double factor; 063 064 /** 065 * Creates a multiply converter with the specified scale factor. 066 * 067 * @param factor 068 * the scaling factor. 069 * @throws IllegalArgumentException 070 * if coefficient is <code>1.0</code> (would result in identity converter) 071 */ 072 public MultiplyConverter(double factor) { 073 if (factor == 1.0) 074 throw new IllegalArgumentException("Would result in identity converter"); 075 this.factor = factor; 076 } 077 078 /** 079 * Returns the scale factor of this converter. 080 * 081 * @return the scale factor. 082 */ 083 public double getFactor() { 084 return factor; 085 } 086 087 @Override 088 public UnitConverter concatenate(UnitConverter converter) { 089 if (!(converter instanceof MultiplyConverter)) 090 return super.concatenate(converter); 091 double newfactor = factor * ((MultiplyConverter) converter).factor; 092 return newfactor == 1.0 ? IDENTITY : new MultiplyConverter(newfactor); 093 } 094 095 @Override 096 public MultiplyConverter inverse() { 097 return new MultiplyConverter(1.0 / factor); 098 } 099 100 @Override 101 public double convert(double value) { 102 return value * factor; 103 } 104 105 @Override 106 public BigDecimal convert(BigDecimal value, MathContext ctx) throws ArithmeticException { 107 return value.multiply(BigDecimal.valueOf(factor), ctx); 108 } 109 110 @Override 111 public final String toString() { 112 return MultiplyConverter.class.getSimpleName() + "(" + factor + ")"; 113 } 114 115 @Override 116 public boolean equals(Object obj) { 117 if (this == obj) { 118 return true; 119 } 120 if (obj instanceof MultiplyConverter) { 121 MultiplyConverter that = (MultiplyConverter) obj; 122 return Objects.equals(factor, that.factor); 123 } 124 return false; 125 } 126 127 @Override 128 public int hashCode() { 129 return Objects.hashCode(factor); 130 } 131 132 @Override 133 public boolean isLinear() { 134 return true; 135 } 136 137 @Override 138 public Double getValue() { 139 return factor; 140 } 141}