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 speed value and unit.
23 */
24 public class AngularSpeedFormatter extends MeasureFormatter<AngularSpeed, AngularSpeedUnit> {
25
26 /**
27 * Degrees per second symbol.
28 */
29 public static final String DEGREES_PER_SECOND = "º/s";
30
31 /**
32 * Radians per second symbol.
33 */
34 public static final String RADIANS_PER_SECOND = "rad/s";
35
36 /**
37 * Constructor.
38 */
39 public AngularSpeedFormatter() {
40 super();
41 }
42
43 /**
44 * Constructor with locale.
45 *
46 * @param locale locale.
47 * @throws IllegalArgumentException if locale is null.
48 */
49 public AngularSpeedFormatter(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 AngularSpeedFormatter(final AngularSpeedFormatter formatter) {
60 this(formatter.getLocale());
61 }
62
63 /**
64 * Determines if two angular speed formatters are equal by comparing all of its
65 * 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 AngularSpeedFormatter) && 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
90 * of a 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 speed value and unit.
103 *
104 * @param source a string to be parsed.
105 * @return an angular speed containing a value and unit.
106 * @throws ParseException if provided string cannot be parsed.
107 * @throws UnknownUnitException if unit cannot be determined.
108 */
109 @Override
110 public AngularSpeed parse(final String source) throws ParseException, UnknownUnitException {
111 return internalParse(source, new AngularSpeed());
112 }
113
114 /**
115 * Attempts to determine an angular speed unit within a measurement string
116 * representation.
117 *
118 * @param source an angular speed measurement string representation.
119 * @return an angular speed unit, or null if none can be determined.
120 */
121 @Override
122 public AngularSpeedUnit findUnit(final String source) {
123 if (source.contains(DEGREES_PER_SECOND + " ") || source.endsWith(DEGREES_PER_SECOND)) {
124 return AngularSpeedUnit.DEGREES_PER_SECOND;
125 }
126 if (source.contains(RADIANS_PER_SECOND + " ") || source.endsWith(RADIANS_PER_SECOND)) {
127 return AngularSpeedUnit.RADIANS_PER_SECOND;
128 }
129 return null;
130 }
131
132 /**
133 * Formats and converts provided angular speed value and unit using metric
134 * system.
135 * This implementation ignored provided unit system.
136 *
137 * @param value a measurement value.
138 * @param unit a measurement unit.
139 * @param system ignored.
140 * @return a string representation of angular speed value and unit.
141 */
142 @Override
143 public String formatAndConvert(final Number value, final AngularSpeedUnit unit, final UnitSystem system) {
144 return format(value, unit);
145 }
146
147 /**
148 * Returns unit string representation.
149 *
150 * @param unit an angular speed unit.
151 * @return its string representation.
152 */
153 @Override
154 public String getUnitSymbol(final AngularSpeedUnit unit) {
155 if (unit == AngularSpeedUnit.DEGREES_PER_SECOND) {
156 return DEGREES_PER_SECOND;
157 } else {
158 return RADIANS_PER_SECOND;
159 }
160 }
161 }