Skip to content

Commit

Permalink
Fix half year method names
Browse files Browse the repository at this point in the history
Also adjust test methods
See #200
  • Loading branch information
jodastephen committed Dec 3, 2023
1 parent 4525d74 commit f1de7fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/main/java/org/threeten/extra/YearHalf.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
* For most applications written today, the ISO-8601 rules are entirely suitable.
* However, any application that makes use of historical dates, and requires them
* to be accurate will find the ISO-8601 approach unsuitable.
* Note that the ISO-8601 standard does not define or refer to halfs.
* Note that the ISO-8601 standard does not define or refer to halves.
*
* <h3>Implementation Requirements:</h3>
* This class is immutable and thread-safe.
Expand Down Expand Up @@ -795,7 +795,7 @@ public YearHalf plus(TemporalAmount amountToAdd) {
* <ul>
* <li>{@code HALF_YEARS} -
* Returns a {@code YearHalf} with the specified number of halves added.
* This is equivalent to {@link #plusHalfs(long)}.
* This is equivalent to {@link #plusHalves(long)}.
* <li>{@code YEARS} -
* Returns a {@code YearHalf} with the specified number of years added.
* This is equivalent to {@link #plusYears(long)}.
Expand Down Expand Up @@ -837,7 +837,7 @@ public YearHalf plus(TemporalAmount amountToAdd) {
@Override
public YearHalf plus(long amountToAdd, TemporalUnit unit) {
if (unit == HALF_YEARS) {
return plusHalfs(amountToAdd);
return plusHalves(amountToAdd);
} else if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case YEARS:
Expand Down Expand Up @@ -883,14 +883,14 @@ public YearHalf plusYears(long yearsToAdd) {
* @return a {@code YearHalf} based on this year-half with the halves added, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public YearHalf plusHalfs(long halvesToAdd) {
public YearHalf plusHalves(long halvesToAdd) {
if (halvesToAdd == 0) {
return this;
}
long halfCount = year * 2L + (half.getValue() - 1);
long calcHalfs = halfCount + halvesToAdd; // safe overflow
int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcHalfs, 2));
int newHalf = (int) Math.floorMod(calcHalfs, 2L) + 1;
long calcHalves = halfCount + halvesToAdd; // safe overflow
int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcHalves, 2));
int newHalf = (int) Math.floorMod(calcHalves, 2L) + 1;
return with(newYear, Half.of(newHalf));
}

Expand Down Expand Up @@ -966,8 +966,8 @@ public YearHalf minusYears(long yearsToSubtract) {
* @return a {@code YearHalf} based on this year-half with the halves subtracted, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public YearHalf minusHalfs(long halvesToSubtract) {
return (halvesToSubtract == Long.MIN_VALUE ? plusHalfs(Long.MAX_VALUE).plusHalfs(1) : plusHalfs(-halvesToSubtract));
public YearHalf minusHalves(long halvesToSubtract) {
return (halvesToSubtract == Long.MIN_VALUE ? plusHalves(Long.MAX_VALUE).plusHalves(1) : plusHalves(-halvesToSubtract));
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -1123,7 +1123,7 @@ public Stream<YearHalf> halvesUntil(YearHalf endExclusive) {
throw new IllegalArgumentException(endExclusive + " < " + this);
}
long intervalLength = until(endExclusive, HALF_YEARS);
return LongStream.range(0, intervalLength).mapToObj(n -> plusHalfs(n));
return LongStream.range(0, intervalLength).mapToObj(n -> plusHalves(n));
}

/**
Expand Down
29 changes: 16 additions & 13 deletions src/test/java/org/threeten/extra/TestYearHalf.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import static java.time.temporal.IsoFields.QUARTER_OF_YEAR;
import static java.time.temporal.IsoFields.QUARTER_YEARS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.threeten.extra.Half.H1;
Expand Down Expand Up @@ -643,13 +644,13 @@ public void test_plusYears() {
}

//-----------------------------------------------------------------------
// plusHalfs(int)
// plusHalves(int)
//-----------------------------------------------------------------------
@Test
public void test_plusHalfs() {
assertEquals(YearHalf.of(2010, H1), YearHalf.of(2007, H2).plusHalfs(5));
assertEquals(YearHalf.of(2007, H2), YearHalf.of(2007, H2).plusHalfs(0));
assertEquals(YearHalf.of(2005, H1), YearHalf.of(2007, H2).plusHalfs(-5));
public void test_plusHalves() {
assertEquals(YearHalf.of(2010, H1), YearHalf.of(2007, H2).plusHalves(5));
assertEquals(YearHalf.of(2007, H2), YearHalf.of(2007, H2).plusHalves(0));
assertEquals(YearHalf.of(2005, H1), YearHalf.of(2007, H2).plusHalves(-5));
}

//-----------------------------------------------------------------------
Expand All @@ -676,13 +677,13 @@ public void test_minusYears() {
}

//-----------------------------------------------------------------------
// minusHalfs(int)
// minusHalves(int)
//-----------------------------------------------------------------------
@Test
public void test_minusHalfs() {
assertEquals(YearHalf.of(2005, H1), YearHalf.of(2007, H2).minusHalfs(5));
assertEquals(YearHalf.of(2007, H2), YearHalf.of(2007, H2).minusHalfs(0));
assertEquals(YearHalf.of(2010, H1), YearHalf.of(2007, H2).minusHalfs(-5));
public void test_minusHalves() {
assertEquals(YearHalf.of(2005, H1), YearHalf.of(2007, H2).minusHalves(5));
assertEquals(YearHalf.of(2007, H2), YearHalf.of(2007, H2).minusHalves(0));
assertEquals(YearHalf.of(2010, H1), YearHalf.of(2007, H2).minusHalves(-5));
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -953,8 +954,10 @@ public void test_equals() {
for (Half half2 : Half.values()) {
YearHalf b = YearHalf.of(year2, half2);
if (year1 == year2 && half1 == half2) {
assertEquals(a, b);
assertTrue(a.equals(b));
assertEquals(a.hashCode(), b.hashCode());
} else {
assertFalse(a.equals(b));
}
}
}
Expand All @@ -964,13 +967,13 @@ public void test_equals() {

@Test
public void test_equals_nullYearHalf() {
assertEquals(false, TEST.equals(null));
assertFalse(TEST.equals(null));
}

@Test
public void test_equals_incorrectType() {
Object obj = "Incorrect type";
assertEquals(false, TEST.equals(obj));
assertFalse(TEST.equals(obj));
}

//-----------------------------------------------------------------------
Expand Down

0 comments on commit f1de7fa

Please sign in to comment.