001 /*
002 * Java GPX Library (jpx-3.1.0).
003 * Copyright (c) 2016-2023 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
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,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.jpx;
021
022 import static java.lang.String.format;
023
024 import java.io.DataInput;
025 import java.io.DataOutput;
026 import java.io.IOException;
027 import java.io.InvalidObjectException;
028 import java.io.ObjectInputStream;
029 import java.io.Serial;
030 import java.io.Serializable;
031
032 /**
033 * Represents a differential GPS station. This object only holds int values in
034 * the range of {@code [0..1023]}.
035 *
036 * @see <a href="https://en.wikipedia.org/wiki/Value_object">Value object</a>
037 *
038 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
039 * @version 2.0
040 * @since 1.0
041 */
042 public final class DGPSStation
043 extends Number
044 implements
045 Comparable<DGPSStation>,
046 Serializable
047 {
048
049 @Serial
050 private static final long serialVersionUID = 2L;
051
052 /**
053 * A constant holding the maximum value a {@code DGPSStation} value can have,
054 * 0 inclusively.
055 *
056 * @since 2.0
057 */
058 public static final int MIN_VALUE = 0;
059
060 /**
061 * A constant holding the maximum value a {@code DGPSStation} value can have,
062 * 1023 inclusively.
063 *
064 * @since 2.0
065 */
066 public static final int MAX_VALUE = 1023;
067
068 private final int _value;
069
070 /**
071 * Create a new {@code DGPSStation} object.
072 *
073 * @param value the differential GPS station number
074 * @throws IllegalArgumentException if the given station number is not in the
075 * range of {@code [0..1023]}
076 */
077 private DGPSStation(final int value) {
078 if (value < MIN_VALUE || value > MAX_VALUE) {
079 throw new IllegalArgumentException(format(
080 "%d is out of range [0, 1023].", value
081 ));
082 }
083
084 _value = value;
085 }
086
087 /**
088 * Return the differential GPS station number.
089 *
090 * @return the differential GPS station number
091 */
092 public int intValue() {
093 return _value;
094 }
095
096 @Override
097 public double doubleValue() {
098 return _value;
099 }
100
101 @Override
102 public long longValue() {
103 return _value;
104 }
105
106 @Override
107 public float floatValue() {
108 return (float)_value;
109 }
110
111 @Override
112 public int compareTo(final DGPSStation other) {
113 return Integer.compare(_value, other._value);
114 }
115
116 @Override
117 public int hashCode() {
118 return Integer.hashCode(_value);
119 }
120
121 @Override
122 public boolean equals(final Object obj) {
123 return obj == this ||
124 obj instanceof DGPSStation dgps &&
125 dgps._value == _value;
126 }
127
128 @Override
129 public String toString() {
130 return Integer.toString(_value);
131 }
132
133 /* *************************************************************************
134 * Static object creation methods
135 * ************************************************************************/
136
137 /**
138 * Create a new {@code DGPSStation} object.
139 *
140 * @param value the differential GPS station number
141 * @return a new {@code DGPSStation} object
142 * @throws IllegalArgumentException if the given station number is not in the
143 * range of {@code [0..1023]}
144 */
145 public static DGPSStation of(final int value) {
146 return new DGPSStation(value);
147 }
148
149 static DGPSStation parse(final String value) {
150 final String stat = Strings.trim(value);
151
152 return stat != null
153 ? DGPSStation.of(Integer.parseInt(stat))
154 : null;
155 }
156
157 /* *************************************************************************
158 * Java object serialization
159 * ************************************************************************/
160
161 @Serial
162 private Object writeReplace() {
163 return new SerialProxy(SerialProxy.DGPS_STATION, this);
164 }
165
166 @Serial
167 private void readObject(final ObjectInputStream stream)
168 throws InvalidObjectException
169 {
170 throw new InvalidObjectException("Serialization proxy required.");
171 }
172
173 void write(final DataOutput out) throws IOException {
174 IO.writeInt(_value, out);
175 }
176
177 static DGPSStation read(final DataInput in) throws IOException {
178 return new DGPSStation(IO.readInt(in));
179 }
180
181 }
|