View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.util;
    21
    22  import java.io.Serializable;
    23  import java.util.Arrays;
    24
    25  /**
    26   * A unbounded stack.
    27   *
    28   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    29   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    30   */
    31  public class Stack implements Serializable {
    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          items = new Object[DEFAULT_CAPACITY];
    45      }
    46
    47      /**
    48       * Clears this stack.
    49       */
    50      public void clear() {
    51          Arrays.fill(items, null);
    52          size = 0;
    53      }
    54
    55      /**
    56       * Pops from this stack.
    57       *
    58       * @return <code>null</code>, if this stack is empty or the element is
    59       *         really <code>null</code>.
    60       */
    61      public Object pop() {
    62          if (size == 0) {
    63              return null;
    64          }
    65
    66          int pos = size - 1;
    67          Object ret = items[pos];
    68          items[pos] = null;
    69          size--;
    70
    71          return ret;
    72      }
    73
    74      /**
    75       * Push into this stack.
    76       */
    77      public void push(Object obj) {
    78          if (size == items.length) {
    79              // expand queue
    80              final int oldLen = items.length;
    81              Object[] tmp = new Object[oldLen * 2];
    82              System.arraycopy(items, 0, tmp, 0, size);
    83              items = tmp;
    84          }
    85
    86          items[size] = obj;
    87          size++;
    88      }
    89
    90      public void remove(Object o) {
    91          for (int i = size - 1; i >= 0; i--) {
    92              if (items[i] == o) {
    93                  System.arraycopy(items, i + 1, items, i, size - i - 1);
    94                  items[size - 1] = null;
    95                  size--;
    96                  break;
    97              }
    98          }
    99      }
    100
    101     /**
    102      * Returns the first element of the stack.
    103      *
    104      * @return <code>null</code>, if the stack is empty, or the element is
    105      *         really <code>null</code>.
    106      */
    107     public Object first() {
    108         if (size == 0) {
    109             return null;
    110         }
    111
    112         return items[size - 1];
    113     }
    114
    115     public Object last() {
    116         if (size == 0) {
    117             return null;
    118         }
    119
    120         return items[0];
    121     }
    122
    123     /**
    124      * Returns <code>true</code> if the stack is empty.
    125      */
    126     public boolean isEmpty() {
    127         return (size == 0);
    128     }
    129
    130     /**
    131      * Returns the number of elements in the stack.
    132      */
    133     public int size() {
    134         return size;
    135     }
    136 }