View Javadoc

1   /*
2    *   @(#) $Id: Stack.java 210062 2005-07-11 03:52:38Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.util;
20  
21  import java.io.Serializable;
22  import java.util.Arrays;
23  
24  /***
25   * A unbounded stack.
26   * 
27   * @author Trustin Lee (trustin@apache.org)
28   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
29   */
30  public class Stack implements Serializable
31  {
32      private static final long serialVersionUID = 3546919169401434168L;
33  
34  	private static final int DEFAULT_CAPACITY = 4;
35  
36      private Object[] items;
37  
38      private int size = 0;
39  
40      /***
41       * Construct a new, empty stack.
42       */
43      public Stack()
44      {
45          items = new Object[ DEFAULT_CAPACITY ];
46      }
47  
48      /***
49       * Clears this stack.
50       */
51      public void clear()
52      {
53          Arrays.fill( items, null );
54          size = 0;
55      }
56  
57      /***
58       * Pops from this stack.
59       * 
60       * @return <code>null</code>, if this stack is empty or the element is
61       *         really <code>null</code>.
62       */
63      public Object pop()
64      {
65          if( size == 0 )
66          {
67              return null;
68          }
69  
70          int pos = size - 1;
71          Object ret = items[ pos ];
72          items[ pos ] = null;
73          size--;
74  
75          return ret;
76      }
77  
78      /***
79       * Push into this stack.
80       */
81      public void push( Object obj )
82      {
83          if( size == items.length )
84          {
85              // expand queue
86              final int oldLen = items.length;
87              Object[] tmp = new Object[ oldLen * 2 ];
88              System.arraycopy( items, 0, tmp, 0, size );
89              items = tmp;
90          }
91  
92          items[ size ] = obj;
93          size++;
94      }
95  
96      public void remove( Object o )
97      {
98          for( int i = size - 1; i >= 0; i-- )
99          {
100             if( items[ i ] == o )
101             {
102                 System.arraycopy( items, i + 1, items, i, size - i - 1 );
103                 items[ size - 1 ] = null;
104                 size--;
105                 break;
106             }
107         }
108     }
109 
110     /***
111      * Returns the first element of the stack.
112      * 
113      * @return <code>null</code>, if the stack is empty, or the element is
114      *         really <code>null</code>.
115      */
116     public Object first()
117     {
118         if( size == 0 )
119         {
120             return null;
121         }
122 
123         return items[ size - 1 ];
124     }
125 
126     public Object last()
127     {
128         if( size == 0 )
129         {
130             return null;
131         }
132 
133         return items[ 0 ];
134     }
135 
136     /***
137      * Returns <code>true</code> if the stack is empty.
138      */
139     public boolean isEmpty()
140     {
141         return ( size == 0 );
142     }
143 
144     /***
145      * Returns the number of elements in the stack.
146      */
147     public int size()
148     {
149         return size;
150     }
151 }