001    package org.maltparser.core.pool;
002    
003    import java.util.ArrayList;
004    
005    import org.maltparser.core.exception.MaltChainedException;
006    
007    public abstract class ObjectPoolList<T> extends ObjectPool<T> {
008            private final ArrayList<T> objectList;
009            private int currentSize;
010            
011            public ObjectPoolList() {
012                    this(Integer.MAX_VALUE);
013            }
014            
015            public ObjectPoolList(int keepThreshold) {
016                    super(keepThreshold);
017                    objectList = new ArrayList<T>();
018            }
019    
020            protected abstract T create() throws MaltChainedException;
021            public abstract void resetObject(T o) throws MaltChainedException;
022            
023            public synchronized T checkOut() throws MaltChainedException {
024                    T t = null;
025                    if (currentSize >= objectList.size()) {
026                            t = create();
027                            objectList.add(t);
028                            currentSize++;  
029                    } else {
030                            t = objectList.get(currentSize);
031                            currentSize++;
032                    }
033                    return t;
034            }
035            
036            public synchronized void checkIn(T o) throws MaltChainedException {
037                    resetObject(o);
038            }
039            
040            public synchronized void checkInAll() throws MaltChainedException {
041                    for (int i = currentSize-1; i >= 0 && i < objectList.size(); i--) {
042                            resetObject(objectList.get(i));
043                            if (currentSize >= keepThreshold) {
044                                    objectList.remove(i);
045                            }
046                    }
047                    currentSize = 0;
048            }
049    
050            public int getCurrentSize() {
051                    return currentSize;
052            }
053    
054            public void setCurrentSize(int currentSize) {
055                    this.currentSize = currentSize;
056            }
057            
058            public int size() {
059                    return objectList.size();
060            }
061            
062            public String toString() {
063                    final StringBuilder sb = new StringBuilder();
064                    for (int i = 0; i < currentSize; i++) {
065                            sb.append(objectList.get(i));
066                            sb.append(", ");
067                    }
068                    return sb.toString();
069            }
070    }