forked from hapifhir/hapi-fhir
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hapifhir:master' into fix/remove-entity-embeddable-conf…
…lict
- Loading branch information
Showing
198 changed files
with
19,423 additions
and
672 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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
69 changes: 69 additions & 0 deletions
69
hapi-fhir-base/src/main/java/ca/uhn/fhir/util/StopLimitAccumulator.java
This file contains 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,69 @@ | ||
/*- | ||
* #%L | ||
* HAPI FHIR - Core Library | ||
* %% | ||
* Copyright (C) 2014 - 2024 Smile CDR, Inc. | ||
* %% | ||
* 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 | ||
* | ||
* http://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. | ||
* #L% | ||
*/ | ||
package ca.uhn.fhir.util; | ||
|
||
import jakarta.annotation.Nonnull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This class collects items from a stream to a given limit and know whether there are | ||
* still more items beyond that limit. | ||
* | ||
* @param <T> the type of object being streamed | ||
*/ | ||
public class StopLimitAccumulator<T> { | ||
private final boolean isTruncated; | ||
private final List<T> myList; | ||
|
||
private StopLimitAccumulator(List<T> theList, boolean theIsTruncated) { | ||
myList = Collections.unmodifiableList(theList); | ||
isTruncated = theIsTruncated; | ||
} | ||
|
||
public static <T> StopLimitAccumulator<T> fromStreamAndLimit(@Nonnull Stream<T> theItemStream, long theLimit) { | ||
assert theLimit > 0; | ||
AtomicBoolean isBeyondLimit = new AtomicBoolean(false); | ||
List<T> accumulator = new ArrayList<>(); | ||
|
||
theItemStream | ||
.limit(theLimit + 1) // Fetch one extra item to see if there are any more items past our limit | ||
.forEach(item -> { | ||
if (accumulator.size() < theLimit) { | ||
accumulator.add(item); | ||
} else { | ||
isBeyondLimit.set(true); | ||
} | ||
}); | ||
return new StopLimitAccumulator<>(accumulator, isBeyondLimit.get()); | ||
} | ||
|
||
public boolean isTruncated() { | ||
return isTruncated; | ||
} | ||
|
||
public List<T> getItemList() { | ||
return myList; | ||
} | ||
} |
Oops, something went wrong.