001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2020, Units of Measurement project. 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.unit; 031 032import javax.measure.Dimension; 033import javax.measure.Quantity; 034import javax.measure.Unit; 035import javax.measure.UnitConverter; 036 037import tech.units.indriya.AbstractUnit; 038import tech.units.indriya.function.AbstractConverter; 039import tech.units.indriya.unit.UnitDimension; 040 041import java.util.Map; 042import java.util.Objects; 043 044/** 045 * <p> 046 * This class represents the building blocks on top of which all others physical units are created. Base units are always unscaled SI units. 047 * </p> 048 * 049 * <p> 050 * When using the {@link tech.units.indriya.spi.StandardModel standard model}, all seven <b>SI</b> base units are dimensionally independent. 051 * </p> 052 * 053 * @see <a href="http://en.wikipedia.org/wiki/SI_base_unit"> Wikipedia: SI base unit</a> 054 * 055 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 056 * @author <a href="mailto:werner@units.tech">Werner Keil</a> 057 * @version 1.6, July 5, 2019 058 * @since 1.0 059 */ 060public final class BaseUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> { 061 062 /** 063 * 064 */ 065 private static final long serialVersionUID = 1721629233768215930L; 066 067 /** 068 * Holds the base unit dimension. 069 */ 070 private final Dimension dimension; 071 072 /** 073 * Creates a base unit having the specified symbol and dimension. 074 * 075 * @param symbol 076 * the symbol of this base unit. 077 */ 078 public BaseUnit(String symbol, Dimension dimension) { 079 super(symbol); 080 this.dimension = dimension; 081 } 082 083 /** 084 * Creates a base unit having the specified symbol. 085 * 086 * @param symbol 087 * the symbol of this base unit. 088 */ 089 public BaseUnit(String symbol) { 090 super(symbol); 091 this.dimension = UnitDimension.NONE; 092 } 093 094 /** 095 * Creates a base unit having the specified symbol and name. 096 * 097 * @param symbol 098 * the symbol of this base unit. 099 * @param name 100 * the name of this base unit. 101 * @throws IllegalArgumentException 102 * if the specified symbol is associated to a different unit. 103 */ 104 public BaseUnit(String symbol, String name) { 105 this(symbol); 106 this.name = name; 107 } 108 109 @Override 110 public Unit<Q> toSystemUnit() { 111 return this; 112 } 113 114 @Override 115 public UnitConverter getSystemConverter() throws UnsupportedOperationException { 116 return AbstractConverter.IDENTITY; 117 } 118 119 @Override 120 public Dimension getDimension() { 121 return dimension; 122 } 123 124@Override 125public int hashCode() { 126 final int prime = 31; 127 int result = 1; 128 result = prime * result + ((getSymbol() == null) ? 0 : getSymbol().hashCode()); 129 //result = result + prime * result + ((name == null) ? 0 : name.hashCode()); 130 result = result + prime * result + ((dimension == null) ? 0 : dimension.hashCode()); 131 return result; 132} 133 134@Override 135public boolean equals(Object obj) { 136 if (this == obj) 137 return true; 138 if (obj == null) 139 return false; 140 if (getClass() != obj.getClass()) 141 return false; 142 @SuppressWarnings("rawtypes") 143 BaseUnit other = (BaseUnit) obj; 144 return Objects.equals(dimension, other.dimension) && Objects.equals(getSymbol(), other.getSymbol()); 145} 146 147 @Override 148 public Map<? extends AbstractUnit<Q>, Integer> getBaseUnits() { 149 // TODO Shall we return null, empty list or what (e.g. Optional from Java 8)? 150 return null; 151 } 152}