View Javadoc
1   /*
2    * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * A combination of a person identity and time in Git.
59   *
60   * Git combines Name + email + time + time zone to specify who wrote or
61   * committed something.
62   */
63  public class PersonIdent implements Serializable {
64  	private static final long serialVersionUID = 1L;
65  
66  	/**
67  	 * @param tzOffset
68  	 *            timezone offset as in {@link #getTimeZoneOffset()}.
69  	 * @return time zone object for the given offset.
70  	 * @since 4.1
71  	 */
72  	public static TimeZone getTimeZone(int tzOffset) {
73  		StringBuilder tzId = new StringBuilder(8);
74  		tzId.append("GMT"); //$NON-NLS-1$
75  		appendTimezone(tzId, tzOffset);
76  		return TimeZone.getTimeZone(tzId.toString());
77  	}
78  
79  	/**
80  	 * Format a timezone offset.
81  	 *
82  	 * @param r
83  	 *            string builder to append to.
84  	 * @param offset
85  	 *            timezone offset as in {@link #getTimeZoneOffset()}.
86  	 * @since 4.1
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 	private final String name;
115 
116 	private final String emailAddress;
117 
118 	private final long when;
119 
120 	private final int tzOffset;
121 
122 	/**
123 	 * Creates new PersonIdent from config info in repository, with current time.
124 	 * This new PersonIdent gets the info from the default committer as available
125 	 * from the configuration.
126 	 *
127 	 * @param repo
128 	 */
129 	public PersonIdent(final Repository repo) {
130 		this(repo.getConfig().get(UserConfig.KEY));
131 	}
132 
133 	/**
134 	 * Copy a {@link PersonIdent}.
135 	 *
136 	 * @param pi
137 	 *            Original {@link PersonIdent}
138 	 */
139 	public PersonIdent(final PersonIdent pi) {
140 		this(pi.getName(), pi.getEmailAddress());
141 	}
142 
143 	/**
144 	 * Construct a new {@link PersonIdent} with current time.
145 	 *
146 	 * @param aName
147 	 * @param aEmailAddress
148 	 */
149 	public PersonIdent(final String aName, final String aEmailAddress) {
150 		this(aName, aEmailAddress, SystemReader.getInstance().getCurrentTime());
151 	}
152 
153 	/**
154 	 * Copy a PersonIdent, but alter the clone's time stamp
155 	 *
156 	 * @param pi
157 	 *            original {@link PersonIdent}
158 	 * @param when
159 	 *            local time
160 	 * @param tz
161 	 *            time zone
162 	 */
163 	public PersonIdent(final PersonIdent pi, final Date when, final TimeZone tz) {
164 		this(pi.getName(), pi.getEmailAddress(), when, tz);
165 	}
166 
167 	/**
168 	 * Copy a {@link PersonIdent}, but alter the clone's time stamp
169 	 *
170 	 * @param pi
171 	 *            original {@link PersonIdent}
172 	 * @param aWhen
173 	 *            local time
174 	 */
175 	public PersonIdent(final PersonIdent pi, final Date aWhen) {
176 		this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
177 	}
178 
179 	/**
180 	 * Construct a PersonIdent from simple data
181 	 *
182 	 * @param aName
183 	 * @param aEmailAddress
184 	 * @param aWhen
185 	 *            local time stamp
186 	 * @param aTZ
187 	 *            time zone
188 	 */
189 	public PersonIdent(final String aName, final String aEmailAddress,
190 			final Date aWhen, final TimeZone aTZ) {
191 		this(aName, aEmailAddress, aWhen.getTime(), aTZ.getOffset(aWhen
192 				.getTime()) / (60 * 1000));
193 	}
194 
195 	/**
196 	 * Copy a PersonIdent, but alter the clone's time stamp
197 	 *
198 	 * @param pi
199 	 *            original {@link PersonIdent}
200 	 * @param aWhen
201 	 *            local time stamp
202 	 * @param aTZ
203 	 *            time zone
204 	 */
205 	public PersonIdent(final PersonIdent pi, final long aWhen, final int aTZ) {
206 		this(pi.getName(), pi.getEmailAddress(), aWhen, aTZ);
207 	}
208 
209 	private PersonIdent(final String aName, final String aEmailAddress,
210 			long when) {
211 		this(aName, aEmailAddress, when, SystemReader.getInstance()
212 				.getTimezone(when));
213 	}
214 
215 	private PersonIdent(final UserConfig config) {
216 		this(config.getCommitterName(), config.getCommitterEmail());
217 	}
218 
219 	/**
220 	 * Construct a {@link PersonIdent}
221 	 *
222 	 * @param aName
223 	 * @param aEmailAddress
224 	 * @param aWhen
225 	 *            local time stamp
226 	 * @param aTZ
227 	 *            time zone
228 	 */
229 	public PersonIdent(final String aName, final String aEmailAddress,
230 			final long aWhen, final int aTZ) {
231 		if (aName == null)
232 			throw new IllegalArgumentException(
233 					JGitText.get().personIdentNameNonNull);
234 		if (aEmailAddress == null)
235 			throw new IllegalArgumentException(
236 					JGitText.get().personIdentEmailNonNull);
237 		name = aName;
238 		emailAddress = aEmailAddress;
239 		when = aWhen;
240 		tzOffset = aTZ;
241 	}
242 
243 	/**
244 	 * @return Name of person
245 	 */
246 	public String getName() {
247 		return name;
248 	}
249 
250 	/**
251 	 * @return email address of person
252 	 */
253 	public String getEmailAddress() {
254 		return emailAddress;
255 	}
256 
257 	/**
258 	 * @return timestamp
259 	 */
260 	public Date getWhen() {
261 		return new Date(when);
262 	}
263 
264 	/**
265 	 * @return this person's declared time zone; null if time zone is unknown.
266 	 */
267 	public TimeZone getTimeZone() {
268 		return getTimeZone(tzOffset);
269 	}
270 
271 	/**
272 	 * @return this person's declared time zone as minutes east of UTC. If the
273 	 *         timezone is to the west of UTC it is negative.
274 	 */
275 	public int getTimeZoneOffset() {
276 		return tzOffset;
277 	}
278 
279 	public int hashCode() {
280 		int hc = getEmailAddress().hashCode();
281 		hc *= 31;
282 		hc += (int) (when / 1000L);
283 		return hc;
284 	}
285 
286 	public boolean equals(final Object o) {
287 		if (o instanceof PersonIdent) {
288 			final PersonIdent p = (PersonIdent) o;
289 			return getName().equals(p.getName())
290 					&& getEmailAddress().equals(p.getEmailAddress())
291 					&& when / 1000L == p.when / 1000L;
292 		}
293 		return false;
294 	}
295 
296 	/**
297 	 * Format for Git storage.
298 	 *
299 	 * @return a string in the git author format
300 	 */
301 	public String toExternalString() {
302 		final StringBuilder r = new StringBuilder();
303 		r.append(getName().trim());
304 		r.append(" <"); //$NON-NLS-1$
305 		r.append(getEmailAddress().trim());
306 		r.append("> "); //$NON-NLS-1$
307 		r.append(when / 1000);
308 		r.append(' ');
309 		appendTimezone(r, tzOffset);
310 		return r.toString();
311 	}
312 
313 	@SuppressWarnings("nls")
314 	public String toString() {
315 		final StringBuilder r = new StringBuilder();
316 		final SimpleDateFormat dtfmt;
317 		dtfmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US);
318 		dtfmt.setTimeZone(getTimeZone());
319 
320 		r.append("PersonIdent[");
321 		r.append(getName());
322 		r.append(", ");
323 		r.append(getEmailAddress());
324 		r.append(", ");
325 		r.append(dtfmt.format(Long.valueOf(when)));
326 		r.append("]");
327 
328 		return r.toString();
329 	}
330 }