1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.mimo.netty.handler.codec.icap;
15
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.jboss.netty.handler.codec.http.HttpVersion;
20
21
22
23
24
25
26
27 public final class IcapVersion {
28
29 private static final Pattern VERSION_PATTERN = Pattern.compile("(\\S+)/(\\d+)\\.(\\d+)");
30
31 private String protocolName;
32 private int major;
33 private int minor;
34 private String text;
35
36 public static final IcapVersion ICAP_1_0 = new IcapVersion("ICAP", 1, 0);
37
38
39
40
41
42
43 private IcapVersion(String protocolName, int major, int minor) {
44 this.protocolName = protocolName;
45 this.major = major;
46 this.minor = minor;
47 this.text = protocolName + '/' + major + '.' + minor;
48 }
49
50
51
52
53
54 private IcapVersion(String text) {
55 if(text == null) {
56 throw new NullPointerException("text");
57 }
58 Matcher m = VERSION_PATTERN.matcher(text.trim().toUpperCase());
59 if (!m.matches()) {
60 throw new IllegalArgumentException("invalid version format: [" + text + "]");
61 }
62 protocolName = m.group(1);
63 major = Integer.parseInt(m.group(2));
64 minor = Integer.parseInt(m.group(3));
65 this.text = text;
66 }
67
68
69
70
71
72 public String getProtocolName() {
73 return protocolName;
74 }
75
76
77
78
79
80 public int getMajorVersion() {
81 return major;
82 }
83
84
85
86
87
88 public int getMinorVersion() {
89 return minor;
90 }
91
92
93
94
95
96 public String getText() {
97 return text;
98 }
99
100
101
102
103
104
105
106 public static IcapVersion valueOf(String text) {
107 if (text == null) {
108 throw new NullPointerException("text");
109 }
110 if (text.trim().toUpperCase().equals("ICAP/1.0")) {
111 return ICAP_1_0;
112 }
113
114 return new IcapVersion(text);
115 }
116
117 @Override
118 public String toString() {
119 return text;
120 }
121 }