001package ca.uhn.fhir.rest.api.server;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.hl7.fhir.instance.model.api.IBaseResource;
024
025import java.util.Collections;
026import java.util.List;
027
028public class SimplePreResourceAccessDetails implements IPreResourceAccessDetails {
029
030        private final List<IBaseResource> myResources;
031        private final boolean[] myBlocked;
032
033        public SimplePreResourceAccessDetails(IBaseResource theResource) {
034                this(Collections.singletonList(theResource));
035        }
036
037        public <T extends IBaseResource> SimplePreResourceAccessDetails(List<T> theResources) {
038                //noinspection unchecked
039                myResources = (List<IBaseResource>) theResources;
040                myBlocked = new boolean[myResources.size()];
041        }
042
043        @Override
044        public int size() {
045                return myResources.size();
046        }
047
048        @Override
049        public IBaseResource getResource(int theIndex) {
050                return myResources.get(theIndex);
051        }
052
053        @Override
054        public void setDontReturnResourceAtIndex(int theIndex) {
055                myBlocked[theIndex] = true;
056        }
057
058        public boolean isDontReturnResourceAtIndex(int theIndex) {
059                return myBlocked[theIndex];
060        }
061
062        /**
063         * Remove any blocked resources from the list that was passed into the constructor
064         */
065        public void applyFilterToList() {
066                for (int i = size() - 1; i >= 0; i--) {
067                        if (isDontReturnResourceAtIndex(i)) {
068                                myResources.remove(i);
069                        }
070                }
071        }
072}