1 /*
2 * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3 */
4 package de.powerstat.validation.values;
5
6
7 import java.util.Objects;
8
9 import de.powerstat.validation.interfaces.IValueObject;
10
11
12 /**
13 * Port.
14 *
15 * Not DSGVO relevant.
16 *
17 * TODO min, max?
18 */
19 public final class Port implements Comparable<Port>, IValueObject
20 {
21 /* *
22 * Cache for singletons.
23 */
24 // private static final Map<Integer, Port> CACHE = new WeakHashMap<>();
25
26 /**
27 * Port.
28 */
29 private final int port;
30
31
32 /**
33 * Constructor.
34 *
35 * @param port Port 0-65535
36 * @throws IndexOutOfBoundsException When the port is less than 0 or greater than 65535
37 */
38 private Port(final int port)
39 {
40 super();
41 if ((port < 0) || (port > 65535))
42 {
43 throw new IndexOutOfBoundsException("Port number out of range (0-65535)!"); //$NON-NLS-1$
44 }
45 this.port = port;
46 }
47
48
49 /**
50 * Port factory.
51 *
52 * @param port Port 0-65535
53 * @return Port object
54 */
55 public static Port of(final int port)
56 {
57 /*
58 synchronized (Port.class)
59 {
60 Port obj = Port.CACHE.get(port);
61 if (obj != null)
62 {
63 return obj;
64 }
65 obj = new Port(port);
66 Port.CACHE.put(Integer.valueOf(port), obj);
67 return obj;
68 }
69 */
70 return new Port(port);
71 }
72
73
74 /**
75 * Port factory.
76 *
77 * @param port Port 0-65535
78 * @return Port object
79 */
80 public static Port of(final String port)
81 {
82 return of(Integer.parseInt(port));
83 }
84
85
86 /**
87 * Is system port (0-1023).
88 *
89 * @return true: system port, otherwise false
90 */
91 public boolean isSystem()
92 {
93 return this.port <= 1023;
94 }
95
96
97 /**
98 * Is registered port (1024-49151).
99 *
100 * @return true: registered port, otherwise false
101 */
102 public boolean isRegistered()
103 {
104 return (this.port >= 1024) && (this.port <= 49151);
105 }
106
107
108 /**
109 * Is dynamic port (49152-65535).
110 *
111 * @return true: dynamic port, otherwise false
112 */
113 public boolean isDynamic()
114 {
115 return (this.port >= 49152);
116 }
117
118
119 /**
120 * Returns the value of this Port as an int.
121 *
122 * @return The numeric value represented by this object after conversion to type int.
123 */
124 public int intValue()
125 {
126 return this.port;
127 }
128
129
130 /**
131 * Returns the value of this Port as an String.
132 *
133 * @return The numeric value represented by this object after conversion to type String.
134 */
135 @Override
136 public String stringValue()
137 {
138 return String.valueOf(this.port);
139 }
140
141
142 /**
143 * Calculate hash code.
144 *
145 * @return Hash
146 * @see java.lang.Object#hashCode()
147 */
148 @Override
149 public int hashCode()
150 {
151 return Integer.hashCode(this.port);
152 }
153
154
155 /**
156 * Is equal with another object.
157 *
158 * @param obj Object
159 * @return true when equal, false otherwise
160 * @see java.lang.Object#equals(java.lang.Object)
161 */
162 @Override
163 public boolean equals(final Object obj)
164 {
165 if (this == obj)
166 {
167 return true;
168 }
169 if (!(obj instanceof Port))
170 {
171 return false;
172 }
173 final Port other = (Port)obj;
174 return this.port == other.port;
175 }
176
177
178 /**
179 * Returns the string representation of this Port.
180 *
181 * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
182 *
183 * "Port[port=0]"
184 *
185 * @return String representation of this Port
186 * @see java.lang.Object#toString()
187 */
188 @Override
189 public String toString()
190 {
191 final var builder = new StringBuilder();
192 builder.append("Port[port=").append(this.port).append(']'); //$NON-NLS-1$
193 return builder.toString();
194 }
195
196
197 /**
198 * Compare with another object.
199 *
200 * @param obj Object to compare with
201 * @return 0: equal; 1: greater; -1: smaller
202 * @see java.lang.Comparable#compareTo(java.lang.Object)
203 */
204 @Override
205 public int compareTo(final Port obj)
206 {
207 Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
208 return Integer.compare(this.port, obj.port);
209 }
210
211 }