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 package org.eclipse.jgit.lib;
47
48 import java.io.Serializable;
49 import java.text.SimpleDateFormat;
50 import java.util.Date;
51 import java.util.Locale;
52 import java.util.TimeZone;
53
54 import org.eclipse.jgit.internal.JGitText;
55 import org.eclipse.jgit.util.SystemReader;
56
57
58
59
60
61
62
63 public class PersonIdent implements Serializable {
64 private static final long serialVersionUID = 1L;
65
66
67
68
69
70
71
72 public static TimeZone getTimeZone(int tzOffset) {
73 StringBuilder tzId = new StringBuilder(8);
74 tzId.append("GMT");
75 appendTimezone(tzId, tzOffset);
76 return TimeZone.getTimeZone(tzId.toString());
77 }
78
79
80
81
82
83
84
85
86
87
88 public static void appendTimezone(StringBuilder r, int offset) {
89 final char sign;
90 final int offsetHours;
91 final int offsetMins;
92
93 if (offset < 0) {
94 sign = '-';
95 offset = -offset;
96 } else {
97 sign = '+';
98 }
99
100 offsetHours = offset / 60;
101 offsetMins = offset % 60;
102
103 r.append(sign);
104 if (offsetHours < 10) {
105 r.append('0');
106 }
107 r.append(offsetHours);
108 if (offsetMins < 10) {
109 r.append('0');
110 }
111 r.append(offsetMins);
112 }
113
114 private final String name;
115
116 private final String emailAddress;
117
118 private final long when;
119
120 private final int tzOffset;
121
122
123
124
125
126
127
128
129 public PersonIdent(final Repository repo) {
130 this(repo.getConfig().get(UserConfig.KEY));
131 }
132
133
134
135
136
137
138
139 public PersonIdent(final PersonIdent pi) {
140 this(pi.getName(), pi.getEmailAddress());
141 }
142
143
144
145
146
147
148
149 public PersonIdent(final String aName, final String aEmailAddress) {
150 this(aName, aEmailAddress, SystemReader.getInstance().getCurrentTime());
151 }
152
153
154
155
156
157
158
159
160
161
162
163 public PersonIdent(final PersonIdent pi, final Date when, final TimeZone tz) {
164 this(pi.getName(), pi.getEmailAddress(), when, tz);
165 }
166
167
168
169
170
171
172
173
174
175 public PersonIdent(final PersonIdent pi, final Date aWhen) {
176 this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
177 }
178
179
180
181
182
183
184
185
186
187
188
189 public PersonIdent(final String aName, final String aEmailAddress,
190 final Date aWhen, final TimeZone aTZ) {
191 this(aName, aEmailAddress, aWhen.getTime(), aTZ.getOffset(aWhen
192 .getTime()) / (60 * 1000));
193 }
194
195
196
197
198
199
200
201
202
203
204
205 public PersonIdent(final PersonIdent pi, final long aWhen, final int aTZ) {
206 this(pi.getName(), pi.getEmailAddress(), aWhen, aTZ);
207 }
208
209 private PersonIdent(final String aName, final String aEmailAddress,
210 long when) {
211 this(aName, aEmailAddress, when, SystemReader.getInstance()
212 .getTimezone(when));
213 }
214
215 private PersonIdent(final UserConfig config) {
216 this(config.getCommitterName(), config.getCommitterEmail());
217 }
218
219
220
221
222
223
224
225
226
227
228
229 public PersonIdent(final String aName, final String aEmailAddress,
230 final long aWhen, final int aTZ) {
231 if (aName == null)
232 throw new IllegalArgumentException(
233 JGitText.get().personIdentNameNonNull);
234 if (aEmailAddress == null)
235 throw new IllegalArgumentException(
236 JGitText.get().personIdentEmailNonNull);
237 name = aName;
238 emailAddress = aEmailAddress;
239 when = aWhen;
240 tzOffset = aTZ;
241 }
242
243
244
245
246 public String getName() {
247 return name;
248 }
249
250
251
252
253 public String getEmailAddress() {
254 return emailAddress;
255 }
256
257
258
259
260 public Date getWhen() {
261 return new Date(when);
262 }
263
264
265
266
267 public TimeZone getTimeZone() {
268 return getTimeZone(tzOffset);
269 }
270
271
272
273
274
275 public int getTimeZoneOffset() {
276 return tzOffset;
277 }
278
279 public int hashCode() {
280 int hc = getEmailAddress().hashCode();
281 hc *= 31;
282 hc += (int) (when / 1000L);
283 return hc;
284 }
285
286 public boolean equals(final Object o) {
287 if (o instanceof PersonIdent) {
288 final PersonIdent p = (PersonIdent) o;
289 return getName().equals(p.getName())
290 && getEmailAddress().equals(p.getEmailAddress())
291 && when / 1000L == p.when / 1000L;
292 }
293 return false;
294 }
295
296
297
298
299
300
301 public String toExternalString() {
302 final StringBuilder r = new StringBuilder();
303 r.append(getName().trim());
304 r.append(" <");
305 r.append(getEmailAddress().trim());
306 r.append("> ");
307 r.append(when / 1000);
308 r.append(' ');
309 appendTimezone(r, tzOffset);
310 return r.toString();
311 }
312
313 @SuppressWarnings("nls")
314 public String toString() {
315 final StringBuilder r = new StringBuilder();
316 final SimpleDateFormat dtfmt;
317 dtfmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US);
318 dtfmt.setTimeZone(getTimeZone());
319
320 r.append("PersonIdent[");
321 r.append(getName());
322 r.append(", ");
323 r.append(getEmailAddress());
324 r.append(", ");
325 r.append(dtfmt.format(Long.valueOf(when)));
326 r.append("]");
327
328 return r.toString();
329 }
330 }