You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PersonIdent.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. package org.eclipse.jgit.lib;
  46. import java.io.Serializable;
  47. import java.text.SimpleDateFormat;
  48. import java.util.Date;
  49. import java.util.Locale;
  50. import java.util.TimeZone;
  51. import org.eclipse.jgit.internal.JGitText;
  52. import org.eclipse.jgit.util.SystemReader;
  53. /**
  54. * A combination of a person identity and time in Git.
  55. *
  56. * Git combines Name + email + time + time zone to specify who wrote or
  57. * committed something.
  58. */
  59. public class PersonIdent implements Serializable {
  60. private static final long serialVersionUID = 1L;
  61. /**
  62. * @param tzOffset
  63. * timezone offset as in {@link #getTimeZoneOffset()}.
  64. * @return time zone object for the given offset.
  65. * @since 4.1
  66. */
  67. public static TimeZone getTimeZone(int tzOffset) {
  68. StringBuilder tzId = new StringBuilder(8);
  69. tzId.append("GMT"); //$NON-NLS-1$
  70. appendTimezone(tzId, tzOffset);
  71. return TimeZone.getTimeZone(tzId.toString());
  72. }
  73. /**
  74. * Format a timezone offset.
  75. *
  76. * @param r
  77. * string builder to append to.
  78. * @param offset
  79. * timezone offset as in {@link #getTimeZoneOffset()}.
  80. * @since 4.1
  81. */
  82. public static void appendTimezone(StringBuilder r, int offset) {
  83. final char sign;
  84. final int offsetHours;
  85. final int offsetMins;
  86. if (offset < 0) {
  87. sign = '-';
  88. offset = -offset;
  89. } else {
  90. sign = '+';
  91. }
  92. offsetHours = offset / 60;
  93. offsetMins = offset % 60;
  94. r.append(sign);
  95. if (offsetHours < 10) {
  96. r.append('0');
  97. }
  98. r.append(offsetHours);
  99. if (offsetMins < 10) {
  100. r.append('0');
  101. }
  102. r.append(offsetMins);
  103. }
  104. private final String name;
  105. private final String emailAddress;
  106. private final long when;
  107. private final int tzOffset;
  108. /**
  109. * Creates new PersonIdent from config info in repository, with current time.
  110. * This new PersonIdent gets the info from the default committer as available
  111. * from the configuration.
  112. *
  113. * @param repo
  114. */
  115. public PersonIdent(final Repository repo) {
  116. this(repo.getConfig().get(UserConfig.KEY));
  117. }
  118. /**
  119. * Copy a {@link PersonIdent}.
  120. *
  121. * @param pi
  122. * Original {@link PersonIdent}
  123. */
  124. public PersonIdent(final PersonIdent pi) {
  125. this(pi.getName(), pi.getEmailAddress());
  126. }
  127. /**
  128. * Construct a new {@link PersonIdent} with current time.
  129. *
  130. * @param aName
  131. * @param aEmailAddress
  132. */
  133. public PersonIdent(final String aName, final String aEmailAddress) {
  134. this(aName, aEmailAddress, SystemReader.getInstance().getCurrentTime());
  135. }
  136. /**
  137. * Copy a PersonIdent, but alter the clone's time stamp
  138. *
  139. * @param pi
  140. * original {@link PersonIdent}
  141. * @param when
  142. * local time
  143. * @param tz
  144. * time zone
  145. */
  146. public PersonIdent(final PersonIdent pi, final Date when, final TimeZone tz) {
  147. this(pi.getName(), pi.getEmailAddress(), when, tz);
  148. }
  149. /**
  150. * Copy a {@link PersonIdent}, but alter the clone's time stamp
  151. *
  152. * @param pi
  153. * original {@link PersonIdent}
  154. * @param aWhen
  155. * local time
  156. */
  157. public PersonIdent(final PersonIdent pi, final Date aWhen) {
  158. this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
  159. }
  160. /**
  161. * Construct a PersonIdent from simple data
  162. *
  163. * @param aName
  164. * @param aEmailAddress
  165. * @param aWhen
  166. * local time stamp
  167. * @param aTZ
  168. * time zone
  169. */
  170. public PersonIdent(final String aName, final String aEmailAddress,
  171. final Date aWhen, final TimeZone aTZ) {
  172. this(aName, aEmailAddress, aWhen.getTime(), aTZ.getOffset(aWhen
  173. .getTime()) / (60 * 1000));
  174. }
  175. /**
  176. * Copy a PersonIdent, but alter the clone's time stamp
  177. *
  178. * @param pi
  179. * original {@link PersonIdent}
  180. * @param aWhen
  181. * local time stamp
  182. * @param aTZ
  183. * time zone
  184. */
  185. public PersonIdent(final PersonIdent pi, final long aWhen, final int aTZ) {
  186. this(pi.getName(), pi.getEmailAddress(), aWhen, aTZ);
  187. }
  188. private PersonIdent(final String aName, final String aEmailAddress,
  189. long when) {
  190. this(aName, aEmailAddress, when, SystemReader.getInstance()
  191. .getTimezone(when));
  192. }
  193. private PersonIdent(final UserConfig config) {
  194. this(config.getCommitterName(), config.getCommitterEmail());
  195. }
  196. /**
  197. * Construct a {@link PersonIdent}.
  198. * <p>
  199. * Whitespace in the name and email is preserved for the lifetime of this
  200. * object, but are trimmed by {@link #toExternalString()}. This means that
  201. * parsing the result of {@link #toExternalString()} may not return an
  202. * equivalent instance.
  203. *
  204. * @param aName
  205. * @param aEmailAddress
  206. * @param aWhen
  207. * local time stamp
  208. * @param aTZ
  209. * time zone
  210. */
  211. public PersonIdent(final String aName, final String aEmailAddress,
  212. final long aWhen, final int aTZ) {
  213. if (aName == null)
  214. throw new IllegalArgumentException(
  215. JGitText.get().personIdentNameNonNull);
  216. if (aEmailAddress == null)
  217. throw new IllegalArgumentException(
  218. JGitText.get().personIdentEmailNonNull);
  219. name = aName;
  220. emailAddress = aEmailAddress;
  221. when = aWhen;
  222. tzOffset = aTZ;
  223. }
  224. /**
  225. * @return Name of person
  226. */
  227. public String getName() {
  228. return name;
  229. }
  230. /**
  231. * @return email address of person
  232. */
  233. public String getEmailAddress() {
  234. return emailAddress;
  235. }
  236. /**
  237. * @return timestamp
  238. */
  239. public Date getWhen() {
  240. return new Date(when);
  241. }
  242. /**
  243. * @return this person's declared time zone; null if time zone is unknown.
  244. */
  245. public TimeZone getTimeZone() {
  246. return getTimeZone(tzOffset);
  247. }
  248. /**
  249. * @return this person's declared time zone as minutes east of UTC. If the
  250. * timezone is to the west of UTC it is negative.
  251. */
  252. public int getTimeZoneOffset() {
  253. return tzOffset;
  254. }
  255. public int hashCode() {
  256. int hc = getEmailAddress().hashCode();
  257. hc *= 31;
  258. hc += (int) (when / 1000L);
  259. return hc;
  260. }
  261. public boolean equals(final Object o) {
  262. if (o instanceof PersonIdent) {
  263. final PersonIdent p = (PersonIdent) o;
  264. return getName().equals(p.getName())
  265. && getEmailAddress().equals(p.getEmailAddress())
  266. && when / 1000L == p.when / 1000L;
  267. }
  268. return false;
  269. }
  270. /**
  271. * Format for Git storage.
  272. *
  273. * @return a string in the git author format
  274. */
  275. public String toExternalString() {
  276. final StringBuilder r = new StringBuilder();
  277. r.append(getName().trim());
  278. r.append(" <"); //$NON-NLS-1$
  279. r.append(getEmailAddress().trim());
  280. r.append("> "); //$NON-NLS-1$
  281. r.append(when / 1000);
  282. r.append(' ');
  283. appendTimezone(r, tzOffset);
  284. return r.toString();
  285. }
  286. @SuppressWarnings("nls")
  287. public String toString() {
  288. final StringBuilder r = new StringBuilder();
  289. final SimpleDateFormat dtfmt;
  290. dtfmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US);
  291. dtfmt.setTimeZone(getTimeZone());
  292. r.append("PersonIdent[");
  293. r.append(getName());
  294. r.append(", ");
  295. r.append(getEmailAddress());
  296. r.append(", ");
  297. r.append(dtfmt.format(Long.valueOf(when)));
  298. r.append("]");
  299. return r.toString();
  300. }
  301. }