Introduce subsection as a separate argument to the config API
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / PersonIdent.java
blob02eb6ff52c56fa711d8ea99514013602d8baa855
1 /*
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() {
33 try {
34 java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
35 String hostname = addr.getCanonicalHostName();
36 return hostname;
37 } catch (java.net.UnknownHostException e) {
38 return "localhost";
42 /**
43 * Creates new PersonIdent from config info in repository, with current time
44 * @param repo
46 public PersonIdent(final Repository repo) {
47 RepositoryConfig config = repo.getConfig();
48 String username = config.getString("user", null, "name");
49 if (username == null)
50 username = System.getProperty("user.name");
52 String email = config.getString("user", null, "email");
53 if (email == null)
54 email = System.getProperty("user.name") + "@" + getHostName();
56 name = username;
57 emailAddress = email;
58 when = Calendar.getInstance().getTimeInMillis();
59 tzOffset = TimeZone.getDefault().getOffset(when.longValue())
60 / (60 * 1000);
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) {
76 name = pi.getName();
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) {
84 name = aName;
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) {
92 name = aName;
93 emailAddress = aEmailAddress;
94 when = new Long(aWhen);
95 tzOffset = aTZ;
98 public PersonIdent(final PersonIdent pi, final long aWhen, final int aTZ) {
99 name = pi.getName();
100 emailAddress = pi.getEmailAddress();
101 when = new Long(aWhen);
102 tzOffset = aTZ;
105 public PersonIdent(final String in) {
106 final int lt = in.indexOf('<');
107 if (lt == -1) {
108 throw new IllegalArgumentException("Malformed PersonIdent string"
109 + " (no < was found): " + in);
111 final int gt = in.indexOf('>', lt);
112 if (gt == -1) {
113 throw new IllegalArgumentException("Malformed PersonIdent string"
114 + " (no > was found): " + in);
116 final int sp = in.indexOf(' ', gt + 2);
117 if (sp == -1) {
118 when = null;
119 tzOffset = -1;
120 } else {
121 final String tzHoursStr = in.substring(sp + 1, sp + 4).trim();
122 final int tzHours;
123 if (tzHoursStr.charAt(0) == '+') {
124 tzHours = Integer.parseInt(tzHoursStr.substring(1));
125 } else {
126 tzHours = Integer.parseInt(tzHoursStr);
128 final int tzMins = Integer.parseInt(in.substring(sp + 4).trim());
129 when = new Long(
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() {
139 return name;
142 public String getEmailAddress() {
143 return emailAddress;
146 public Date getWhen() {
147 if (when != null)
148 return new Date(when.longValue());
149 return null;
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));
163 return false;
166 public String toExternalString() {
167 final StringBuffer r = new StringBuffer();
168 int offset = tzOffset;
169 final char sign;
170 final int offsetHours;
171 final int offsetMins;
173 if (offset < 0) {
174 sign = '-';
175 offset = -offset;
176 } else {
177 sign = '+';
180 offsetHours = offset / 60;
181 offsetMins = offset % 60;
183 r.append(getName());
184 r.append(" <");
185 r.append(getEmailAddress());
186 r.append("> ");
187 if (when != null) {
188 r.append(when.longValue() / 1000);
189 r.append(' ');
190 r.append(sign);
191 if (offsetHours < 10) {
192 r.append('0');
194 r.append(offsetHours);
195 if (offsetMins < 10) {
196 r.append('0');
198 r.append(offsetMins);
200 return r.toString();
203 public String toString() {
204 final StringBuffer r = new StringBuffer();
205 int minutes;
207 minutes = tzOffset < 0 ? -tzOffset : tzOffset;
208 minutes = (minutes / 100) * 60 + (minutes % 100);
209 minutes = tzOffset < 0 ? -minutes : minutes;
211 r.append("PersonIdent[");
212 r.append(getName());
213 r.append(", ");
214 r.append(getEmailAddress());
215 r.append(", ");
216 if (when != null) {
217 r.append(new Date(when.longValue() + minutes * 60));
219 r.append("]");
221 return r.toString();