View Javadoc
1   /*
2    * Copyright (C) 2018 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.units;
17  
18  import java.text.ParseException;
19  import java.util.Locale;
20  
21  /**
22   * Formats and parses angular acceleration value and unit.
23   */
24  public class AngularAccelerationFormatter extends MeasureFormatter<AngularAcceleration, AngularAccelerationUnit> {
25  
26      /**
27       * Degrees per squared second symbol.
28       */
29      public static final String DEGREES_PER_SQUARED_SECOND = "º/s²";
30  
31      /**
32       * Radians per squared second symbol.
33       */
34      public static final String RADIANS_PER_SQUARED_SECOND = "rad/s²";
35  
36      /**
37       * Constructor.
38       */
39      public AngularAccelerationFormatter() {
40          super();
41      }
42  
43      /**
44       * Constructor with locale.
45       *
46       * @param locale locale.
47       * @throws IllegalArgumentException if locale is null.
48       */
49      public AngularAccelerationFormatter(final Locale locale) {
50          super(locale);
51      }
52  
53      /**
54       * Copy constructor.
55       *
56       * @param formatter input instance to copy from.
57       * @throws NullPointerException if provided formatter is null.
58       */
59      public AngularAccelerationFormatter(final AngularAccelerationFormatter formatter) {
60          this(formatter.getLocale());
61      }
62  
63      /**
64       * Determines if two angular acceleration formatters are equal by comparing all of
65       * its internal parameters.
66       *
67       * @param obj another object to compare.
68       * @return true if provided object is assumed to be equal to this instance.
69       */
70      @Override
71      public boolean equals(final Object obj) {
72          final var equals = super.equals(obj);
73          return (obj instanceof AngularAccelerationFormatter) && equals;
74      }
75  
76      /**
77       * Hash code generated for this instance.
78       * Hash codes can be internally used by some collections to coarsely compare objects.
79       * This implementation only calls parent implementation to avoid static analyzer warning.
80       *
81       * @return hash code.
82       */
83      @Override
84      public int hashCode() {
85          return super.hashCode();
86      }
87  
88      /**
89       * Gets unit system for detected unit into provided string representation of a
90       * measurement.
91       *
92       * @param source a measurement string representation to be checked.
93       * @return a unit system (metric) or null if unit cannot be determined.
94       */
95      @Override
96      public UnitSystem getUnitSystem(final String source) {
97          final var unit = findUnit(source);
98          return unit != null ? UnitSystem.METRIC : null;
99      }
100 
101     /**
102      * Parses provided string and tries to determine an angular acceleration value
103      * and unit.
104      *
105      * @param source text to be parsed.
106      * @return an angular acceleration containing a value and unit.
107      * @throws ParseException       if provided string cannot be parsed.
108      * @throws UnknownUnitException if unit cannot be determined.
109      */
110     @Override
111     public AngularAcceleration parse(final String source) throws ParseException, UnknownUnitException {
112         return internalParse(source, new AngularAcceleration());
113     }
114 
115     /**
116      * Attempts to determine an angular acceleration unit within a measurement string
117      * representation.
118      *
119      * @param source an angular acceleration measurement string representation.
120      * @return an angular acceleration unit, or null if none can be determined.
121      */
122     @Override
123     public AngularAccelerationUnit findUnit(final String source) {
124         if (source.contains(DEGREES_PER_SQUARED_SECOND + " ") || source.endsWith(DEGREES_PER_SQUARED_SECOND)) {
125             return AngularAccelerationUnit.DEGREES_PER_SQUARED_SECOND;
126         }
127         if (source.contains(RADIANS_PER_SQUARED_SECOND + " ") || source.endsWith(RADIANS_PER_SQUARED_SECOND)) {
128             return AngularAccelerationUnit.RADIANS_PER_SQUARED_SECOND;
129         }
130         return null;
131     }
132 
133     /**
134      * Formats and converts provided angular acceleration value and unit using metric
135      * system.
136      * This implementation ignored provided unit system.
137      *
138      * @param value  a measurement value.
139      * @param unit   a measurement unit.
140      * @param system ignored.
141      * @return a string representation of angular acceleration value and unit.
142      */
143     @Override
144     public String formatAndConvert(final Number value, final AngularAccelerationUnit unit, final UnitSystem system) {
145         return format(value, unit);
146     }
147 
148     /**
149      * Returns unit string representation.
150      *
151      * @param unit an angular acceleration unit.
152      * @return its string representation.
153      */
154     public String getUnitSymbol(final AngularAccelerationUnit unit) {
155         if (unit == AngularAccelerationUnit.DEGREES_PER_SQUARED_SECOND) {
156             return DEGREES_PER_SQUARED_SECOND;
157         } else {
158             return RADIANS_PER_SQUARED_SECOND;
159         }
160     }
161 }