001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.uif;
017
018import org.kuali.rice.krad.uif.view.View;
019
020import java.util.Stack;
021
022/**
023 * Holds preloaded view instances up to a configured size
024 *
025 * <p>
026 * The initial creation of the view object from Spring can be expensive in certain cases. To help with this, views
027 * can be preloaded with this pool class. When a request for a new view instance is made, a check will be done first
028 * to see if there is a pool and if so pull the already loaded view
029 * </p>
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 * @see UifDictionaryIndex#getViewById(java.lang.String)
033 */
034public class UifViewPool {
035   private int maxSize;
036   private Stack<View> views;
037
038   public UifViewPool() {
039       maxSize = 1;
040       views = new Stack<View>();
041   }
042
043   public UifViewPool(int maxSize) {
044       this.maxSize = maxSize;
045       views = new Stack<View>();
046   }
047
048   /**
049     * Maximum number of view instances the pool can hold
050     *
051     * <p>
052     * On initial startup of the application (during dictionary loading), view instances will be loaded and
053     * filled in a pool up to the max size configuration. The default is to preload one view, and each time
054     * the view is retrieved it is replaced. If a request is made before the view is replaced, the view is rebuilt
055     * from Spring. Therefore the performance gain is not present. For views with high concurrency, this property
056     * can be tweaked as needed. Please note larger pool sizes cost more in memory storage and application
057     * start up time
058     * </p>
059     *
060     * @return int max pool size
061     */
062   public int getMaxSize() {
063       return maxSize;
064   }
065
066   /**
067     * Setter for the pool max size
068     *
069     * @param maxSize
070     */
071   public void setMaxSize(int maxSize) {
072       this.maxSize = maxSize;
073   }
074
075   /**
076     * Adds a view instance to the pool
077     *
078     * @param view - view instance to add
079     */
080   public void addViewInstance(View view) {
081       views.push(view);
082   }
083
084   /**
085     * Retrieves a view instance from the pool and removes the instance
086     *
087     * @return View instance
088     */
089   public View getViewInstance() {
090       return views.pop();
091   }
092
093   /**
094     * Retrieves a view instance from the pool without removing it
095     *
096     * @return instance of a View
097     */
098   public View getViewSharedInstance() {
099       return views.peek();
100   }
101
102   /**
103     * Indicates whether the pool is full (number of view instances equals configured max size)
104     *
105     * @return boolean true if pool is full, else if not
106     */
107   public boolean isFull() {
108       return views.size() == maxSize;
109   }
110
111   /**
112     * Indicates whether the pool is empty (contains no view instances)
113     *
114     * <p>
115     * When the pool is empty, no view instances may be retrieved until the pool requires view instances.
116     * The calling code may choose to wait for a period of time and check again, or request a view instance from
117     * Spring
118     * </p>
119     *
120     * @return boolean true if the pool is empty, false if not
121     */
122   public boolean isEmpty() {
123       return (views == null) || (views.size() == 0);
124   }
125}