2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.jgit
.lib
;
19 import java
.util
.Calendar
;
20 import java
.util
.Date
;
21 import java
.util
.TimeZone
;
23 public class PersonIdent
{
24 private final String name
;
26 private final String emailAddress
;
28 private final Long when
;
30 private final int tzOffset
;
32 private static String
getHostName() {
34 java
.net
.InetAddress addr
= java
.net
.InetAddress
.getLocalHost();
35 String hostname
= addr
.getCanonicalHostName();
37 } catch (java
.net
.UnknownHostException e
) {
43 * Creates new PersonIdent from config info in repository, with current time
46 public PersonIdent(final Repository repo
) {
47 RepositoryConfig config
= repo
.getConfig();
48 String username
= config
.getString("user", null, "name");
50 username
= System
.getProperty("user.name");
52 String email
= config
.getString("user", null, "email");
54 email
= System
.getProperty("user.name") + "@" + getHostName();
58 when
= Calendar
.getInstance().getTimeInMillis();
59 tzOffset
= TimeZone
.getDefault().getOffset(when
.longValue())
63 public PersonIdent(final PersonIdent pi
) {
64 this(pi
.getName(), pi
.getEmailAddress());
67 public PersonIdent(final String aName
, final String aEmailAddress
) {
68 this(aName
, aEmailAddress
, new Date(), TimeZone
.getDefault());
71 public PersonIdent(final PersonIdent pi
, final Date when
, final TimeZone tz
) {
72 this(pi
.getName(), pi
.getEmailAddress(), when
, tz
);
75 public PersonIdent(final PersonIdent pi
, final Date aWhen
) {
77 emailAddress
= pi
.getEmailAddress();
78 when
= new Long(aWhen
.getTime());
79 tzOffset
= pi
.tzOffset
;
82 public PersonIdent(final String aName
, final String aEmailAddress
,
83 final Date aWhen
, final TimeZone aTZ
) {
85 emailAddress
= aEmailAddress
;
86 when
= new Long(aWhen
.getTime());
87 tzOffset
= aTZ
.getOffset(when
.longValue()) / (60 * 1000);
90 public PersonIdent(final String aName
, final String aEmailAddress
,
91 final long aWhen
, final int aTZ
) {
93 emailAddress
= aEmailAddress
;
94 when
= new Long(aWhen
);
98 public PersonIdent(final PersonIdent pi
, final long aWhen
, final int aTZ
) {
100 emailAddress
= pi
.getEmailAddress();
101 when
= new Long(aWhen
);
105 public PersonIdent(final String in
) {
106 final int lt
= in
.indexOf('<');
108 throw new IllegalArgumentException("Malformed PersonIdent string"
109 + " (no < was found): " + in
);
111 final int gt
= in
.indexOf('>', lt
);
113 throw new IllegalArgumentException("Malformed PersonIdent string"
114 + " (no > was found): " + in
);
116 final int sp
= in
.indexOf(' ', gt
+ 2);
121 final String tzHoursStr
= in
.substring(sp
+ 1, sp
+ 4).trim();
123 if (tzHoursStr
.charAt(0) == '+') {
124 tzHours
= Integer
.parseInt(tzHoursStr
.substring(1));
126 tzHours
= Integer
.parseInt(tzHoursStr
);
128 final int tzMins
= Integer
.parseInt(in
.substring(sp
+ 4).trim());
130 Long
.parseLong(in
.substring(gt
+ 1, sp
).trim()) * 1000);
131 tzOffset
= tzHours
* 60 + tzMins
;
134 name
= in
.substring(0, lt
).trim();
135 emailAddress
= in
.substring(lt
+ 1, gt
).trim();
138 public String
getName() {
142 public String
getEmailAddress() {
146 public Date
getWhen() {
148 return new Date(when
.longValue());
152 public int hashCode() {
153 return getEmailAddress().hashCode() ^
(when
.intValue());
156 public boolean equals(final Object o
) {
157 if (o
instanceof PersonIdent
) {
158 final PersonIdent p
= (PersonIdent
) o
;
159 return getName().equals(p
.getName())
160 && getEmailAddress().equals(p
.getEmailAddress())
161 && (when
== p
.when
|| when
!= null && when
.equals(p
.when
));
166 public String
toExternalString() {
167 final StringBuffer r
= new StringBuffer();
168 int offset
= tzOffset
;
170 final int offsetHours
;
171 final int offsetMins
;
180 offsetHours
= offset
/ 60;
181 offsetMins
= offset
% 60;
185 r
.append(getEmailAddress());
188 r
.append(when
.longValue() / 1000);
191 if (offsetHours
< 10) {
194 r
.append(offsetHours
);
195 if (offsetMins
< 10) {
198 r
.append(offsetMins
);
203 public String
toString() {
204 final StringBuffer r
= new StringBuffer();
207 minutes
= tzOffset
< 0 ?
-tzOffset
: tzOffset
;
208 minutes
= (minutes
/ 100) * 60 + (minutes
% 100);
209 minutes
= tzOffset
< 0 ?
-minutes
: minutes
;
211 r
.append("PersonIdent[");
214 r
.append(getEmailAddress());
217 r
.append(new Date(when
.longValue() + minutes
* 60));