001package ca.uhn.fhir.rest.api.server;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2021 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 com.google.common.collect.Lists;
024import org.apache.commons.lang3.Validate;
025import org.hl7.fhir.instance.model.api.IBaseResource;
026
027import java.util.Arrays;
028import java.util.Collection;
029import java.util.Iterator;
030import java.util.List;
031
032public class SimplePreResourceShowDetails implements IPreResourceShowDetails {
033
034        private static final IBaseResource[] EMPTY_RESOURCE_ARRAY = new IBaseResource[0];
035
036        private final IBaseResource[] myResources;
037        private final boolean[] myResourceMarkedAsSubset;
038
039        /**
040         * Constructor for a single resource
041         */
042        public SimplePreResourceShowDetails(IBaseResource theResource) {
043                this(Lists.newArrayList(theResource));
044        }
045
046        /**
047         * Constructor for a collection of resources
048         */
049        public <T extends IBaseResource> SimplePreResourceShowDetails(Collection<T> theResources) {
050                myResources = theResources.toArray(EMPTY_RESOURCE_ARRAY);
051                myResourceMarkedAsSubset = new boolean[myResources.length];
052        }
053
054        @Override
055        public int size() {
056                return myResources.length;
057        }
058
059        @Override
060        public IBaseResource getResource(int theIndex) {
061                return myResources[theIndex];
062        }
063
064        @Override
065        public void setResource(int theIndex, IBaseResource theResource) {
066                Validate.isTrue(theIndex >= 0, "Invalid index %d - theIndex must not be < 0", theIndex);
067                Validate.isTrue(theIndex < myResources.length, "Invalid index {} - theIndex must be < %d", theIndex, myResources.length);
068                myResources[theIndex] = theResource;
069        }
070
071        @Override
072        public void markResourceAtIndexAsSubset(int theIndex) {
073                Validate.isTrue(theIndex >= 0, "Invalid index %d - theIndex must not be < 0", theIndex);
074                Validate.isTrue(theIndex < myResources.length, "Invalid index {} - theIndex must be < %d", theIndex, myResources.length);
075                myResourceMarkedAsSubset[theIndex] = true;
076        }
077
078        @Override
079        public Iterator<IBaseResource> iterator() {
080                return Arrays.asList(myResources).iterator();
081        }
082
083        public List<IBaseResource> toList() {
084                return Lists.newArrayList(myResources);
085        }
086}