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