1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.myfaces.view.facelets.pool.impl; 20 21 import java.util.Queue; 22 import java.util.concurrent.ConcurrentLinkedQueue; 23 import java.util.concurrent.atomic.AtomicInteger; 24 import org.apache.myfaces.view.facelets.pool.ViewEntry; 25 26 /** 27 * Fast pool using ConcurrentLinkedQueue, with uses an AtomicInteger as 28 * count limit. The reasons of design this pool in this way are: 29 * 30 * <ol> 31 * <li>There is no need to put a hard limit about the max number of views stored 32 * in the pool. Remember ViewEntry internally has a Soft or Weak reference over 33 * the view. The maxCount is just a way to limit the max footprint fo the pool 34 * in memory, but if the limit is exceed, the vm can always reclaim the memory space.</li> 35 * <li>View creation is quite fast, so according to previous tests done, 36 * include any syncronized method in this code will produce worse performance.</li> 37 * </ol> 38 * 39 * @author Leonardo Uribe 40 */ 41 public class ViewPoolEntryHolder 42 { 43 private Queue<ViewEntry> queue; 44 45 private AtomicInteger count; 46 47 private int maxCount; 48 49 public ViewPoolEntryHolder(int maxCount) 50 { 51 this.queue = new ConcurrentLinkedQueue<ViewEntry>(); 52 this.count = new AtomicInteger(); 53 this.maxCount = maxCount; 54 } 55 56 public boolean add(ViewEntry entry) 57 { 58 if (count.get() < maxCount) 59 { 60 queue.add(entry); 61 count.incrementAndGet(); 62 return true; 63 } 64 return false; 65 } 66 67 public ViewEntry poll() 68 { 69 ViewEntry entry = queue.poll(); 70 count.decrementAndGet(); 71 return entry; 72 } 73 74 public boolean isFull() 75 { 76 return count.get() >= maxCount; 77 } 78 79 public int getCount() 80 { 81 return count.get(); 82 } 83 }