001package ca.uhn.fhir.rest.api.server;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
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.List;
028
029public class SimplePreResourceShowDetails implements IPreResourceShowDetails {
030
031        private final List<IBaseResource> myResources;
032        private final boolean[] mySubSets;
033
034        public SimplePreResourceShowDetails(IBaseResource theResource) {
035                this(Lists.newArrayList(theResource));
036        }
037
038        public <T extends IBaseResource> SimplePreResourceShowDetails(List<T> theResources) {
039                //noinspection unchecked
040                myResources = (List<IBaseResource>) theResources;
041                mySubSets = new boolean[myResources.size()];
042        }
043
044        @Override
045        public int size() {
046                return myResources.size();
047        }
048
049        @Override
050        public IBaseResource getResource(int theIndex) {
051                return myResources.get(theIndex);
052        }
053
054        @Override
055        public void setResource(int theIndex, IBaseResource theResource) {
056                Validate.isTrue(theIndex >= 0, "Invalid index %d - theIndex must not be < 0", theIndex);
057                Validate.isTrue(theIndex < myResources.size(), "Invalid index {} - theIndex must be < %d", theIndex, myResources.size());
058                myResources.set(theIndex, theResource);
059        }
060
061        @Override
062        public void markResourceAtIndexAsSubset(int theIndex) {
063                Validate.isTrue(theIndex >= 0, "Invalid index %d - theIndex must not be < 0", theIndex);
064                Validate.isTrue(theIndex < myResources.size(), "Invalid index {} - theIndex must be < %d", theIndex, myResources.size());
065                mySubSets[theIndex] = true;
066        }
067}