001package ca.uhn.fhir.rest.server;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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 java.util.LinkedHashMap;
024import java.util.UUID;
025
026import org.apache.commons.lang3.Validate;
027
028public class FifoMemoryPagingProvider implements IPagingProvider {
029
030        private LinkedHashMap<String, IBundleProvider> myBundleProviders;
031        private int myDefaultPageSize=10;
032        private int myMaximumPageSize=50;
033        private int mySize;
034
035        public FifoMemoryPagingProvider(int theSize) {
036                Validate.isTrue(theSize > 0, "theSize must be greater than 0");
037
038                mySize = theSize;
039                myBundleProviders = new LinkedHashMap<String, IBundleProvider>(mySize);
040        }
041
042        @Override
043        public int getDefaultPageSize() {
044                return myDefaultPageSize;
045        }
046
047        @Override
048        public int getMaximumPageSize() {
049                return myMaximumPageSize;
050        }
051
052        @Override
053        public synchronized IBundleProvider retrieveResultList(String theId) {
054                return myBundleProviders.get(theId);
055        }
056
057        public FifoMemoryPagingProvider setDefaultPageSize(int theDefaultPageSize) {
058                Validate.isTrue(theDefaultPageSize > 0, "size must be greater than 0");
059                myDefaultPageSize = theDefaultPageSize;
060                return this;
061        }
062
063        public FifoMemoryPagingProvider setMaximumPageSize(int theMaximumPageSize) {
064                Validate.isTrue(theMaximumPageSize > 0, "size must be greater than 0");
065                myMaximumPageSize = theMaximumPageSize;
066                return this;
067        }
068
069        @Override
070        public synchronized String storeResultList(IBundleProvider theList) {
071                while (myBundleProviders.size() > mySize) {
072                        myBundleProviders.remove(myBundleProviders.keySet().iterator().next());
073                }
074
075                String key = UUID.randomUUID().toString();
076                myBundleProviders.put(key, theList);
077                return key;
078        }
079
080}