Last change
on this file since 193 was
193,
checked in by Maciej Komosinski, 10 years ago
|
Set svn:eol-style native for all textual files
|
-
Property svn:eol-style set to
native
|
File size:
1.2 KB
|
Line | |
---|
1 | package com.framsticks.communication; |
---|
2 | |
---|
3 | import java.util.regex.Matcher; |
---|
4 | import java.util.regex.Pattern; |
---|
5 | |
---|
6 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
7 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
8 | import com.framsticks.util.FramsticksException; |
---|
9 | |
---|
10 | @FramsClassAnnotation |
---|
11 | public class Address { |
---|
12 | |
---|
13 | private final static Pattern pattern = Pattern.compile("^([^:]*)(:([0-9]+))?$"); |
---|
14 | |
---|
15 | protected final String hostName; |
---|
16 | protected final int port; |
---|
17 | |
---|
18 | /** |
---|
19 | * @param hostName |
---|
20 | * @param port |
---|
21 | */ |
---|
22 | public Address(String hostName, int port) { |
---|
23 | this.hostName = hostName; |
---|
24 | this.port = port; |
---|
25 | } |
---|
26 | |
---|
27 | public Address(String address) { |
---|
28 | Matcher matcher = pattern.matcher(address); |
---|
29 | if (!matcher.matches()) { |
---|
30 | throw new FramsticksException().msg("invalid address").arg("address", address); |
---|
31 | } |
---|
32 | hostName = matcher.group(1); |
---|
33 | port = matcher.group(3) != null ? Integer.parseInt(matcher.group(3)) : 9009; |
---|
34 | } |
---|
35 | |
---|
36 | @Override |
---|
37 | public String toString() { |
---|
38 | return hostName + ":" + port; |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * @return the hostName |
---|
43 | */ |
---|
44 | @ParamAnnotation |
---|
45 | public String getHostName() { |
---|
46 | return hostName; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * @return the port |
---|
51 | */ |
---|
52 | @ParamAnnotation |
---|
53 | public int getPort() { |
---|
54 | return port; |
---|
55 | } |
---|
56 | |
---|
57 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.