Fix potential IllegalStateException in Limit.
Fixes GH-3023.
This commit is contained in:
@@ -17,7 +17,7 @@ package org.springframework.data.domain;
|
|||||||
|
|
||||||
import org.springframework.data.domain.Limit.Limited;
|
import org.springframework.data.domain.Limit.Limited;
|
||||||
import org.springframework.data.domain.Limit.Unlimited;
|
import org.springframework.data.domain.Limit.Unlimited;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Limit} represents the maximum value up to which an operation should continue processing. It may be used for
|
* {@link Limit} represents the maximum value up to which an operation should continue processing. It may be used for
|
||||||
@@ -94,14 +94,17 @@ public sealed interface Limit permits Limited,Unlimited {
|
|||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!ClassUtils.isAssignable(Limit.class, obj.getClass())) {
|
|
||||||
|
if (!(obj instanceof Limit that)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Limit that = (Limit) obj;
|
|
||||||
if (this.isUnlimited() && that.isUnlimited()) {
|
if (this.isUnlimited() ^ that.isUnlimited()) {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
return max() == that.max();
|
|
||||||
|
return this.isUnlimited() && that.isUnlimited()
|
||||||
|
|| max() == that.max();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -55,4 +55,15 @@ class LimitUnitTests {
|
|||||||
void unlimitedErrorsOnMax() {
|
void unlimitedErrorsOnMax() {
|
||||||
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> Limit.unlimited().max());
|
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> Limit.unlimited().max());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // GH-3023
|
||||||
|
void equalsProperly() {
|
||||||
|
|
||||||
|
Limit unlimited = Limit.unlimited();
|
||||||
|
Limit limited = Limit.of(5);
|
||||||
|
|
||||||
|
assertThat(limited.equals(unlimited)).isFalse();
|
||||||
|
assertThat(unlimited.equals(limited)).isFalse();
|
||||||
|
assertThat(unlimited.equals(unlimited)).isTrue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user