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 import org.eclipse.jgit.util.time.ProposedTimestamp;
57
58
59
60
61
62
63
64 public class PersonIdent implements Serializable {
65 private static final long serialVersionUID = 1L;
66
67
68
69
70
71
72
73
74
75 public static TimeZone getTimeZone(int tzOffset) {
76 StringBuilder tzId = new StringBuilder(8);
77 tzId.append("GMT");
78 appendTimezone(tzId, tzOffset);
79 return TimeZone.getTimeZone(tzId.toString());
80 }
81
82
83
84
85
86
87
88
89
90
91 public static void appendTimezone(StringBuilder r, int offset) {
92 final char sign;
93 final int offsetHours;
94 final int offsetMins;
95
96 if (offset < 0) {
97 sign = '-';
98 offset = -offset;
99 } else {
100 sign = '+';
101 }
102
103 offsetHours = offset / 60;
104 offsetMins = offset % 60;
105
106 r.append(sign);
107 if (offsetHours < 10) {
108 r.append('0');
109 }
110 r.append(offsetHours);
111 if (offsetMins < 10) {
112 r.append('0');
113 }
114 r.append(offsetMins);
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public static void appendSanitized(StringBuilder r, String str) {
131
132 int i = 0;
133 while (i < str.length() && str.charAt(i) <= ' ') {
134 i++;
135 }
136 int end = str.length();
137 while (end > i && str.charAt(end - 1) <= ' ') {
138 end--;
139 }
140
141 for (; i < end; i++) {
142 char c = str.charAt(i);
143 switch (c) {
144 case '\n':
145 case '<':
146 case '>':
147 continue;
148 default:
149 r.append(c);
150 break;
151 }
152 }
153 }
154
155 private final String name;
156
157 private final String emailAddress;
158
159 private final long when;
160
161 private final int tzOffset;
162
163
164
165
166
167
168
169
170 public PersonIdent(Repository repo) {
171 this(repo.getConfig().get(UserConfig.KEY));
172 }
173
174
175
176
177
178
179
180 public PersonIdent(PersonIdent pi) {
181 this(pi.getName(), pi.getEmailAddress());
182 }
183
184
185
186
187
188
189
190
191
192
193 public PersonIdent(String aName, String aEmailAddress) {
194 this(aName, aEmailAddress, SystemReader.getInstance().getCurrentTime());
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209 public PersonIdent(String aName, String aEmailAddress,
210 ProposedTimestamp when) {
211 this(aName, aEmailAddress, when.millis());
212 }
213
214
215
216
217
218
219
220
221
222
223
224 public PersonIdent(PersonIdent pi, Date when, TimeZone tz) {
225 this(pi.getName(), pi.getEmailAddress(), when, tz);
226 }
227
228
229
230
231
232
233
234
235
236
237 public PersonIdent(PersonIdent pi, Date aWhen) {
238 this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
239 }
240
241
242
243
244
245
246
247
248
249
250
251 public PersonIdent(final String aName, final String aEmailAddress,
252 final Date aWhen, final TimeZone aTZ) {
253 this(aName, aEmailAddress, aWhen.getTime(), aTZ.getOffset(aWhen
254 .getTime()) / (60 * 1000));
255 }
256
257
258
259
260
261
262
263
264
265
266
267 public PersonIdent(PersonIdent pi, long aWhen, int aTZ) {
268 this(pi.getName(), pi.getEmailAddress(), aWhen, aTZ);
269 }
270
271 private PersonIdent(final String aName, final String aEmailAddress,
272 long when) {
273 this(aName, aEmailAddress, when, SystemReader.getInstance()
274 .getTimezone(when));
275 }
276
277 private PersonIdent(UserConfig config) {
278 this(config.getCommitterName(), config.getCommitterEmail());
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 public PersonIdent(final String aName, final String aEmailAddress,
299 final long aWhen, final int aTZ) {
300 if (aName == null)
301 throw new IllegalArgumentException(
302 JGitText.get().personIdentNameNonNull);
303 if (aEmailAddress == null)
304 throw new IllegalArgumentException(
305 JGitText.get().personIdentEmailNonNull);
306 name = aName;
307 emailAddress = aEmailAddress;
308 when = aWhen;
309 tzOffset = aTZ;
310 }
311
312
313
314
315
316
317 public String getName() {
318 return name;
319 }
320
321
322
323
324
325
326 public String getEmailAddress() {
327 return emailAddress;
328 }
329
330
331
332
333
334
335 public Date getWhen() {
336 return new Date(when);
337 }
338
339
340
341
342
343
344 public TimeZone getTimeZone() {
345 return getTimeZone(tzOffset);
346 }
347
348
349
350
351
352
353
354 public int getTimeZoneOffset() {
355 return tzOffset;
356 }
357
358
359
360
361
362
363 @Override
364 public int hashCode() {
365 int hc = getEmailAddress().hashCode();
366 hc *= 31;
367 hc += (int) (when / 1000L);
368 return hc;
369 }
370
371
372 @Override
373 public boolean equals(Object o) {
374 if (o instanceof PersonIdent) {
375 final PersonIdent p = (PersonIdent) o;
376 return getName().equals(p.getName())
377 && getEmailAddress().equals(p.getEmailAddress())
378 && when / 1000L == p.when / 1000L;
379 }
380 return false;
381 }
382
383
384
385
386
387
388 public String toExternalString() {
389 final StringBuilder r = new StringBuilder();
390 appendSanitized(r, getName());
391 r.append(" <");
392 appendSanitized(r, getEmailAddress());
393 r.append("> ");
394 r.append(when / 1000);
395 r.append(' ');
396 appendTimezone(r, tzOffset);
397 return r.toString();
398 }
399
400
401 @Override
402 @SuppressWarnings("nls")
403 public String toString() {
404 final StringBuilder r = new StringBuilder();
405 final SimpleDateFormat dtfmt;
406 dtfmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US);
407 dtfmt.setTimeZone(getTimeZone());
408
409 r.append("PersonIdent[");
410 r.append(getName());
411 r.append(", ");
412 r.append(getEmailAddress());
413 r.append(", ");
414 r.append(dtfmt.format(Long.valueOf(when)));
415 r.append("]");
416
417 return r.toString();
418 }
419 }
420