1
2
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
14
15
16
17
18
19 public final class Port implements Comparable<Port>, IValueObject
20 {
21
22
23
24
25
26
27
28
29 private final int port;
30
31
32
33
34
35
36
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)!");
44 }
45 this.port = port;
46 }
47
48
49
50
51
52
53
54
55 public static Port of(final int port)
56 {
57
58
59
60
61
62
63
64
65
66
67
68
69
70 return new Port(port);
71 }
72
73
74
75
76
77
78
79
80 public static Port of(final String port)
81 {
82 return of(Integer.parseInt(port));
83 }
84
85
86
87
88
89
90
91 public boolean isSystem()
92 {
93 return this.port <= 1023;
94 }
95
96
97
98
99
100
101
102 public boolean isRegistered()
103 {
104 return (this.port >= 1024) && (this.port <= 49151);
105 }
106
107
108
109
110
111
112
113 public boolean isDynamic()
114 {
115 return (this.port >= 49152);
116 }
117
118
119
120
121
122
123
124 public int intValue()
125 {
126 return this.port;
127 }
128
129
130
131
132
133
134
135 @Override
136 public String stringValue()
137 {
138 return String.valueOf(this.port);
139 }
140
141
142
143
144
145
146
147
148 @Override
149 public int hashCode()
150 {
151 return Integer.hashCode(this.port);
152 }
153
154
155
156
157
158
159
160
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
180
181
182
183
184
185
186
187
188 @Override
189 public String toString()
190 {
191 final var builder = new StringBuilder();
192 builder.append("Port[port=").append(this.port).append(']');
193 return builder.toString();
194 }
195
196
197
198
199
200
201
202
203
204 @Override
205 public int compareTo(final Port obj)
206 {
207 Objects.requireNonNull(obj, "obj");
208 return Integer.compare(this.port, obj.port);
209 }
210
211 }