001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2018, 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, Indriya nor the names of their 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 tech.units.indriya.function; 031 032import java.math.BigDecimal; 033import java.math.MathContext; 034import java.util.Objects; 035 036import javax.measure.UnitConverter; 037 038import tech.units.indriya.AbstractConverter; 039import tech.uom.lib.common.function.DoubleFactorSupplier; 040import tech.uom.lib.common.function.ValueSupplier; 041 042/** 043 * <p> 044 * This class represents a converter multiplying numeric values by a constant 045 * scaling factor (<code>double</code> based). 046 * </p> 047 * 048 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 049 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 050 * @version 1.1, Apr 18, 2018 051 * @since 1.0 052 */ 053public final class MultiplyConverter extends AbstractConverter implements ValueSupplier<Double>, DoubleFactorSupplier { 054 055 /** 056 * 057 */ 058 private static final long serialVersionUID = 6588759878444545649L; 059 060 /** 061 * Holds the scale factor. 062 */ 063 private final double factor; 064 065 /** 066 * Creates a multiply converter with the specified scale factor. 067 * 068 * @param factor 069 * the scaling factor. 070 */ 071 public MultiplyConverter(double factor) { 072 this.factor = factor; 073 } 074 075 /** 076 * Creates a multiply converter with the specified scale factor. 077 * 078 * @param factor 079 * the scaling factor. 080 */ 081 public static MultiplyConverter of(double factor) { 082 return new MultiplyConverter(factor); 083 } 084 085 /** 086 * Returns the scale factor of this converter. 087 * 088 * @return the scale factor. 089 */ 090 public double getFactor() { 091 return factor; 092 } 093 094 @Override 095 public boolean isIdentity() { 096 return factor == 1.0; 097 } 098 099 @Override 100 protected boolean isSimpleCompositionWith(AbstractConverter that) { 101 return that instanceof MultiplyConverter; 102 } 103 104 @Override 105 protected AbstractConverter simpleCompose(AbstractConverter that) { 106 return new MultiplyConverter(factor * ((MultiplyConverter) that).factor); 107 } 108 109 @Override 110 public MultiplyConverter inverseWhenNotIdentity() { 111 return new MultiplyConverter(1.0 / factor); 112 } 113 114 @Override 115 public double convertWhenNotIdentity(double value) { 116 return value * factor; 117 } 118 119 @Override 120 public BigDecimal convertWhenNotIdentity(BigDecimal value, MathContext ctx) throws ArithmeticException { 121 return value.multiply(BigDecimal.valueOf(factor), ctx); 122 } 123 124 @Override 125 public final String transformationLiteral() { 126 return String.format("x -> x * %s", factor); 127 } 128 129 @Override 130 public boolean equals(Object obj) { 131 if (this == obj) { 132 return true; 133 } 134 if (obj instanceof MultiplyConverter) { 135 MultiplyConverter that = (MultiplyConverter) obj; 136 return Objects.equals(factor, that.factor); 137 } 138 return false; 139 } 140 141 @Override 142 public int hashCode() { 143 return Objects.hashCode(factor); 144 } 145 146 @Override 147 public boolean isLinear() { 148 return true; 149 } 150 151 @Override 152 public Double getValue() { 153 return factor; 154 } 155 156 @Override 157 public int compareTo(UnitConverter o) { 158 if (this == o) { 159 return 0; 160 } 161 if (o instanceof MultiplyConverter) { 162 return getValue().compareTo(((MultiplyConverter) o).getValue()); 163 } 164 return -1; 165 } 166}