UserAgent.java
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.espeed.ua;
import java.io.Serializable;
public class UserAgent implements Serializable
{
private static final long serialVersionUID = 7025462762784240212L;
private OperatingSystem operatingSystem;
private Browser browser;
private int id;
private String userAgentString;
/**
* This constructor is created for APIs that require default constructor
* and should never be used directly.
* @deprecated Use {@link #UserAgent(OperatingSystem, Browser)}
*/
@Deprecated
public UserAgent()
{
this(OperatingSystem.UNKNOWN, Browser.UNKNOWN);
}
public UserAgent(OperatingSystem operatingSystem, Browser browser)
{
this.operatingSystem = operatingSystem;
this.browser = browser;
this.id = (( operatingSystem.getId() << 16) + browser.getId());
}
public UserAgent(String userAgentString)
{
String userAgentLowercaseString = userAgentString == null ? null : userAgentString.toLowerCase();
Browser browser = Browser.parseUserAgentLowercaseString(userAgentLowercaseString);
OperatingSystem operatingSystem = OperatingSystem.UNKNOWN;
// BOTs don't have an interesting OS for us
if (browser != Browser.BOT)
operatingSystem = OperatingSystem.parseUserAgentLowercaseString(userAgentLowercaseString);
this.operatingSystem = operatingSystem;
this.browser = browser;
this.id = ((operatingSystem.getId() << 16) + browser.getId());
this.userAgentString = userAgentString;
}
/**
* @param userAgentString User-agent string as provided in the request.
* @return UserAgent
*/
public static UserAgent parseUserAgentString(String userAgentString) {
return new UserAgent(userAgentString);
}
/**
* Detects the detailed version information of the browser. Depends on the userAgent to be available.
* Use it only after using UserAgent(String) or UserAgent.parseUserAgent(String).
* Returns null if it can not detect the version information.
* @return Version
*/
public Version getBrowserVersion() {
return this.browser.getVersion(this.userAgentString);
}
/**
* @return the system
*/
public OperatingSystem getOperatingSystem() {
return operatingSystem;
}
/**
* @return the browser
*/
public Browser getBrowser() {
return browser;
}
/**
* Returns an unique integer value of the operating system and browser combination
* @return the id
*/
public int getId() {
return id;
}
/**
* Combined string representation of both enums
*/
public String toString() {
return this.operatingSystem.toString() + "-" + this.browser.toString();
}
/**
* Returns UserAgent based on specified unique id
* @param id Id value of the user agent.
* @return UserAgent
*/
public static UserAgent valueOf(int id)
{
OperatingSystem operatingSystem = OperatingSystem.valueOf((short) (id >> 16));
Browser browser = Browser.valueOf( (short) (id & 0x0FFFF));
return new UserAgent(operatingSystem,browser);
}
/**
* Returns UserAgent based on combined string representation
* @param name Name of the user agent.
* @return UserAgent
*/
public static UserAgent valueOf(String name)
{
if (name == null)
throw new NullPointerException("Name is null");
String[] elements = name.split("-");
if (elements.length == 2)
{
OperatingSystem operatingSystem = OperatingSystem.valueOf(elements[0]);
Browser browser = Browser.valueOf(elements[1]);
return new UserAgent(operatingSystem,browser);
}
throw new IllegalArgumentException(
"Invalid string for userAgent " + name);
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((browser == null) ? 0 : browser.hashCode());
result = prime * result + id;
result = prime * result
+ ((operatingSystem == null) ? 0 : operatingSystem.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final UserAgent other = (UserAgent) obj;
if (browser == null) {
if (other.browser != null)
return false;
} else if (!browser.equals(other.browser))
return false;
if (id != other.id)
return false;
if (operatingSystem == null) {
if (other.operatingSystem != null)
return false;
} else if (!operatingSystem.equals(other.operatingSystem))
return false;
return true;
}
}