1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 677240 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-07-16 04:25:47 -0700 (Wed, 16 Jul 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  this work for additional information regarding copyright ownership.
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  (the "License"); you may not use this file except in compliance with
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  See the License for the specific language governing permissions and
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  limitations under the License.
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.conn.tsccm;
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException;
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.ListIterator;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Queue;
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.LinkedList;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.logging.Log;
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.logging.LogFactory;
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.OperatedClientConnection;
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.routing.HttpRoute;
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.util.LangUtils;
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * A connection sub-pool for a specific route, used by {@link ConnPoolByRoute}.
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The methods in this class are unsynchronized. It is expected that the
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * containing pool takes care of synchronization.
49d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *
50d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead.
51d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
52d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     for further details.
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
54d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath@Deprecated
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class RouteSpecificPool {
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final Log log = LogFactory.getLog(getClass());
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The route this pool is for. */
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final HttpRoute route;
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** the maximum number of entries allowed for this pool */
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final int maxEntries;
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The list of free entries.
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This list is managed LIFO, to increase idle times and
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * allow for closing connections that are not really needed.
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final LinkedList<BasicPoolEntry> freeEntries;
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The list of threads waiting for this pool. */
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final Queue<WaitingThread> waitingThreads;
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The number of created entries. */
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected int numEntries;
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new route-specific pool.
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param route the route for which to pool
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param maxEntries the maximum number of entries allowed for this pool
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public RouteSpecificPool(HttpRoute route, int maxEntries) {
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.route = route;
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.maxEntries = maxEntries;
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.freeEntries = new LinkedList<BasicPoolEntry>();
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.waitingThreads = new LinkedList<WaitingThread>();
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.numEntries = 0;
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Obtains the route for which this pool is specific.
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  the route
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public final HttpRoute getRoute() {
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return route;
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Obtains the maximum number of entries allowed for this pool.
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  the max entry number
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public final int getMaxEntries() {
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return maxEntries;
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Indicates whether this pool is unused.
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * A pool is unused if there is neither an entry nor a waiting thread.
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * All entries count, not only the free but also the allocated ones.
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  <code>true</code> if this pool is unused,
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          <code>false</code> otherwise
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean isUnused() {
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return (numEntries < 1) && waitingThreads.isEmpty();
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Return remaining capacity of this pool
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return capacity
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int getCapacity() {
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return maxEntries - numEntries;
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Obtains the number of entries.
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This includes not only the free entries, but also those that
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * have been created and are currently issued to an application.
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  the number of entries for the route of this pool
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public final int getEntryCount() {
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return numEntries;
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Obtains a free entry from this pool, if one is available.
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return an available pool entry, or <code>null</code> if there is none
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public BasicPoolEntry allocEntry(final Object state) {
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!freeEntries.isEmpty()) {
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            ListIterator<BasicPoolEntry> it = freeEntries.listIterator(freeEntries.size());
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            while (it.hasPrevious()) {
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                BasicPoolEntry entry = it.previous();
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (LangUtils.equals(state, entry.getState())) {
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    it.remove();
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    return entry;
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!freeEntries.isEmpty()) {
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            BasicPoolEntry entry = freeEntries.remove();
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            entry.setState(null);
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            OperatedClientConnection conn = entry.getConnection();
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                conn.close();
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                log.debug("I/O error closing connection", ex);
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return entry;
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return null;
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns an allocated entry to this pool.
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param entry     the entry obtained from {@link #allocEntry allocEntry}
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  or presented to {@link #createdEntry createdEntry}
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void freeEntry(BasicPoolEntry entry) {
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (numEntries < 1) {
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("No entry created for this pool. " + route);
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (numEntries <= freeEntries.size()) {
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("No entry allocated from this pool. " + route);
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        freeEntries.add(entry);
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Indicates creation of an entry for this pool.
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The entry will <i>not</i> be added to the list of free entries,
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * it is only recognized as belonging to this pool now. It can then
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * be passed to {@link #freeEntry freeEntry}.
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param entry     the entry that was created for this pool
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void createdEntry(BasicPoolEntry entry) {
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!route.equals(entry.getPlannedRoute())) {
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Entry not planned for this pool." +
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                 "\npool: " + route +
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                 "\nplan: " + entry.getPlannedRoute());
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        numEntries++;
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Deletes an entry from this pool.
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Only entries that are currently free in this pool can be deleted.
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Allocated entries can not be deleted.
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param entry     the entry to delete from this pool
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  <code>true</code> if the entry was found and deleted, or
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          <code>false</code> if the entry was not found
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean deleteEntry(BasicPoolEntry entry) {
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final boolean found = freeEntries.remove(entry);
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (found)
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            numEntries--;
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return found;
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Forgets about an entry from this pool.
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This method is used to indicate that an entry
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * {@link #allocEntry allocated}
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * from this pool has been lost and will not be returned.
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void dropEntry() {
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (numEntries < 1) {
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("There is no entry that could be dropped.");
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        numEntries--;
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Adds a waiting thread.
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This pool makes no attempt to match waiting threads with pool entries.
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * It is the caller's responsibility to check that there is no entry
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * before adding a waiting thread.
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param wt        the waiting thread
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void queueThread(WaitingThread wt) {
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (wt == null) {
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Waiting thread must not be null.");
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.waitingThreads.add(wt);
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Checks whether there is a waiting thread in this pool.
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  <code>true</code> if there is a waiting thread,
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          <code>false</code> otherwise
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean hasThread() {
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return !this.waitingThreads.isEmpty();
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the next thread in the queue.
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  a waiting thread, or <code>null</code> if there is none
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public WaitingThread nextThread() {
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.waitingThreads.peek();
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
291069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
292069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
293069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
294069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Removes a waiting thread, if it is queued.
295069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
296069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param wt        the waiting thread
297069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
298069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void removeThread(WaitingThread wt) {
299069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (wt == null)
300069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return;
301069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
302069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.waitingThreads.remove(wt);
303069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
304069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
305069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
306069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} // class RouteSpecificPool
307