[#8233] YearToSecond should be equal to YearToMonth or DayToSecond, if same interval is represented

This commit is contained in:
lukaseder 2019-01-21 11:58:46 +01:00
parent 126c8b496e
commit 2771a378f7
3 changed files with 95 additions and 50 deletions

View File

@ -96,7 +96,14 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
private final int nano;
/**
* Create a new day-hour interval.
* Create a new interval.
*/
public DayToSecond() {
this(0, 0, 0, 0, 0, false);
}
/**
* Create a new day interval.
*/
public DayToSecond(int days) {
this(days, 0, 0, 0, 0, false);
@ -110,21 +117,21 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
}
/**
* Create a new day-hour interval.
* Create a new day-minute interval.
*/
public DayToSecond(int days, int hours, int minutes) {
this(days, hours, minutes, 0, 0, false);
}
/**
* Create a new day-hour interval.
* Create a new day-second interval.
*/
public DayToSecond(int days, int hours, int minutes, int seconds) {
this(days, hours, minutes, seconds, 0, false);
}
/**
* Create a new day-hour interval.
* Create a new day-nanoseconds interval.
*/
public DayToSecond(int days, int hours, int minutes, int seconds, int nano) {
this(days, hours, minutes, seconds, nano, false);
@ -494,12 +501,17 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
@Override
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + days;
result = prime * result + hours;
result = prime * result + minutes;
result = prime * result + nano;
result = prime * result + seconds;
int result = 0;
if (days != 0)
result = prime * result + days;
if (hours != 0)
result = prime * result + hours;
if (minutes != 0)
result = prime * result + minutes;
if (nano != 0)
result = prime * result + nano;
if (seconds != 0)
result = prime * result + seconds;
return result;
}
@ -509,20 +521,24 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
if (getClass() == obj.getClass()) {
DayToSecond other = (DayToSecond) obj;
if (days != other.days)
return false;
if (hours != other.hours)
return false;
if (minutes != other.minutes)
return false;
if (nano != other.nano)
return false;
if (seconds != other.seconds)
return false;
return true;
}
else if (obj instanceof YearToSecond)
return obj.equals(this);
else
return false;
DayToSecond other = (DayToSecond) obj;
if (days != other.days)
return false;
if (hours != other.hours)
return false;
if (minutes != other.minutes)
return false;
if (nano != other.nano)
return false;
if (seconds != other.seconds)
return false;
return true;
}
@Override

View File

@ -87,6 +87,13 @@ public final class YearToMonth extends Number implements Interval, Comparable<Ye
private final int years;
private final int months;
/**
* Create a new year-month interval.
*/
public YearToMonth() {
this(0, 0, false);
}
/**
* Create a new year-month interval.
*/
@ -237,9 +244,11 @@ public final class YearToMonth extends Number implements Interval, Comparable<Ye
@Override
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + months;
result = prime * result + years;
int result = 0;
if (months != 0)
result = prime * result + months;
if (years != 0)
result = prime * result + years;
return result;
}
@ -249,14 +258,18 @@ public final class YearToMonth extends Number implements Interval, Comparable<Ye
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
if (getClass() == obj.getClass()) {
YearToMonth other = (YearToMonth) obj;
if (months != other.months)
return false;
if (years != other.years)
return false;
return true;
}
else if (obj instanceof YearToSecond)
return obj.equals(this);
else
return false;
YearToMonth other = (YearToMonth) obj;
if (months != other.months)
return false;
if (years != other.years)
return false;
return true;
}
@Override

View File

@ -90,8 +90,8 @@ public final class YearToSecond extends Number implements Interval, Comparable<Y
private final DayToSecond dayToSecond;
public YearToSecond(YearToMonth yearToMonth, DayToSecond dayToSecond) {
this.yearToMonth = yearToMonth == null ? new YearToMonth(0) : yearToMonth;
this.dayToSecond = dayToSecond == null ? new DayToSecond(0) : dayToSecond;
this.yearToMonth = yearToMonth == null ? new YearToMonth() : yearToMonth;
this.dayToSecond = dayToSecond == null ? new DayToSecond() : dayToSecond;
}
/**
@ -334,9 +334,13 @@ public final class YearToSecond extends Number implements Interval, Comparable<Y
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dayToSecond == null) ? 0 : dayToSecond.hashCode());
result = prime * result + ((yearToMonth == null) ? 0 : yearToMonth.hashCode());
int result = 0;
int h1 = dayToSecond.hashCode();
int h2 = yearToMonth.hashCode();
if (h1 != 0)
result = prime * result + h1;
if (h2 != 0)
result = prime * result + h2;
return result;
}
@ -346,22 +350,34 @@ public final class YearToSecond extends Number implements Interval, Comparable<Y
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
YearToSecond other = (YearToSecond) obj;
if (dayToSecond == null) {
if (other.dayToSecond != null)
if (getClass() == obj.getClass()) {
YearToSecond other = (YearToSecond) obj;
if (dayToSecond == null) {
if (other.dayToSecond != null)
return false;
}
else if (!dayToSecond.equals(other.dayToSecond))
return false;
}
else if (!dayToSecond.equals(other.dayToSecond))
return false;
if (yearToMonth == null) {
if (other.yearToMonth != null)
if (yearToMonth == null) {
if (other.yearToMonth != null)
return false;
}
else if (!yearToMonth.equals(other.yearToMonth))
return false;
return true;
}
else if (!yearToMonth.equals(other.yearToMonth))
else if (obj instanceof YearToMonth) {
YearToMonth other = (YearToMonth) obj;
return getDayToSecond().intValue() == 0 &&
getYearToMonth().equals(other);
}
else if (obj instanceof DayToSecond) {
DayToSecond other = (DayToSecond) obj;
return getYearToMonth().intValue() == 0 &&
getDayToSecond().equals(other);
}
else
return false;
return true;
}
@Override