Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
张胜利
/
Androidgroup
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
550e1a79
authored
Feb 09, 2015
by
张胜利
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
再来初始化一次
parent
9ab372c0
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
2639 additions
and
0 deletions
imageLoader/src/com/nostra13/universalimageloader/core/assist/ImageScaleType.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/ImageSize.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/LoadedFrom.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/QueueProcessingType.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/ViewScaleType.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/BlockingDeque.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/Deque.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/LIFOLinkedBlockingDeque.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/LinkedBlockingDeque.java
imageLoader/src/com/nostra13/universalimageloader/core/assist/ImageScaleType.java
0 → 100644
View file @
550e1a79
/*******************************************************************************
* Copyright 2011-2014 Sergey Tarasevich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
;
/**
* Type of image scaling during decoding.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @since 1.5.0
*/
public
enum
ImageScaleType
{
/** Image won't be scaled */
NONE
,
/**
* Image will be scaled down only if image size is greater than
* {@linkplain javax.microedition.khronos.opengles.GL10#GL_MAX_TEXTURE_SIZE maximum acceptable texture size}.
* Usually it's 2048x2048.<br />
* If Bitmap is expected to display than it must not exceed this size (otherwise you'll get the exception
* "OpenGLRenderer: Bitmap too large to be uploaded into a texture".<br />
* Image will be subsampled in an integer number of times (1, 2, 3, ...) to maximum texture size of device.
*/
NONE_SAFE
,
/**
* Image will be reduces 2-fold until next reduce step make image smaller target size.<br />
* It's <b>fast</b> type and it's preferable for usage in lists/grids/galleries (and other
* {@linkplain android.widget.AdapterView adapter-views}) .<br />
* Relates to {@link android.graphics.BitmapFactory.Options#inSampleSize}<br />
* Note: If original image size is smaller than target size then original image <b>won't</b> be scaled.
*/
IN_SAMPLE_POWER_OF_2
,
/**
* Image will be subsampled in an integer number of times (1, 2, 3, ...). Use it if memory economy is quite
* important.<br />
* Relates to {@link android.graphics.BitmapFactory.Options#inSampleSize}<br />
* Note: If original image size is smaller than target size then original image <b>won't</b> be scaled.
*/
IN_SAMPLE_INT
,
/**
* Image will scaled-down exactly to target size (scaled width or height or both will be equal to target size;
* depends on {@linkplain android.widget.ImageView.ScaleType ImageView's scale type}). Use it if memory economy is
* critically important.<br />
* <b>Note:</b> If original image size is smaller than target size then original image <b>won't</b> be scaled.<br />
* <br />
* <b>NOTE:</b> For creating result Bitmap (of exact size) additional Bitmap will be created with
* {@link android.graphics.Bitmap#createBitmap(android.graphics.Bitmap, int, int, int, int, android.graphics.Matrix, boolean)
* Bitmap.createBitmap(...)}.<br />
* <b>Cons:</b> Saves memory by keeping smaller Bitmap in memory cache (comparing with IN_SAMPLE... scale types)<br />
* <b>Pros:</b> Requires more memory in one time for creation of result Bitmap.
*/
EXACTLY
,
/**
* Image will scaled exactly to target size (scaled width or height or both will be equal to target size; depends on
* {@linkplain android.widget.ImageView.ScaleType ImageView's scale type}). Use it if memory economy is critically
* important.<br />
* <b>Note:</b> If original image size is smaller than target size then original image <b>will be stretched</b> to
* target size.<br />
* <br />
* <b>NOTE:</b> For creating result Bitmap (of exact size) additional Bitmap will be created with
* {@link android.graphics.Bitmap#createBitmap(android.graphics.Bitmap, int, int, int, int, android.graphics.Matrix, boolean)
* Bitmap.createBitmap(...)}.<br />
* <b>Cons:</b> Saves memory by keeping smaller Bitmap in memory cache (comparing with IN_SAMPLE... scale types)<br />
* <b>Pros:</b> Requires more memory in one time for creation of result Bitmap.
*/
EXACTLY_STRETCHED
}
imageLoader/src/com/nostra13/universalimageloader/core/assist/ImageSize.java
0 → 100644
View file @
550e1a79
/*******************************************************************************
* Copyright 2011-2013 Sergey Tarasevich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
;
/**
* Present width and height values
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @since 1.0.0
*/
public
class
ImageSize
{
private
static
final
int
TO_STRING_MAX_LENGHT
=
9
;
// "9999x9999".length()
private
static
final
String
SEPARATOR
=
"x"
;
private
final
int
width
;
private
final
int
height
;
public
ImageSize
(
int
width
,
int
height
)
{
this
.
width
=
width
;
this
.
height
=
height
;
}
public
ImageSize
(
int
width
,
int
height
,
int
rotation
)
{
if
(
rotation
%
180
==
0
)
{
this
.
width
=
width
;
this
.
height
=
height
;
}
else
{
this
.
width
=
height
;
this
.
height
=
width
;
}
}
public
int
getWidth
()
{
return
width
;
}
public
int
getHeight
()
{
return
height
;
}
/** Scales down dimensions in <b>sampleSize</b> times. Returns new object. */
public
ImageSize
scaleDown
(
int
sampleSize
)
{
return
new
ImageSize
(
width
/
sampleSize
,
height
/
sampleSize
);
}
/** Scales dimensions according to incoming scale. Returns new object. */
public
ImageSize
scale
(
float
scale
)
{
return
new
ImageSize
((
int
)
(
width
*
scale
),
(
int
)
(
height
*
scale
));
}
@Override
public
String
toString
()
{
return
new
StringBuilder
(
TO_STRING_MAX_LENGHT
).
append
(
width
).
append
(
SEPARATOR
).
append
(
height
).
toString
();
}
}
imageLoader/src/com/nostra13/universalimageloader/core/assist/LoadedFrom.java
0 → 100644
View file @
550e1a79
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
;
/**
* Source image loaded from.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public
enum
LoadedFrom
{
NETWORK
,
DISC_CACHE
,
MEMORY_CACHE
}
imageLoader/src/com/nostra13/universalimageloader/core/assist/QueueProcessingType.java
0 → 100644
View file @
550e1a79
/*******************************************************************************
* Copyright 2011-2013 Sergey Tarasevich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
;
/**
* Queue processing type which will be used for display task processing
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @since 1.6.3
*/
public
enum
QueueProcessingType
{
FIFO
,
LIFO
}
imageLoader/src/com/nostra13/universalimageloader/core/assist/ViewScaleType.java
0 → 100644
View file @
550e1a79
/*******************************************************************************
* Copyright 2011-2013 Sergey Tarasevich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
;
import
android.widget.ImageView
;
import
android.widget.ImageView.ScaleType
;
/**
* Simplify {@linkplain ScaleType ImageView's scale type} to 2 types: {@link #FIT_INSIDE} and {@link #CROP}
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @since 1.6.1
*/
public
enum
ViewScaleType
{
/**
* Scale the image uniformly (maintain the image's aspect ratio) so that at least one dimension (width or height) of
* the image will be equal to or less the corresponding dimension of the view.
*/
FIT_INSIDE
,
/**
* Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the
* image will be equal to or larger than the corresponding dimension of the view.
*/
CROP
;
/**
* Defines scale type of ImageView.
*
* @param imageView {@link ImageView}
* @return {@link #FIT_INSIDE} for
* <ul>
* <li>{@link ScaleType#FIT_CENTER}</li>
* <li>{@link ScaleType#FIT_XY}</li>
* <li>{@link ScaleType#FIT_START}</li>
* <li>{@link ScaleType#FIT_END}</li>
* <li>{@link ScaleType#CENTER_INSIDE}</li>
* </ul>
* {@link #CROP} for
* <ul>
* <li>{@link ScaleType#CENTER}</li>
* <li>{@link ScaleType#CENTER_CROP}</li>
* <li>{@link ScaleType#MATRIX}</li>
* </ul>
*/
public
static
ViewScaleType
fromImageView
(
ImageView
imageView
)
{
switch
(
imageView
.
getScaleType
())
{
case
FIT_CENTER:
case
FIT_XY:
case
FIT_START:
case
FIT_END:
case
CENTER_INSIDE:
return
FIT_INSIDE
;
case
MATRIX:
case
CENTER:
case
CENTER_CROP:
default
:
return
CROP
;
}
}
}
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/BlockingDeque.java
0 → 100644
View file @
550e1a79
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
*/
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
.
deque
;
import
java.util.Iterator
;
import
java.util.NoSuchElementException
;
import
java.util.concurrent.BlockingQueue
;
import
java.util.concurrent.TimeUnit
;
/**
* A {@link Deque} that additionally supports blocking operations that wait
* for the deque to become non-empty when retrieving an element, and wait for
* space to become available in the deque when storing an element.
*
* <p><tt>BlockingDeque</tt> methods come in four forms, with different ways
* of handling operations that cannot be satisfied immediately, but may be
* satisfied at some point in the future:
* one throws an exception, the second returns a special value (either
* <tt>null</tt> or <tt>false</tt>, depending on the operation), the third
* blocks the current thread indefinitely until the operation can succeed,
* and the fourth blocks for only a given maximum time limit before giving
* up. These methods are summarized in the following table:
*
* <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1>
* <tr>
* <td ALIGN=CENTER COLSPAN = 5> <b>First Element (Head)</b></td>
* </tr>
* <tr>
* <td></td>
* <td ALIGN=CENTER><em>Throws exception</em></td>
* <td ALIGN=CENTER><em>Special value</em></td>
* <td ALIGN=CENTER><em>Blocks</em></td>
* <td ALIGN=CENTER><em>Times out</em></td>
* </tr>
* <tr>
* <td><b>Insert</b></td>
* <td>{@link #addFirst addFirst(e)}</td>
* <td>{@link #offerFirst offerFirst(e)}</td>
* <td>{@link #putFirst putFirst(e)}</td>
* <td>{@link #offerFirst offerFirst(e, time, unit)}</td>
* </tr>
* <tr>
* <td><b>Remove</b></td>
* <td>{@link #removeFirst removeFirst()}</td>
* <td>{@link #pollFirst pollFirst()}</td>
* <td>{@link #takeFirst takeFirst()}</td>
* <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td>
* </tr>
* <tr>
* <td><b>Examine</b></td>
* <td>{@link #getFirst getFirst()}</td>
* <td>{@link #peekFirst peekFirst()}</td>
* <td><em>not applicable</em></td>
* <td><em>not applicable</em></td>
* </tr>
* <tr>
* <td ALIGN=CENTER COLSPAN = 5> <b>Last Element (Tail)</b></td>
* </tr>
* <tr>
* <td></td>
* <td ALIGN=CENTER><em>Throws exception</em></td>
* <td ALIGN=CENTER><em>Special value</em></td>
* <td ALIGN=CENTER><em>Blocks</em></td>
* <td ALIGN=CENTER><em>Times out</em></td>
* </tr>
* <tr>
* <td><b>Insert</b></td>
* <td>{@link #addLast addLast(e)}</td>
* <td>{@link #offerLast offerLast(e)}</td>
* <td>{@link #putLast putLast(e)}</td>
* <td>{@link #offerLast offerLast(e, time, unit)}</td>
* </tr>
* <tr>
* <td><b>Remove</b></td>
* <td>{@link #removeLast() removeLast()}</td>
* <td>{@link #pollLast() pollLast()}</td>
* <td>{@link #takeLast takeLast()}</td>
* <td>{@link #pollLast(long, TimeUnit) pollLast(time, unit)}</td>
* </tr>
* <tr>
* <td><b>Examine</b></td>
* <td>{@link #getLast getLast()}</td>
* <td>{@link #peekLast peekLast()}</td>
* <td><em>not applicable</em></td>
* <td><em>not applicable</em></td>
* </tr>
* </table>
*
* <p>Like any {@link BlockingQueue}, a <tt>BlockingDeque</tt> is thread safe,
* does not permit null elements, and may (or may not) be
* capacity-constrained.
*
* <p>A <tt>BlockingDeque</tt> implementation may be used directly as a FIFO
* <tt>BlockingQueue</tt>. The methods inherited from the
* <tt>BlockingQueue</tt> interface are precisely equivalent to
* <tt>BlockingDeque</tt> methods as indicated in the following table:
*
* <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1>
* <tr>
* <td ALIGN=CENTER> <b><tt>BlockingQueue</tt> Method</b></td>
* <td ALIGN=CENTER> <b>Equivalent <tt>BlockingDeque</tt> Method</b></td>
* </tr>
* <tr>
* <td ALIGN=CENTER COLSPAN = 2> <b>Insert</b></td>
* </tr>
* <tr>
* <td>{@link #add add(e)}</td>
* <td>{@link #addLast addLast(e)}</td>
* </tr>
* <tr>
* <td>{@link #offer offer(e)}</td>
* <td>{@link #offerLast offerLast(e)}</td>
* </tr>
* <tr>
* <td>{@link #put put(e)}</td>
* <td>{@link #putLast putLast(e)}</td>
* </tr>
* <tr>
* <td>{@link #offer offer(e, time, unit)}</td>
* <td>{@link #offerLast offerLast(e, time, unit)}</td>
* </tr>
* <tr>
* <td ALIGN=CENTER COLSPAN = 2> <b>Remove</b></td>
* </tr>
* <tr>
* <td>{@link #remove() remove()}</td>
* <td>{@link #removeFirst() removeFirst()}</td>
* </tr>
* <tr>
* <td>{@link #poll() poll()}</td>
* <td>{@link #pollFirst() pollFirst()}</td>
* </tr>
* <tr>
* <td>{@link #take() take()}</td>
* <td>{@link #takeFirst() takeFirst()}</td>
* </tr>
* <tr>
* <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td>
* <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td>
* </tr>
* <tr>
* <td ALIGN=CENTER COLSPAN = 2> <b>Examine</b></td>
* </tr>
* <tr>
* <td>{@link #element() element()}</td>
* <td>{@link #getFirst() getFirst()}</td>
* </tr>
* <tr>
* <td>{@link #peek() peek()}</td>
* <td>{@link #peekFirst() peekFirst()}</td>
* </tr>
* </table>
*
* <p>Memory consistency effects: As with other concurrent
* collections, actions in a thread prior to placing an object into a
* {@code BlockingDeque}
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
* actions subsequent to the access or removal of that element from
* the {@code BlockingDeque} in another thread.
*
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @since 1.6
* @author Doug Lea
* @param <E> the type of elements held in this collection
*/
public
interface
BlockingDeque
<
E
>
extends
BlockingQueue
<
E
>,
Deque
<
E
>
{
/*
* We have "diamond" multiple interface inheritance here, and that
* introduces ambiguities. Methods might end up with different
* specs depending on the branch chosen by javadoc. Thus a lot of
* methods specs here are copied from superinterfaces.
*/
/**
* Inserts the specified element at the front of this deque if it is
* possible to do so immediately without violating capacity restrictions,
* throwing an <tt>IllegalStateException</tt> if no space is currently
* available. When using a capacity-restricted deque, it is generally
* preferable to use {@link #offerFirst offerFirst}.
*
* @param e the element to add
* @throws IllegalStateException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException {@inheritDoc}
*/
void
addFirst
(
E
e
);
/**
* Inserts the specified element at the end of this deque if it is
* possible to do so immediately without violating capacity restrictions,
* throwing an <tt>IllegalStateException</tt> if no space is currently
* available. When using a capacity-restricted deque, it is generally
* preferable to use {@link #offerLast offerLast}.
*
* @param e the element to add
* @throws IllegalStateException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException {@inheritDoc}
*/
void
addLast
(
E
e
);
/**
* Inserts the specified element at the front of this deque if it is
* possible to do so immediately without violating capacity restrictions,
* returning <tt>true</tt> upon success and <tt>false</tt> if no space is
* currently available.
* When using a capacity-restricted deque, this method is generally
* preferable to the {@link #addFirst addFirst} method, which can
* fail to insert an element only by throwing an exception.
*
* @param e the element to add
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException {@inheritDoc}
*/
boolean
offerFirst
(
E
e
);
/**
* Inserts the specified element at the end of this deque if it is
* possible to do so immediately without violating capacity restrictions,
* returning <tt>true</tt> upon success and <tt>false</tt> if no space is
* currently available.
* When using a capacity-restricted deque, this method is generally
* preferable to the {@link #addLast addLast} method, which can
* fail to insert an element only by throwing an exception.
*
* @param e the element to add
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException {@inheritDoc}
*/
boolean
offerLast
(
E
e
);
/**
* Inserts the specified element at the front of this deque,
* waiting if necessary for space to become available.
*
* @param e the element to add
* @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
void
putFirst
(
E
e
)
throws
InterruptedException
;
/**
* Inserts the specified element at the end of this deque,
* waiting if necessary for space to become available.
*
* @param e the element to add
* @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
void
putLast
(
E
e
)
throws
InterruptedException
;
/**
* Inserts the specified element at the front of this deque,
* waiting up to the specified wait time if necessary for space to
* become available.
*
* @param e the element to add
* @param timeout how long to wait before giving up, in units of
* <tt>unit</tt>
* @param unit a <tt>TimeUnit</tt> determining how to interpret the
* <tt>timeout</tt> parameter
* @return <tt>true</tt> if successful, or <tt>false</tt> if
* the specified waiting time elapses before space is available
* @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
offerFirst
(
E
e
,
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
;
/**
* Inserts the specified element at the end of this deque,
* waiting up to the specified wait time if necessary for space to
* become available.
*
* @param e the element to add
* @param timeout how long to wait before giving up, in units of
* <tt>unit</tt>
* @param unit a <tt>TimeUnit</tt> determining how to interpret the
* <tt>timeout</tt> parameter
* @return <tt>true</tt> if successful, or <tt>false</tt> if
* the specified waiting time elapses before space is available
* @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
offerLast
(
E
e
,
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
;
/**
* Retrieves and removes the first element of this deque, waiting
* if necessary until an element becomes available.
*
* @return the head of this deque
* @throws InterruptedException if interrupted while waiting
*/
E
takeFirst
()
throws
InterruptedException
;
/**
* Retrieves and removes the last element of this deque, waiting
* if necessary until an element becomes available.
*
* @return the tail of this deque
* @throws InterruptedException if interrupted while waiting
*/
E
takeLast
()
throws
InterruptedException
;
/**
* Retrieves and removes the first element of this deque, waiting
* up to the specified wait time if necessary for an element to
* become available.
*
* @param timeout how long to wait before giving up, in units of
* <tt>unit</tt>
* @param unit a <tt>TimeUnit</tt> determining how to interpret the
* <tt>timeout</tt> parameter
* @return the head of this deque, or <tt>null</tt> if the specified
* waiting time elapses before an element is available
* @throws InterruptedException if interrupted while waiting
*/
E
pollFirst
(
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
;
/**
* Retrieves and removes the last element of this deque, waiting
* up to the specified wait time if necessary for an element to
* become available.
*
* @param timeout how long to wait before giving up, in units of
* <tt>unit</tt>
* @param unit a <tt>TimeUnit</tt> determining how to interpret the
* <tt>timeout</tt> parameter
* @return the tail of this deque, or <tt>null</tt> if the specified
* waiting time elapses before an element is available
* @throws InterruptedException if interrupted while waiting
*/
E
pollLast
(
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
;
/**
* Removes the first occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged.
* More formally, removes the first element <tt>e</tt> such that
* <tt>o.equals(e)</tt> (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call).
*
* @param o element to be removed from this deque, if present
* @return <tt>true</tt> if an element was removed as a result of this call
* @throws ClassCastException if the class of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null (optional)
*/
boolean
removeFirstOccurrence
(
Object
o
);
/**
* Removes the last occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged.
* More formally, removes the last element <tt>e</tt> such that
* <tt>o.equals(e)</tt> (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call).
*
* @param o element to be removed from this deque, if present
* @return <tt>true</tt> if an element was removed as a result of this call
* @throws ClassCastException if the class of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null (optional)
*/
boolean
removeLastOccurrence
(
Object
o
);
// *** BlockingQueue methods ***
/**
* Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque) if it is possible to do so
* immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and throwing an
* <tt>IllegalStateException</tt> if no space is currently available.
* When using a capacity-restricted deque, it is generally preferable to
* use {@link #offer offer}.
*
* <p>This method is equivalent to {@link #addLast addLast}.
*
* @param e the element to add
* @throws IllegalStateException {@inheritDoc}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
add
(
E
e
);
/**
* Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque) if it is possible to do so
* immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and <tt>false</tt> if no space is currently
* available. When using a capacity-restricted deque, this method is
* generally preferable to the {@link #add} method, which can fail to
* insert an element only by throwing an exception.
*
* <p>This method is equivalent to {@link #offerLast offerLast}.
*
* @param e the element to add
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
offer
(
E
e
);
/**
* Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque), waiting if necessary for
* space to become available.
*
* <p>This method is equivalent to {@link #putLast putLast}.
*
* @param e the element to add
* @throws InterruptedException {@inheritDoc}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
void
put
(
E
e
)
throws
InterruptedException
;
/**
* Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque), waiting up to the
* specified wait time if necessary for space to become available.
*
* <p>This method is equivalent to
* {@link #offerLast offerLast}.
*
* @param e the element to add
* @return <tt>true</tt> if the element was added to this deque, else
* <tt>false</tt>
* @throws InterruptedException {@inheritDoc}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
offer
(
E
e
,
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
;
/**
* Retrieves and removes the head of the queue represented by this deque
* (in other words, the first element of this deque).
* This method differs from {@link #poll poll} only in that it
* throws an exception if this deque is empty.
*
* <p>This method is equivalent to {@link #removeFirst() removeFirst}.
*
* @return the head of the queue represented by this deque
* @throws NoSuchElementException if this deque is empty
*/
E
remove
();
/**
* Retrieves and removes the head of the queue represented by this deque
* (in other words, the first element of this deque), or returns
* <tt>null</tt> if this deque is empty.
*
* <p>This method is equivalent to {@link #pollFirst()}.
*
* @return the head of this deque, or <tt>null</tt> if this deque is empty
*/
E
poll
();
/**
* Retrieves and removes the head of the queue represented by this deque
* (in other words, the first element of this deque), waiting if
* necessary until an element becomes available.
*
* <p>This method is equivalent to {@link #takeFirst() takeFirst}.
*
* @return the head of this deque
* @throws InterruptedException if interrupted while waiting
*/
E
take
()
throws
InterruptedException
;
/**
* Retrieves and removes the head of the queue represented by this deque
* (in other words, the first element of this deque), waiting up to the
* specified wait time if necessary for an element to become available.
*
* <p>This method is equivalent to
* {@link #pollFirst(long,TimeUnit) pollFirst}.
*
* @return the head of this deque, or <tt>null</tt> if the
* specified waiting time elapses before an element is available
* @throws InterruptedException if interrupted while waiting
*/
E
poll
(
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
;
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque (in other words, the first element of this deque).
* This method differs from {@link #peek peek} only in that it throws an
* exception if this deque is empty.
*
* <p>This method is equivalent to {@link #getFirst() getFirst}.
*
* @return the head of this deque
* @throws NoSuchElementException if this deque is empty
*/
E
element
();
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque (in other words, the first element of this deque), or
* returns <tt>null</tt> if this deque is empty.
*
* <p>This method is equivalent to {@link #peekFirst() peekFirst}.
*
* @return the head of this deque, or <tt>null</tt> if this deque is empty
*/
E
peek
();
/**
* Removes the first occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged.
* More formally, removes the first element <tt>e</tt> such that
* <tt>o.equals(e)</tt> (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call).
*
* <p>This method is equivalent to
* {@link #removeFirstOccurrence removeFirstOccurrence}.
*
* @param o element to be removed from this deque, if present
* @return <tt>true</tt> if this deque changed as a result of the call
* @throws ClassCastException if the class of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null (optional)
*/
boolean
remove
(
Object
o
);
/**
* Returns <tt>true</tt> if this deque contains the specified element.
* More formally, returns <tt>true</tt> if and only if this deque contains
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
*
* @param o object to be checked for containment in this deque
* @return <tt>true</tt> if this deque contains the specified element
* @throws ClassCastException if the class of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null (optional)
*/
public
boolean
contains
(
Object
o
);
/**
* Returns the number of elements in this deque.
*
* @return the number of elements in this deque
*/
public
int
size
();
/**
* Returns an iterator over the elements in this deque in proper sequence.
* The elements will be returned in order from first (head) to last (tail).
*
* @return an iterator over the elements in this deque in proper sequence
*/
Iterator
<
E
>
iterator
();
// *** Stack methods ***
/**
* Pushes an element onto the stack represented by this deque. In other
* words, inserts the element at the front of this deque unless it would
* violate capacity restrictions.
*
* <p>This method is equivalent to {@link #addFirst addFirst}.
*
* @throws IllegalStateException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException {@inheritDoc}
*/
void
push
(
E
e
);
}
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/Deque.java
0 → 100644
View file @
550e1a79
/*
* Written by Doug Lea and Josh Bloch with assistance from members of
* JCP JSR-166 Expert Group and released to the public domain, as explained
* at http://creativecommons.org/licenses/publicdomain
*/
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
.
deque
;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.NoSuchElementException
;
import
java.util.Queue
;
import
java.util.Stack
;
/**
* A linear collection that supports element insertion and removal at
* both ends. The name <i>deque</i> is short for "double ended queue"
* and is usually pronounced "deck". Most <tt>Deque</tt>
* implementations place no fixed limits on the number of elements
* they may contain, but this interface supports capacity-restricted
* deques as well as those with no fixed size limit.
*
* <p>This interface defines methods to access the elements at both
* ends of the deque. Methods are provided to insert, remove, and
* examine the element. Each of these methods exists in two forms:
* one throws an exception if the operation fails, the other returns a
* special value (either <tt>null</tt> or <tt>false</tt>, depending on
* the operation). The latter form of the insert operation is
* designed specifically for use with capacity-restricted
* <tt>Deque</tt> implementations; in most implementations, insert
* operations cannot fail.
*
* <p>The twelve methods described above are summarized in the
* following table:
*
* <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1>
* <tr>
* <td></td>
* <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>
* <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>
* </tr>
* <tr>
* <td></td>
* <td ALIGN=CENTER><em>Throws exception</em></td>
* <td ALIGN=CENTER><em>Special value</em></td>
* <td ALIGN=CENTER><em>Throws exception</em></td>
* <td ALIGN=CENTER><em>Special value</em></td>
* </tr>
* <tr>
* <td><b>Insert</b></td>
* <td>{@link #addFirst addFirst(e)}</td>
* <td>{@link #offerFirst offerFirst(e)}</td>
* <td>{@link #addLast addLast(e)}</td>
* <td>{@link #offerLast offerLast(e)}</td>
* </tr>
* <tr>
* <td><b>Remove</b></td>
* <td>{@link #removeFirst removeFirst()}</td>
* <td>{@link #pollFirst pollFirst()}</td>
* <td>{@link #removeLast removeLast()}</td>
* <td>{@link #pollLast pollLast()}</td>
* </tr>
* <tr>
* <td><b>Examine</b></td>
* <td>{@link #getFirst getFirst()}</td>
* <td>{@link #peekFirst peekFirst()}</td>
* <td>{@link #getLast getLast()}</td>
* <td>{@link #peekLast peekLast()}</td>
* </tr>
* </table>
*
* <p>This interface extends the {@link Queue} interface. When a deque is
* used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
* added at the end of the deque and removed from the beginning. The methods
* inherited from the <tt>Queue</tt> interface are precisely equivalent to
* <tt>Deque</tt> methods as indicated in the following table:
*
* <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1>
* <tr>
* <td ALIGN=CENTER> <b><tt>Queue</tt> Method</b></td>
* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>
* </tr>
* <tr>
* <td>{@link java.util.Queue#add add(e)}</td>
* <td>{@link #addLast addLast(e)}</td>
* </tr>
* <tr>
* <td>{@link java.util.Queue#offer offer(e)}</td>
* <td>{@link #offerLast offerLast(e)}</td>
* </tr>
* <tr>
* <td>{@link java.util.Queue#remove remove()}</td>
* <td>{@link #removeFirst removeFirst()}</td>
* </tr>
* <tr>
* <td>{@link java.util.Queue#poll poll()}</td>
* <td>{@link #pollFirst pollFirst()}</td>
* </tr>
* <tr>
* <td>{@link java.util.Queue#element element()}</td>
* <td>{@link #getFirst getFirst()}</td>
* </tr>
* <tr>
* <td>{@link java.util.Queue#peek peek()}</td>
* <td>{@link #peek peekFirst()}</td>
* </tr>
* </table>
*
* <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This
* interface should be used in preference to the legacy {@link Stack} class.
* When a deque is used as a stack, elements are pushed and popped from the
* beginning of the deque. Stack methods are precisely equivalent to
* <tt>Deque</tt> methods as indicated in the table below:
*
* <p>
* <table BORDER CELLPADDING=3 CELLSPACING=1>
* <tr>
* <td ALIGN=CENTER> <b>Stack Method</b></td>
* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>
* </tr>
* <tr>
* <td>{@link #push push(e)}</td>
* <td>{@link #addFirst addFirst(e)}</td>
* </tr>
* <tr>
* <td>{@link #pop pop()}</td>
* <td>{@link #removeFirst removeFirst()}</td>
* </tr>
* <tr>
* <td>{@link #peek peek()}</td>
* <td>{@link #peekFirst peekFirst()}</td>
* </tr>
* </table>
*
* <p>Note that the {@link #peek peek} method works equally well when
* a deque is used as a queue or a stack; in either case, elements are
* drawn from the beginning of the deque.
*
* <p>This interface provides two methods to remove interior
* elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
* {@link #removeLastOccurrence removeLastOccurrence}.
*
* <p>Unlike the {@link List} interface, this interface does not
* provide support for indexed access to elements.
*
* <p>While <tt>Deque</tt> implementations are not strictly required
* to prohibit the insertion of null elements, they are strongly
* encouraged to do so. Users of any <tt>Deque</tt> implementations
* that do allow null elements are strongly encouraged <i>not</i> to
* take advantage of the ability to insert nulls. This is so because
* <tt>null</tt> is used as a special return value by various methods
* to indicated that the deque is empty.
*
* <p><tt>Deque</tt> implementations generally do not define
* element-based versions of the <tt>equals</tt> and <tt>hashCode</tt>
* methods, but instead inherit the identity-based versions from class
* <tt>Object</tt>.
*
* @author Doug Lea
* @author Josh Bloch
* @since 1.6
* @param <E> the type of elements held in this collection
*/
public
interface
Deque
<
E
>
extends
Queue
<
E
>
{
/**
* Inserts the specified element at the front of this deque if it is
* possible to do so immediately without violating capacity restrictions.
* When using a capacity-restricted deque, it is generally preferable to
* use method {@link #offerFirst}.
*
* @param e the element to add
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
void
addFirst
(
E
e
);
/**
* Inserts the specified element at the end of this deque if it is
* possible to do so immediately without violating capacity restrictions.
* When using a capacity-restricted deque, it is generally preferable to
* use method {@link #offerLast}.
*
* <p>This method is equivalent to {@link #add}.
*
* @param e the element to add
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
void
addLast
(
E
e
);
/**
* Inserts the specified element at the front of this deque unless it would
* violate capacity restrictions. When using a capacity-restricted deque,
* this method is generally preferable to the {@link #addFirst} method,
* which can fail to insert an element only by throwing an exception.
*
* @param e the element to add
* @return <tt>true</tt> if the element was added to this deque, else
* <tt>false</tt>
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
offerFirst
(
E
e
);
/**
* Inserts the specified element at the end of this deque unless it would
* violate capacity restrictions. When using a capacity-restricted deque,
* this method is generally preferable to the {@link #addLast} method,
* which can fail to insert an element only by throwing an exception.
*
* @param e the element to add
* @return <tt>true</tt> if the element was added to this deque, else
* <tt>false</tt>
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
offerLast
(
E
e
);
/**
* Retrieves and removes the first element of this deque. This method
* differs from {@link #pollFirst pollFirst} only in that it throws an
* exception if this deque is empty.
*
* @return the head of this deque
* @throws NoSuchElementException if this deque is empty
*/
E
removeFirst
();
/**
* Retrieves and removes the last element of this deque. This method
* differs from {@link #pollLast pollLast} only in that it throws an
* exception if this deque is empty.
*
* @return the tail of this deque
* @throws NoSuchElementException if this deque is empty
*/
E
removeLast
();
/**
* Retrieves and removes the first element of this deque,
* or returns <tt>null</tt> if this deque is empty.
*
* @return the head of this deque, or <tt>null</tt> if this deque is empty
*/
E
pollFirst
();
/**
* Retrieves and removes the last element of this deque,
* or returns <tt>null</tt> if this deque is empty.
*
* @return the tail of this deque, or <tt>null</tt> if this deque is empty
*/
E
pollLast
();
/**
* Retrieves, but does not remove, the first element of this deque.
*
* This method differs from {@link #peekFirst peekFirst} only in that it
* throws an exception if this deque is empty.
*
* @return the head of this deque
* @throws NoSuchElementException if this deque is empty
*/
E
getFirst
();
/**
* Retrieves, but does not remove, the last element of this deque.
* This method differs from {@link #peekLast peekLast} only in that it
* throws an exception if this deque is empty.
*
* @return the tail of this deque
* @throws NoSuchElementException if this deque is empty
*/
E
getLast
();
/**
* Retrieves, but does not remove, the first element of this deque,
* or returns <tt>null</tt> if this deque is empty.
*
* @return the head of this deque, or <tt>null</tt> if this deque is empty
*/
E
peekFirst
();
/**
* Retrieves, but does not remove, the last element of this deque,
* or returns <tt>null</tt> if this deque is empty.
*
* @return the tail of this deque, or <tt>null</tt> if this deque is empty
*/
E
peekLast
();
/**
* Removes the first occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged.
* More formally, removes the first element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>
* (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call).
*
* @param o element to be removed from this deque, if present
* @return <tt>true</tt> if an element was removed as a result of this call
* @throws ClassCastException if the class of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements (optional)
*/
boolean
removeFirstOccurrence
(
Object
o
);
/**
* Removes the last occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged.
* More formally, removes the last element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>
* (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call).
*
* @param o element to be removed from this deque, if present
* @return <tt>true</tt> if an element was removed as a result of this call
* @throws ClassCastException if the class of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements (optional)
*/
boolean
removeLastOccurrence
(
Object
o
);
// *** Queue methods ***
/**
* Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque) if it is possible to do so
* immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and throwing an
* <tt>IllegalStateException</tt> if no space is currently available.
* When using a capacity-restricted deque, it is generally preferable to
* use {@link #offer offer}.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e the element to add
* @return <tt>true</tt> (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
add
(
E
e
);
/**
* Inserts the specified element into the queue represented by this deque
* (in other words, at the tail of this deque) if it is possible to do so
* immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and <tt>false</tt> if no space is currently
* available. When using a capacity-restricted deque, this method is
* generally preferable to the {@link #add} method, which can fail to
* insert an element only by throwing an exception.
*
* <p>This method is equivalent to {@link #offerLast}.
*
* @param e the element to add
* @return <tt>true</tt> if the element was added to this deque, else
* <tt>false</tt>
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
boolean
offer
(
E
e
);
/**
* Retrieves and removes the head of the queue represented by this deque
* (in other words, the first element of this deque).
* This method differs from {@link #poll poll} only in that it throws an
* exception if this deque is empty.
*
* <p>This method is equivalent to {@link #removeFirst()}.
*
* @return the head of the queue represented by this deque
* @throws NoSuchElementException if this deque is empty
*/
E
remove
();
/**
* Retrieves and removes the head of the queue represented by this deque
* (in other words, the first element of this deque), or returns
* <tt>null</tt> if this deque is empty.
*
* <p>This method is equivalent to {@link #pollFirst()}.
*
* @return the first element of this deque, or <tt>null</tt> if
* this deque is empty
*/
E
poll
();
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque (in other words, the first element of this deque).
* This method differs from {@link #peek peek} only in that it throws an
* exception if this deque is empty.
*
* <p>This method is equivalent to {@link #getFirst()}.
*
* @return the head of the queue represented by this deque
* @throws NoSuchElementException if this deque is empty
*/
E
element
();
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque (in other words, the first element of this deque), or
* returns <tt>null</tt> if this deque is empty.
*
* <p>This method is equivalent to {@link #peekFirst()}.
*
* @return the head of the queue represented by this deque, or
* <tt>null</tt> if this deque is empty
*/
E
peek
();
// *** Stack methods ***
/**
* Pushes an element onto the stack represented by this deque (in other
* words, at the head of this deque) if it is possible to do so
* immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and throwing an
* <tt>IllegalStateException</tt> if no space is currently available.
*
* <p>This method is equivalent to {@link #addFirst}.
*
* @param e the element to push
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this deque
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this deque
*/
void
push
(
E
e
);
/**
* Pops an element from the stack represented by this deque. In other
* words, removes and returns the first element of this deque.
*
* <p>This method is equivalent to {@link #removeFirst()}.
*
* @return the element at the front of this deque (which is the top
* of the stack represented by this deque)
* @throws NoSuchElementException if this deque is empty
*/
E
pop
();
// *** Collection methods ***
/**
* Removes the first occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged.
* More formally, removes the first element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>
* (if such an element exists).
* Returns <tt>true</tt> if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call).
*
* <p>This method is equivalent to {@link #removeFirstOccurrence}.
*
* @param o element to be removed from this deque, if present
* @return <tt>true</tt> if an element was removed as a result of this call
* @throws ClassCastException if the class of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements (optional)
*/
boolean
remove
(
Object
o
);
/**
* Returns <tt>true</tt> if this deque contains the specified element.
* More formally, returns <tt>true</tt> if and only if this deque contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this deque is to be tested
* @return <tt>true</tt> if this deque contains the specified element
* @throws ClassCastException if the type of the specified element
* is incompatible with this deque (optional)
* @throws NullPointerException if the specified element is null and this
* deque does not permit null elements (optional)
*/
boolean
contains
(
Object
o
);
/**
* Returns the number of elements in this deque.
*
* @return the number of elements in this deque
*/
public
int
size
();
/**
* Returns an iterator over the elements in this deque in proper sequence.
* The elements will be returned in order from first (head) to last (tail).
*
* @return an iterator over the elements in this deque in proper sequence
*/
Iterator
<
E
>
iterator
();
/**
* Returns an iterator over the elements in this deque in reverse
* sequential order. The elements will be returned in order from
* last (tail) to first (head).
*
* @return an iterator over the elements in this deque in reverse
* sequence
*/
Iterator
<
E
>
descendingIterator
();
}
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/LIFOLinkedBlockingDeque.java
0 → 100644
View file @
550e1a79
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
.
deque
;
import
java.util.NoSuchElementException
;
/**
* {@link LinkedBlockingDeque} using LIFO algorithm
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @since 1.6.3
*/
public
class
LIFOLinkedBlockingDeque
<
T
>
extends
LinkedBlockingDeque
<
T
>
{
private
static
final
long
serialVersionUID
=
-
4114786347960826192L
;
/**
* Inserts the specified element at the front of this deque if it is possible to do so immediately without violating
* capacity restrictions, returning <tt>true</tt> upon success and <tt>false</tt> if no space is currently
* available. When using a capacity-restricted deque, this method is generally preferable to the {@link #addFirst
* addFirst} method, which can fail to insert an element only by throwing an exception.
*
* @param e
* the element to add
* @throws ClassCastException
* {@inheritDoc}
* @throws NullPointerException
* if the specified element is null
* @throws IllegalArgumentException
* {@inheritDoc}
*/
@Override
public
boolean
offer
(
T
e
)
{
return
super
.
offerFirst
(
e
);
}
/**
* Retrieves and removes the first element of this deque. This method differs from {@link #pollFirst pollFirst} only
* in that it throws an exception if this deque is empty.
*
* @return the head of this deque
* @throws NoSuchElementException
* if this deque is empty
*/
@Override
public
T
remove
()
{
return
super
.
removeFirst
();
}
}
\ No newline at end of file
imageLoader/src/com/nostra13/universalimageloader/core/assist/deque/LinkedBlockingDeque.java
0 → 100644
View file @
550e1a79
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
*/
package
com
.
nostra13
.
universalimageloader
.
core
.
assist
.
deque
;
import
java.util.AbstractQueue
;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.NoSuchElementException
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.locks.Condition
;
import
java.util.concurrent.locks.ReentrantLock
;
/**
* An optionally-bounded {@linkplain BlockingDeque blocking deque} based on
* linked nodes.
*
* <p> The optional capacity bound constructor argument serves as a
* way to prevent excessive expansion. The capacity, if unspecified,
* is equal to {@link Integer#MAX_VALUE}. Linked nodes are
* dynamically created upon each insertion unless this would bring the
* deque above capacity.
*
* <p>Most operations run in constant time (ignoring time spent
* blocking). Exceptions include {@link #remove(Object) remove},
* {@link #removeFirstOccurrence removeFirstOccurrence}, {@link
* #removeLastOccurrence removeLastOccurrence}, {@link #contains
* contains}, {@link #iterator iterator.remove()}, and the bulk
* operations, all of which run in linear time.
*
* <p>This class and its iterator implement all of the
* <em>optional</em> methods of the {@link Collection} and {@link
* Iterator} interfaces.
*
* <p>This class is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @since 1.6
* @author Doug Lea
* @param <E> the type of elements held in this collection
*/
public
class
LinkedBlockingDeque
<
E
>
extends
AbstractQueue
<
E
>
implements
BlockingDeque
<
E
>,
java
.
io
.
Serializable
{
/*
* Implemented as a simple doubly-linked list protected by a
* single lock and using conditions to manage blocking.
*
* To implement weakly consistent iterators, it appears we need to
* keep all Nodes GC-reachable from a predecessor dequeued Node.
* That would cause two problems:
* - allow a rogue Iterator to cause unbounded memory retention
* - cause cross-generational linking of old Nodes to new Nodes if
* a Node was tenured while live, which generational GCs have a
* hard time dealing with, causing repeated major collections.
* However, only non-deleted Nodes need to be reachable from
* dequeued Nodes, and reachability does not necessarily have to
* be of the kind understood by the GC. We use the trick of
* linking a Node that has just been dequeued to itself. Such a
* self-link implicitly means to jump to "first" (for next links)
* or "last" (for prev links).
*/
/*
* We have "diamond" multiple interface/abstract class inheritance
* here, and that introduces ambiguities. Often we want the
* BlockingDeque javadoc combined with the AbstractQueue
* implementation, so a lot of method specs are duplicated here.
*/
private
static
final
long
serialVersionUID
=
-
387911632671998426L
;
/** Doubly-linked list node class */
static
final
class
Node
<
E
>
{
/**
* The item, or null if this node has been removed.
*/
E
item
;
/**
* One of:
* - the real predecessor Node
* - this Node, meaning the predecessor is tail
* - null, meaning there is no predecessor
*/
Node
<
E
>
prev
;
/**
* One of:
* - the real successor Node
* - this Node, meaning the successor is head
* - null, meaning there is no successor
*/
Node
<
E
>
next
;
Node
(
E
x
)
{
item
=
x
;
}
}
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient
Node
<
E
>
first
;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient
Node
<
E
>
last
;
/** Number of items in the deque */
private
transient
int
count
;
/** Maximum number of items in the deque */
private
final
int
capacity
;
/** Main lock guarding all access */
final
ReentrantLock
lock
=
new
ReentrantLock
();
/** Condition for waiting takes */
private
final
Condition
notEmpty
=
lock
.
newCondition
();
/** Condition for waiting puts */
private
final
Condition
notFull
=
lock
.
newCondition
();
/**
* Creates a {@code LinkedBlockingDeque} with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public
LinkedBlockingDeque
()
{
this
(
Integer
.
MAX_VALUE
);
}
/**
* Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity.
*
* @param capacity the capacity of this deque
* @throws IllegalArgumentException if {@code capacity} is less than 1
*/
public
LinkedBlockingDeque
(
int
capacity
)
{
if
(
capacity
<=
0
)
throw
new
IllegalArgumentException
();
this
.
capacity
=
capacity
;
}
/**
* Creates a {@code LinkedBlockingDeque} with a capacity of
* {@link Integer#MAX_VALUE}, initially containing the elements of
* the given collection, added in traversal order of the
* collection's iterator.
*
* @param c the collection of elements to initially contain
* @throws NullPointerException if the specified collection or any
* of its elements are null
*/
public
LinkedBlockingDeque
(
Collection
<?
extends
E
>
c
)
{
this
(
Integer
.
MAX_VALUE
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
// Never contended, but necessary for visibility
try
{
for
(
E
e
:
c
)
{
if
(
e
==
null
)
throw
new
NullPointerException
();
if
(!
linkLast
(
new
Node
<
E
>(
e
)))
throw
new
IllegalStateException
(
"Deque full"
);
}
}
finally
{
lock
.
unlock
();
}
}
// Basic linking and unlinking operations, called only while holding lock
/**
* Links node as first element, or returns false if full.
*/
private
boolean
linkFirst
(
Node
<
E
>
node
)
{
// assert lock.isHeldByCurrentThread();
if
(
count
>=
capacity
)
return
false
;
Node
<
E
>
f
=
first
;
node
.
next
=
f
;
first
=
node
;
if
(
last
==
null
)
last
=
node
;
else
f
.
prev
=
node
;
++
count
;
notEmpty
.
signal
();
return
true
;
}
/**
* Links node as last element, or returns false if full.
*/
private
boolean
linkLast
(
Node
<
E
>
node
)
{
// assert lock.isHeldByCurrentThread();
if
(
count
>=
capacity
)
return
false
;
Node
<
E
>
l
=
last
;
node
.
prev
=
l
;
last
=
node
;
if
(
first
==
null
)
first
=
node
;
else
l
.
next
=
node
;
++
count
;
notEmpty
.
signal
();
return
true
;
}
/**
* Removes and returns first element, or null if empty.
*/
private
E
unlinkFirst
()
{
// assert lock.isHeldByCurrentThread();
Node
<
E
>
f
=
first
;
if
(
f
==
null
)
return
null
;
Node
<
E
>
n
=
f
.
next
;
E
item
=
f
.
item
;
f
.
item
=
null
;
f
.
next
=
f
;
// help GC
first
=
n
;
if
(
n
==
null
)
last
=
null
;
else
n
.
prev
=
null
;
--
count
;
notFull
.
signal
();
return
item
;
}
/**
* Removes and returns last element, or null if empty.
*/
private
E
unlinkLast
()
{
// assert lock.isHeldByCurrentThread();
Node
<
E
>
l
=
last
;
if
(
l
==
null
)
return
null
;
Node
<
E
>
p
=
l
.
prev
;
E
item
=
l
.
item
;
l
.
item
=
null
;
l
.
prev
=
l
;
// help GC
last
=
p
;
if
(
p
==
null
)
first
=
null
;
else
p
.
next
=
null
;
--
count
;
notFull
.
signal
();
return
item
;
}
/**
* Unlinks x.
*/
void
unlink
(
Node
<
E
>
x
)
{
// assert lock.isHeldByCurrentThread();
Node
<
E
>
p
=
x
.
prev
;
Node
<
E
>
n
=
x
.
next
;
if
(
p
==
null
)
{
unlinkFirst
();
}
else
if
(
n
==
null
)
{
unlinkLast
();
}
else
{
p
.
next
=
n
;
n
.
prev
=
p
;
x
.
item
=
null
;
// Don't mess with x's links. They may still be in use by
// an iterator.
--
count
;
notFull
.
signal
();
}
}
// BlockingDeque methods
/**
* @throws IllegalStateException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public
void
addFirst
(
E
e
)
{
if
(!
offerFirst
(
e
))
throw
new
IllegalStateException
(
"Deque full"
);
}
/**
* @throws IllegalStateException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public
void
addLast
(
E
e
)
{
if
(!
offerLast
(
e
))
throw
new
IllegalStateException
(
"Deque full"
);
}
/**
* @throws NullPointerException {@inheritDoc}
*/
public
boolean
offerFirst
(
E
e
)
{
if
(
e
==
null
)
throw
new
NullPointerException
();
Node
<
E
>
node
=
new
Node
<
E
>(
e
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
linkFirst
(
node
);
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws NullPointerException {@inheritDoc}
*/
public
boolean
offerLast
(
E
e
)
{
if
(
e
==
null
)
throw
new
NullPointerException
();
Node
<
E
>
node
=
new
Node
<
E
>(
e
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
linkLast
(
node
);
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws NullPointerException {@inheritDoc}
* @throws InterruptedException {@inheritDoc}
*/
public
void
putFirst
(
E
e
)
throws
InterruptedException
{
if
(
e
==
null
)
throw
new
NullPointerException
();
Node
<
E
>
node
=
new
Node
<
E
>(
e
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
while
(!
linkFirst
(
node
))
notFull
.
await
();
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws NullPointerException {@inheritDoc}
* @throws InterruptedException {@inheritDoc}
*/
public
void
putLast
(
E
e
)
throws
InterruptedException
{
if
(
e
==
null
)
throw
new
NullPointerException
();
Node
<
E
>
node
=
new
Node
<
E
>(
e
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
while
(!
linkLast
(
node
))
notFull
.
await
();
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws NullPointerException {@inheritDoc}
* @throws InterruptedException {@inheritDoc}
*/
public
boolean
offerFirst
(
E
e
,
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
{
if
(
e
==
null
)
throw
new
NullPointerException
();
Node
<
E
>
node
=
new
Node
<
E
>(
e
);
long
nanos
=
unit
.
toNanos
(
timeout
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lockInterruptibly
();
try
{
while
(!
linkFirst
(
node
))
{
if
(
nanos
<=
0
)
return
false
;
nanos
=
notFull
.
awaitNanos
(
nanos
);
}
return
true
;
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws NullPointerException {@inheritDoc}
* @throws InterruptedException {@inheritDoc}
*/
public
boolean
offerLast
(
E
e
,
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
{
if
(
e
==
null
)
throw
new
NullPointerException
();
Node
<
E
>
node
=
new
Node
<
E
>(
e
);
long
nanos
=
unit
.
toNanos
(
timeout
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lockInterruptibly
();
try
{
while
(!
linkLast
(
node
))
{
if
(
nanos
<=
0
)
return
false
;
nanos
=
notFull
.
awaitNanos
(
nanos
);
}
return
true
;
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public
E
removeFirst
()
{
E
x
=
pollFirst
();
if
(
x
==
null
)
throw
new
NoSuchElementException
();
return
x
;
}
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public
E
removeLast
()
{
E
x
=
pollLast
();
if
(
x
==
null
)
throw
new
NoSuchElementException
();
return
x
;
}
public
E
pollFirst
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
unlinkFirst
();
}
finally
{
lock
.
unlock
();
}
}
public
E
pollLast
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
unlinkLast
();
}
finally
{
lock
.
unlock
();
}
}
public
E
takeFirst
()
throws
InterruptedException
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
E
x
;
while
(
(
x
=
unlinkFirst
())
==
null
)
notEmpty
.
await
();
return
x
;
}
finally
{
lock
.
unlock
();
}
}
public
E
takeLast
()
throws
InterruptedException
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
E
x
;
while
(
(
x
=
unlinkLast
())
==
null
)
notEmpty
.
await
();
return
x
;
}
finally
{
lock
.
unlock
();
}
}
public
E
pollFirst
(
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
{
long
nanos
=
unit
.
toNanos
(
timeout
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lockInterruptibly
();
try
{
E
x
;
while
(
(
x
=
unlinkFirst
())
==
null
)
{
if
(
nanos
<=
0
)
return
null
;
nanos
=
notEmpty
.
awaitNanos
(
nanos
);
}
return
x
;
}
finally
{
lock
.
unlock
();
}
}
public
E
pollLast
(
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
{
long
nanos
=
unit
.
toNanos
(
timeout
);
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lockInterruptibly
();
try
{
E
x
;
while
(
(
x
=
unlinkLast
())
==
null
)
{
if
(
nanos
<=
0
)
return
null
;
nanos
=
notEmpty
.
awaitNanos
(
nanos
);
}
return
x
;
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public
E
getFirst
()
{
E
x
=
peekFirst
();
if
(
x
==
null
)
throw
new
NoSuchElementException
();
return
x
;
}
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public
E
getLast
()
{
E
x
=
peekLast
();
if
(
x
==
null
)
throw
new
NoSuchElementException
();
return
x
;
}
public
E
peekFirst
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
(
first
==
null
)
?
null
:
first
.
item
;
}
finally
{
lock
.
unlock
();
}
}
public
E
peekLast
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
(
last
==
null
)
?
null
:
last
.
item
;
}
finally
{
lock
.
unlock
();
}
}
public
boolean
removeFirstOccurrence
(
Object
o
)
{
if
(
o
==
null
)
return
false
;
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
for
(
Node
<
E
>
p
=
first
;
p
!=
null
;
p
=
p
.
next
)
{
if
(
o
.
equals
(
p
.
item
))
{
unlink
(
p
);
return
true
;
}
}
return
false
;
}
finally
{
lock
.
unlock
();
}
}
public
boolean
removeLastOccurrence
(
Object
o
)
{
if
(
o
==
null
)
return
false
;
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
for
(
Node
<
E
>
p
=
last
;
p
!=
null
;
p
=
p
.
prev
)
{
if
(
o
.
equals
(
p
.
item
))
{
unlink
(
p
);
return
true
;
}
}
return
false
;
}
finally
{
lock
.
unlock
();
}
}
// BlockingQueue methods
/**
* Inserts the specified element at the end of this deque unless it would
* violate capacity restrictions. When using a capacity-restricted deque,
* it is generally preferable to use method {@link #offer offer}.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws NullPointerException if the specified element is null
*/
public
boolean
add
(
E
e
)
{
addLast
(
e
);
return
true
;
}
/**
* @throws NullPointerException if the specified element is null
*/
public
boolean
offer
(
E
e
)
{
return
offerLast
(
e
);
}
/**
* @throws NullPointerException {@inheritDoc}
* @throws InterruptedException {@inheritDoc}
*/
public
void
put
(
E
e
)
throws
InterruptedException
{
putLast
(
e
);
}
/**
* @throws NullPointerException {@inheritDoc}
* @throws InterruptedException {@inheritDoc}
*/
public
boolean
offer
(
E
e
,
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
{
return
offerLast
(
e
,
timeout
,
unit
);
}
/**
* Retrieves and removes the head of the queue represented by this deque.
* This method differs from {@link #poll poll} only in that it throws an
* exception if this deque is empty.
*
* <p>This method is equivalent to {@link #removeFirst() removeFirst}.
*
* @return the head of the queue represented by this deque
* @throws NoSuchElementException if this deque is empty
*/
public
E
remove
()
{
return
removeFirst
();
}
public
E
poll
()
{
return
pollFirst
();
}
public
E
take
()
throws
InterruptedException
{
return
takeFirst
();
}
public
E
poll
(
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
{
return
pollFirst
(
timeout
,
unit
);
}
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque. This method differs from {@link #peek peek} only in that
* it throws an exception if this deque is empty.
*
* <p>This method is equivalent to {@link #getFirst() getFirst}.
*
* @return the head of the queue represented by this deque
* @throws NoSuchElementException if this deque is empty
*/
public
E
element
()
{
return
getFirst
();
}
public
E
peek
()
{
return
peekFirst
();
}
/**
* Returns the number of additional elements that this deque can ideally
* (in the absence of memory or resource constraints) accept without
* blocking. This is always equal to the initial capacity of this deque
* less the current {@code size} of this deque.
*
* <p>Note that you <em>cannot</em> always tell if an attempt to insert
* an element will succeed by inspecting {@code remainingCapacity}
* because it may be the case that another thread is about to
* insert or remove an element.
*/
public
int
remainingCapacity
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
capacity
-
count
;
}
finally
{
lock
.
unlock
();
}
}
/**
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
public
int
drainTo
(
Collection
<?
super
E
>
c
)
{
return
drainTo
(
c
,
Integer
.
MAX_VALUE
);
}
/**
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
public
int
drainTo
(
Collection
<?
super
E
>
c
,
int
maxElements
)
{
if
(
c
==
null
)
throw
new
NullPointerException
();
if
(
c
==
this
)
throw
new
IllegalArgumentException
();
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
int
n
=
Math
.
min
(
maxElements
,
count
);
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
c
.
add
(
first
.
item
);
// In this order, in case add() throws.
unlinkFirst
();
}
return
n
;
}
finally
{
lock
.
unlock
();
}
}
// Stack methods
/**
* @throws IllegalStateException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public
void
push
(
E
e
)
{
addFirst
(
e
);
}
/**
* @throws NoSuchElementException {@inheritDoc}
*/
public
E
pop
()
{
return
removeFirst
();
}
// Collection methods
/**
* Removes the first occurrence of the specified element from this deque.
* If the deque does not contain the element, it is unchanged.
* More formally, removes the first element {@code e} such that
* {@code o.equals(e)} (if such an element exists).
* Returns {@code true} if this deque contained the specified element
* (or equivalently, if this deque changed as a result of the call).
*
* <p>This method is equivalent to
* {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
*
* @param o element to be removed from this deque, if present
* @return {@code true} if this deque changed as a result of the call
*/
public
boolean
remove
(
Object
o
)
{
return
removeFirstOccurrence
(
o
);
}
/**
* Returns the number of elements in this deque.
*
* @return the number of elements in this deque
*/
public
int
size
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
return
count
;
}
finally
{
lock
.
unlock
();
}
}
/**
* Returns {@code true} if this deque contains the specified element.
* More formally, returns {@code true} if and only if this deque contains
* at least one element {@code e} such that {@code o.equals(e)}.
*
* @param o object to be checked for containment in this deque
* @return {@code true} if this deque contains the specified element
*/
public
boolean
contains
(
Object
o
)
{
if
(
o
==
null
)
return
false
;
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
for
(
Node
<
E
>
p
=
first
;
p
!=
null
;
p
=
p
.
next
)
if
(
o
.
equals
(
p
.
item
))
return
true
;
return
false
;
}
finally
{
lock
.
unlock
();
}
}
/*
* TODO: Add support for more efficient bulk operations.
*
* We don't want to acquire the lock for every iteration, but we
* also want other threads a chance to interact with the
* collection, especially when count is close to capacity.
*/
// /**
// * Adds all of the elements in the specified collection to this
// * queue. Attempts to addAll of a queue to itself result in
// * {@code IllegalArgumentException}. Further, the behavior of
// * this operation is undefined if the specified collection is
// * modified while the operation is in progress.
// *
// * @param c collection containing elements to be added to this queue
// * @return {@code true} if this queue changed as a result of the call
// * @throws ClassCastException {@inheritDoc}
// * @throws NullPointerException {@inheritDoc}
// * @throws IllegalArgumentException {@inheritDoc}
// * @throws IllegalStateException {@inheritDoc}
// * @see #add(Object)
// */
// public boolean addAll(Collection<? extends E> c) {
// if (c == null)
// throw new NullPointerException();
// if (c == this)
// throw new IllegalArgumentException();
// final ReentrantLock lock = this.lock;
// lock.lock();
// try {
// boolean modified = false;
// for (E e : c)
// if (linkLast(e))
// modified = true;
// return modified;
// } finally {
// lock.unlock();
// }
// }
/**
* Returns an array containing all of the elements in this deque, in
* proper sequence (from first to last element).
*
* <p>The returned array will be "safe" in that no references to it are
* maintained by this deque. (In other words, this method must allocate
* a new array). The caller is thus free to modify the returned array.
*
* <p>This method acts as bridge between array-based and collection-based
* APIs.
*
* @return an array containing all of the elements in this deque
*/
public
Object
[]
toArray
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
Object
[]
a
=
new
Object
[
count
];
int
k
=
0
;
for
(
Node
<
E
>
p
=
first
;
p
!=
null
;
p
=
p
.
next
)
a
[
k
++]
=
p
.
item
;
return
a
;
}
finally
{
lock
.
unlock
();
}
}
/**
* Returns an array containing all of the elements in this deque, in
* proper sequence; the runtime type of the returned array is that of
* the specified array. If the deque fits in the specified array, it
* is returned therein. Otherwise, a new array is allocated with the
* runtime type of the specified array and the size of this deque.
*
* <p>If this deque fits in the specified array with room to spare
* (i.e., the array has more elements than this deque), the element in
* the array immediately following the end of the deque is set to
* {@code null}.
*
* <p>Like the {@link #toArray()} method, this method acts as bridge between
* array-based and collection-based APIs. Further, this method allows
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
* <p>Suppose {@code x} is a deque known to contain only strings.
* The following code can be used to dump the deque into a newly
* allocated array of {@code String}:
*
* <pre>
* String[] y = x.toArray(new String[0]);</pre>
*
* Note that {@code toArray(new Object[0])} is identical in function to
* {@code toArray()}.
*
* @param a the array into which the elements of the deque are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose
* @return an array containing all of the elements in this deque
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this deque
* @throws NullPointerException if the specified array is null
*/
@SuppressWarnings
(
"unchecked"
)
public
<
T
>
T
[]
toArray
(
T
[]
a
)
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
if
(
a
.
length
<
count
)
a
=
(
T
[])
java
.
lang
.
reflect
.
Array
.
newInstance
(
a
.
getClass
().
getComponentType
(),
count
);
int
k
=
0
;
for
(
Node
<
E
>
p
=
first
;
p
!=
null
;
p
=
p
.
next
)
a
[
k
++]
=
(
T
)
p
.
item
;
if
(
a
.
length
>
k
)
a
[
k
]
=
null
;
return
a
;
}
finally
{
lock
.
unlock
();
}
}
public
String
toString
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
Node
<
E
>
p
=
first
;
if
(
p
==
null
)
return
"[]"
;
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
'['
);
for
(;;)
{
E
e
=
p
.
item
;
sb
.
append
(
e
==
this
?
"(this Collection)"
:
e
);
p
=
p
.
next
;
if
(
p
==
null
)
return
sb
.
append
(
']'
).
toString
();
sb
.
append
(
','
).
append
(
' '
);
}
}
finally
{
lock
.
unlock
();
}
}
/**
* Atomically removes all of the elements from this deque.
* The deque will be empty after this call returns.
*/
public
void
clear
()
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
for
(
Node
<
E
>
f
=
first
;
f
!=
null
;
)
{
f
.
item
=
null
;
Node
<
E
>
n
=
f
.
next
;
f
.
prev
=
null
;
f
.
next
=
null
;
f
=
n
;
}
first
=
last
=
null
;
count
=
0
;
notFull
.
signalAll
();
}
finally
{
lock
.
unlock
();
}
}
/**
* Returns an iterator over the elements in this deque in proper sequence.
* The elements will be returned in order from first (head) to last (tail).
*
* <p>The returned iterator is a "weakly consistent" iterator that
* will never throw {@link java.util.ConcurrentModificationException
* ConcurrentModificationException}, and guarantees to traverse
* elements as they existed upon construction of the iterator, and
* may (but is not guaranteed to) reflect any modifications
* subsequent to construction.
*
* @return an iterator over the elements in this deque in proper sequence
*/
public
Iterator
<
E
>
iterator
()
{
return
new
Itr
();
}
/**
* Returns an iterator over the elements in this deque in reverse
* sequential order. The elements will be returned in order from
* last (tail) to first (head).
*
* <p>The returned iterator is a "weakly consistent" iterator that
* will never throw {@link java.util.ConcurrentModificationException
* ConcurrentModificationException}, and guarantees to traverse
* elements as they existed upon construction of the iterator, and
* may (but is not guaranteed to) reflect any modifications
* subsequent to construction.
*
* @return an iterator over the elements in this deque in reverse order
*/
public
Iterator
<
E
>
descendingIterator
()
{
return
new
DescendingItr
();
}
/**
* Base class for Iterators for LinkedBlockingDeque
*/
private
abstract
class
AbstractItr
implements
Iterator
<
E
>
{
/**
* The next node to return in next()
*/
Node
<
E
>
next
;
/**
* nextItem holds on to item fields because once we claim that
* an element exists in hasNext(), we must return item read
* under lock (in advance()) even if it was in the process of
* being removed when hasNext() was called.
*/
E
nextItem
;
/**
* Node returned by most recent call to next. Needed by remove.
* Reset to null if this element is deleted by a call to remove.
*/
private
Node
<
E
>
lastRet
;
abstract
Node
<
E
>
firstNode
();
abstract
Node
<
E
>
nextNode
(
Node
<
E
>
n
);
AbstractItr
()
{
// set to initial position
final
ReentrantLock
lock
=
LinkedBlockingDeque
.
this
.
lock
;
lock
.
lock
();
try
{
next
=
firstNode
();
nextItem
=
(
next
==
null
)
?
null
:
next
.
item
;
}
finally
{
lock
.
unlock
();
}
}
/**
* Returns the successor node of the given non-null, but
* possibly previously deleted, node.
*/
private
Node
<
E
>
succ
(
Node
<
E
>
n
)
{
// Chains of deleted nodes ending in null or self-links
// are possible if multiple interior nodes are removed.
for
(;;)
{
Node
<
E
>
s
=
nextNode
(
n
);
if
(
s
==
null
)
return
null
;
else
if
(
s
.
item
!=
null
)
return
s
;
else
if
(
s
==
n
)
return
firstNode
();
else
n
=
s
;
}
}
/**
* Advances next.
*/
void
advance
()
{
final
ReentrantLock
lock
=
LinkedBlockingDeque
.
this
.
lock
;
lock
.
lock
();
try
{
// assert next != null;
next
=
succ
(
next
);
nextItem
=
(
next
==
null
)
?
null
:
next
.
item
;
}
finally
{
lock
.
unlock
();
}
}
public
boolean
hasNext
()
{
return
next
!=
null
;
}
public
E
next
()
{
if
(
next
==
null
)
throw
new
NoSuchElementException
();
lastRet
=
next
;
E
x
=
nextItem
;
advance
();
return
x
;
}
public
void
remove
()
{
Node
<
E
>
n
=
lastRet
;
if
(
n
==
null
)
throw
new
IllegalStateException
();
lastRet
=
null
;
final
ReentrantLock
lock
=
LinkedBlockingDeque
.
this
.
lock
;
lock
.
lock
();
try
{
if
(
n
.
item
!=
null
)
unlink
(
n
);
}
finally
{
lock
.
unlock
();
}
}
}
/** Forward iterator */
private
class
Itr
extends
AbstractItr
{
Node
<
E
>
firstNode
()
{
return
first
;
}
Node
<
E
>
nextNode
(
Node
<
E
>
n
)
{
return
n
.
next
;
}
}
/** Descending iterator */
private
class
DescendingItr
extends
AbstractItr
{
Node
<
E
>
firstNode
()
{
return
last
;
}
Node
<
E
>
nextNode
(
Node
<
E
>
n
)
{
return
n
.
prev
;
}
}
/**
* Save the state of this deque to a stream (that is, serialize it).
*
* @serialData The capacity (int), followed by elements (each an
* {@code Object}) in the proper order, followed by a null
* @param s the stream
*/
private
void
writeObject
(
java
.
io
.
ObjectOutputStream
s
)
throws
java
.
io
.
IOException
{
final
ReentrantLock
lock
=
this
.
lock
;
lock
.
lock
();
try
{
// Write out capacity and any hidden stuff
s
.
defaultWriteObject
();
// Write out all elements in the proper order.
for
(
Node
<
E
>
p
=
first
;
p
!=
null
;
p
=
p
.
next
)
s
.
writeObject
(
p
.
item
);
// Use trailing null as sentinel
s
.
writeObject
(
null
);
}
finally
{
lock
.
unlock
();
}
}
/**
* Reconstitute this deque from a stream (that is,
* deserialize it).
* @param s the stream
*/
private
void
readObject
(
java
.
io
.
ObjectInputStream
s
)
throws
java
.
io
.
IOException
,
ClassNotFoundException
{
s
.
defaultReadObject
();
count
=
0
;
first
=
null
;
last
=
null
;
// Read in all elements and place in queue
for
(;;)
{
@SuppressWarnings
(
"unchecked"
)
E
item
=
(
E
)
s
.
readObject
();
if
(
item
==
null
)
break
;
add
(
item
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment