PersonIdent: Add ctors that accept Instant in addition to Date
Error Prone is flagging Date-API as obsolete and recommends to migrate to Instant and LocalDate. Given that more JGit users starting to migrate to new Time API, offer ctors that accept Instant type and also add new getter that returns when attribute as Instant type. Change-Id: I64a36bf40f191495c6889c1dff314ede06848880
This commit is contained in:
parent
889b82f0e5
commit
67097f5de4
|
@ -13,6 +13,8 @@
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
@ -42,6 +44,34 @@ public void test002_NewIdent() {
|
||||||
p.toExternalString());
|
p.toExternalString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNewIdentInstant() {
|
||||||
|
PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
|
||||||
|
Instant.ofEpochMilli(1142878501000L),
|
||||||
|
ZoneId.of("America/New_York"));
|
||||||
|
assertEquals("A U Thor", p.getName());
|
||||||
|
assertEquals("author@example.com", p.getEmailAddress());
|
||||||
|
assertEquals(Instant.ofEpochMilli(1142878501000L),
|
||||||
|
p.getWhenAsInstant());
|
||||||
|
assertEquals("A U Thor <author@example.com> 1142878501 -0500",
|
||||||
|
p.toExternalString());
|
||||||
|
assertEquals(ZoneId.of("GMT-05:00"), p.getZoneId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNewIdentInstant2() {
|
||||||
|
final PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
|
||||||
|
Instant.ofEpochMilli(1142878501000L),
|
||||||
|
ZoneId.of("Asia/Kolkata"));
|
||||||
|
assertEquals("A U Thor", p.getName());
|
||||||
|
assertEquals("author@example.com", p.getEmailAddress());
|
||||||
|
assertEquals(Instant.ofEpochMilli(1142878501000L),
|
||||||
|
p.getWhenAsInstant());
|
||||||
|
assertEquals("A U Thor <author@example.com> 1142878501 +0530",
|
||||||
|
p.toExternalString());
|
||||||
|
assertEquals(ZoneId.of("GMT+05:30"), p.getZoneId());
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void nullForNameShouldThrowIllegalArgumentException() {
|
public void nullForNameShouldThrowIllegalArgumentException() {
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
@ -205,6 +207,20 @@ public PersonIdent(PersonIdent pi, Date aWhen) {
|
||||||
this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
|
this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy a {@link org.eclipse.jgit.lib.PersonIdent}, but alter the clone's
|
||||||
|
* time stamp
|
||||||
|
*
|
||||||
|
* @param pi
|
||||||
|
* original {@link org.eclipse.jgit.lib.PersonIdent}
|
||||||
|
* @param aWhen
|
||||||
|
* local time as Instant
|
||||||
|
* @since 6.1
|
||||||
|
*/
|
||||||
|
public PersonIdent(PersonIdent pi, Instant aWhen) {
|
||||||
|
this(pi.getName(), pi.getEmailAddress(), aWhen.toEpochMilli(), pi.tzOffset);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a PersonIdent from simple data
|
* Construct a PersonIdent from simple data
|
||||||
*
|
*
|
||||||
|
@ -221,6 +237,27 @@ public PersonIdent(final String aName, final String aEmailAddress,
|
||||||
.getTime()) / (60 * 1000));
|
.getTime()) / (60 * 1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a PersonIdent from simple data
|
||||||
|
*
|
||||||
|
* @param aName
|
||||||
|
* a {@link java.lang.String} object.
|
||||||
|
* @param aEmailAddress
|
||||||
|
* a {@link java.lang.String} object.
|
||||||
|
* @param aWhen
|
||||||
|
* local time stamp
|
||||||
|
* @param zoneId
|
||||||
|
* time zone id
|
||||||
|
* @since 6.1
|
||||||
|
*/
|
||||||
|
public PersonIdent(final String aName, String aEmailAddress, Instant aWhen,
|
||||||
|
ZoneId zoneId) {
|
||||||
|
this(aName, aEmailAddress, aWhen.toEpochMilli(),
|
||||||
|
TimeZone.getTimeZone(zoneId)
|
||||||
|
.getOffset(aWhen
|
||||||
|
.toEpochMilli()) / (60 * 1000));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy a PersonIdent, but alter the clone's time stamp
|
* Copy a PersonIdent, but alter the clone's time stamp
|
||||||
*
|
*
|
||||||
|
@ -303,6 +340,16 @@ public Date getWhen() {
|
||||||
return new Date(when);
|
return new Date(when);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get when attribute as instant
|
||||||
|
*
|
||||||
|
* @return timestamp
|
||||||
|
* @since 6.1
|
||||||
|
*/
|
||||||
|
public Instant getWhenAsInstant() {
|
||||||
|
return Instant.ofEpochMilli(when);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get this person's declared time zone
|
* Get this person's declared time zone
|
||||||
*
|
*
|
||||||
|
@ -312,6 +359,16 @@ public TimeZone getTimeZone() {
|
||||||
return getTimeZone(tzOffset);
|
return getTimeZone(tzOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the time zone id
|
||||||
|
*
|
||||||
|
* @return the time zone id
|
||||||
|
* @since 6.1
|
||||||
|
*/
|
||||||
|
public ZoneId getZoneId() {
|
||||||
|
return getTimeZone().toZoneId();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get this person's declared time zone as minutes east of UTC.
|
* Get this person's declared time zone as minutes east of UTC.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue