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