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