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