001/*
002 * ============================================================================
003 * Copyright © 2002-2023 by Thomas Thrien.
004 * All Rights Reserved.
005 * ============================================================================
006 * Licensed to the public under the agreements of the GNU Lesser General Public
007 * License, version 3.0 (the "License"). You may obtain a copy of the License at
008 *
009 *      http://www.gnu.org/licenses/lgpl.html
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017
018package org.tquadrat.foundation.value;
019
020import static java.lang.String.format;
021import static java.util.Arrays.stream;
022import static org.apiguardian.api.API.Status.STABLE;
023import static org.tquadrat.foundation.lang.Objects.requireNotEmptyArgument;
024
025import java.math.BigDecimal;
026
027import org.apiguardian.api.API;
028import org.tquadrat.foundation.annotation.ClassVersion;
029import org.tquadrat.foundation.value.api.DimensionWithLinearConversion;
030
031/**
032 *  The various instances of pressure.
033 *
034 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
035 *  @version $Id: Pressure.java 1072 2023-09-30 20:44:38Z tquadrat $
036 *  @since 0.1.0
037 *
038 *  @UMLGraph.link
039 */
040@ClassVersion( sourceVersion = "$Id: Pressure.java 1072 2023-09-30 20:44:38Z tquadrat $" )
041@API( status = STABLE, since = "0.1.0" )
042public enum Pressure implements DimensionWithLinearConversion
043{
044        /*------------------*\
045    ====** Enum Declaration **=================================================
046        \*------------------*/
047    /**
048     *  A milli Pascal.
049     */
050    MILLI_PASCAL( new BigDecimal( "0.001" ), "mPa" ),
051
052    /**
053     *  One Pascal.
054     */
055    PASCAL( BigDecimal.ONE, "Pa" ),
056
057    /**
058     *  Same as
059     *  {@link #PASCAL},
060     *  but a different unit.
061     */
062    NEWTON_PER_SQUAREMETER( PASCAL.factor(), "N/m^2" )
063    {
064        /**
065         *  {@inheritDoc}
066         */
067        @Override
068        public final String unitSymbolForPrinting() { return "N/m²"; }
069    },
070
071    /**
072     *  A hekto Pascal.
073     */
074    HEKTO_PASCAL( new BigDecimal( "100" ), "hPa" ),
075
076    /**
077     *  A milli bar; same as
078     *  {@link #HEKTO_PASCAL}.
079     */
080    MILLI_BAR( HEKTO_PASCAL.factor(), "mbar" ),
081
082    /**
083     *  A Torr.
084     */
085    TORR( new BigDecimal( "133.322368421" ), "Torr" ),
086
087    /**
088     *  A mm of Mercury; same as
089     *  {@link #TORR}.
090     */
091    MILLIMETER_OF_MERCURY( TORR.factor(), "mmHg" ),
092
093    /**
094     *  An inch of Mercury.
095     */
096    INCH_OF_MERCURY( new BigDecimal( "3386.3881578" ), "inHg" ),
097
098    /**
099     *  A kilo Pascal.
100     */
101    KILO_PASCAL( new BigDecimal( "1000" ), "kPa" ),
102
103    /**
104     *  A psi.
105     */
106    PSI( new BigDecimal( "6894.757293178" ), "psi" ),
107
108    /**
109     *  A technical <i>atmosphere</i> (at).
110     */
111    AT( new BigDecimal( "98066.5" ), "at" ),
112
113    /**
114     *  A <i>bar</i>.
115     */
116    BAR( new BigDecimal( "100.000" ), "bar" ),
117
118    /**
119     *  A physical <i>atmosphere</i> (atm).
120     */
121    ATM( new BigDecimal( "101325.2738" ), "atm" ),
122
123    /**
124     *  A mega Pascal.
125     */
126    MEGA_PASCAL( new BigDecimal( "1000000" ), "MPa" ),
127
128    /**
129     *  Same as
130     *  {@link #MEGA_PASCAL},
131     *  but a different unit.
132     */
133    NEWTON_PER_SQUAREMILLIMETER( MEGA_PASCAL.factor(), "N/mm^2" )
134    {
135        /**
136         *  {@inheritDoc}
137         */
138        @Override
139        public final String unitSymbolForPrinting() { return "N/mm²"; }
140    };
141
142        /*------------*\
143    ====** Attributes **=======================================================
144        \*------------*/
145    /**
146     *  The factor.
147     */
148    private final BigDecimal m_Factor;
149
150    /**
151     *  The unit string.
152     */
153    private final String m_UnitSymbol;
154
155        /*--------------*\
156    ====** Constructors **=====================================================
157        \*--------------*/
158    /**
159     *  Creates a new {@code Pressure} instance.
160     *
161     *  @param  factor  The factor.
162     *  @param  unitSymbol    The unit string.
163     */
164    private Pressure( final BigDecimal factor, final String unitSymbol )
165    {
166        m_Factor = factor.stripTrailingZeros();
167        m_UnitSymbol = unitSymbol;
168    }   //  Pressure()
169
170        /*---------*\
171    ====** Methods **==========================================================
172        \*---------*/
173    /**
174     *  {@inheritDoc}
175     */
176    @Override
177    @SuppressWarnings( "unchecked" )
178    public final Pressure baseUnit() { return PASCAL; }
179
180    /**
181     *  {@inheritDoc}
182     */
183    @Override
184    public final BigDecimal factor() { return m_Factor; }
185
186    /**
187     *  Returns the {@code Pressure} instance for the given unit.
188     *
189     *  @param  unitSymbol    The unit symbol.
190     *  @return The respective instance.
191     *  @throws IllegalArgumentException    The given unit is unknown.
192     */
193    public static final Pressure forUnit( final String unitSymbol ) throws IllegalArgumentException
194    {
195        requireNotEmptyArgument( unitSymbol, "unitSymbol" );
196
197        final var retValue = stream( values() )
198            .filter( v -> v.unitSymbol().equals( unitSymbol ) )
199            .findFirst()
200            .orElseThrow( () -> new IllegalArgumentException( format( MSG_UnknownUnit, unitSymbol ) ) );
201
202        //---* Done *----------------------------------------------------------
203        return retValue;
204    }   //  forUnit()
205
206    /**
207     *  {@inheritDoc}
208     */
209    @Override
210    public final String unitSymbol() { return m_UnitSymbol; }
211}
212//  enum Pressure
213
214/*
215 *  End of File
216 */