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