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 an unsigned integer value.
034 *
035 * @see <a href="https://en.wikipedia.org/wiki/Value_object">Value object</a>
036 *
037 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
038 * @version 1.2
039 * @since 1.0
040 */
041 public final class UInt
042 extends Number
043 implements
044 Comparable<UInt>,
045 Serializable
046 {
047
048 @Serial
049 private static final long serialVersionUID = 2L;
050
051 private final int _value;
052
053 /**
054 * Create a new unsigned integer object with the given value.
055 *
056 * @param value the {@code UInt} value
057 * @throws IllegalArgumentException if the given {@code value} is smaller
058 * than zero
059 */
060 private UInt(final int value) {
061 if (value < 0) {
062 throw new IllegalArgumentException(format("%d is negative.", value));
063 }
064 _value = value;
065 }
066
067 /**
068 * Return the unsigned integer value.
069 *
070 * @return the unsigned integer value
071 */
072 public int getValue() {
073 return _value;
074 }
075
076 @Override
077 public int intValue() {
078 return _value;
079 }
080
081 @Override
082 public long longValue() {
083 return _value;
084 }
085
086 @Override
087 public float floatValue() {
088 return _value;
089 }
090
091 @Override
092 public double doubleValue() {
093 return _value;
094 }
095
096 @Override
097 public int compareTo(final UInt other) {
098 return Integer.compare(_value, other._value);
099 }
100
101 @Override
102 public int hashCode() {
103 return Integer.hashCode(_value);
104 }
105
106 @Override
107 public boolean equals(final Object obj) {
108 return obj == this ||
109 obj instanceof UInt uint &&
110 uint._value == _value;
111 }
112
113 @Override
114 public String toString() {
115 return Integer.toString(_value);
116 }
117
118
119 /* *************************************************************************
120 * Static object creation methods
121 * ************************************************************************/
122
123 /**
124 * Create a new unsigned integer object with the given value.
125 *
126 * @param value the {@code UInt} value
127 * @return a new unsigned integer object with the given value
128 * @throws IllegalArgumentException if the given {@code value} is smaller
129 * than zero
130 */
131 public static UInt of(final int value) {
132 return new UInt(value);
133 }
134
135 static UInt parse(final String value) {
136 final String uint = Strings.trim(value);
137
138 return uint != null
139 ? UInt.of(Integer.parseInt(uint))
140 : null;
141 }
142
143 /* *************************************************************************
144 * Java object serialization
145 * ************************************************************************/
146
147 @Serial
148 private Object writeReplace() {
149 return new SerialProxy(SerialProxy.UINT, this);
150 }
151
152 @Serial
153 private void readObject(final ObjectInputStream stream)
154 throws InvalidObjectException
155 {
156 throw new InvalidObjectException("Serialization proxy required.");
157 }
158
159 void write(final DataOutput out) throws IOException {
160 IO.writeInt(_value, out);
161 }
162
163 static UInt read(final DataInput in) throws IOException {
164 return new UInt(IO.readInt(in));
165 }
166
167 }
|