-
Notifications
You must be signed in to change notification settings - Fork 682
Introduce Limit
type
#2836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Introduce Limit
type
#2836
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d33ead0
Prepare issue branch.
christophstrobl 0318d95
Introduce Limit.
christophstrobl 3a27f3d
Detect Limit Parameter in QueryMethod
christophstrobl 1cbbf30
QueryMethod restrictions
christophstrobl 2cd0c87
Use Limit/Sort parameters to fill pageable parameter.
christophstrobl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/org/springframework/data/domain/Limit.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.domain; | ||
|
||
import org.springframework.data.domain.Limit.Limited; | ||
import org.springframework.data.domain.Limit.Unlimited; | ||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* {@link Limit} represents the maximum value up to which an operation should continue processing. It may be used for | ||
* defining the {@link #max() maximum} number of results within a repository finder method or if applicable a template | ||
* operation. | ||
* <p> | ||
* A {@link Limit#isUnlimited()} is used to indicate that there is no {@link Limit} defined, which should be favoured | ||
* over using {@literal null} or {@link java.util.Optional#empty()} to indicate the absence of an actual {@link Limit}. | ||
* </p> | ||
* {@link Limit} itself does not make assumptions about the actual {@link #max()} value sign. This means that a negative | ||
* value may be valid within a defined context. Implementations should override {@link #isUnlimited()} to fit their | ||
* needs and enforce restrictions if needed. | ||
* | ||
* @author Christoph Strobl | ||
* @since 3.2 | ||
*/ | ||
public sealed interface Limit permits Limited, Unlimited { | ||
|
||
/** | ||
* @return the max number of potential results. | ||
*/ | ||
int max(); | ||
|
||
/** | ||
* @return {@literal true} if no limiting (maximum value) should be applied. | ||
*/ | ||
default boolean isUnlimited() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also have a |
||
return Unlimited.INSTANCE.equals(this); | ||
} | ||
|
||
/** | ||
* @return a {@link Limit} instance that does not define {@link #max()} and answers {@link #isUnlimited()} with | ||
* {@literal true}. | ||
*/ | ||
static Limit unlimited() { | ||
return Unlimited.INSTANCE; | ||
} | ||
|
||
/** | ||
* Create a new {@link Limit} from the given {@literal max} value. | ||
* | ||
* @param max the maximum value. | ||
* @return new instance of {@link Limit}. | ||
*/ | ||
static Limit of(int max) { | ||
return new Limited(max); | ||
} | ||
|
||
final class Limited implements Limit { | ||
|
||
private final int max; | ||
|
||
Limited(int max) { | ||
this.max = max; | ||
} | ||
|
||
@Override | ||
public int max() { | ||
return max; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
|
||
if (obj == null) { | ||
return false; | ||
} | ||
if (!ClassUtils.isAssignable(Limit.class, obj.getClass())) { | ||
return false; | ||
} | ||
Limit that = (Limit) obj; | ||
if (this.isUnlimited() && that.isUnlimited()) { | ||
return true; | ||
} | ||
return max() == that.max(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return (int) (max ^ (max >>> 32)); | ||
} | ||
} | ||
|
||
final class Unlimited implements Limit { | ||
|
||
static final Limit INSTANCE = new Unlimited(); | ||
|
||
Unlimited() {} | ||
|
||
@Override | ||
public int max() { | ||
throw new IllegalStateException( | ||
"Unlimited does not define 'max'. Please check 'isUnlimited' before attempting to read 'max'"); | ||
} | ||
|
||
@Override | ||
public boolean isUnlimited() { | ||
return true; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
* | ||
* @author Oliver Gierke | ||
* @author Mark Paluch | ||
* @author Christoph Strobl | ||
*/ | ||
public interface Pageable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a |
||
|
||
|
@@ -33,7 +34,18 @@ public interface Pageable { | |
* @return | ||
*/ | ||
static Pageable unpaged() { | ||
return Unpaged.INSTANCE; | ||
return unpaged(Sort.unsorted()); | ||
} | ||
|
||
/** | ||
* Returns a {@link Pageable} instance representing no pagination setup having a defined result {@link Sort order}. | ||
* | ||
* @param sort must not be {@literal null}, use {@link Sort#unsorted()} if needed. | ||
* @return never {@literal null}. | ||
* @since 3.2 | ||
*/ | ||
static Pageable unpaged(Sort sort) { | ||
return Unpaged.sorted(sort); | ||
} | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double "this this"?