View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.ds;
33  
34  import javax.annotation.Nullable;
35  import javax.annotation.ParametersAreNonnullByDefault;
36  
37  /**
38   * /**
39   * A simple stack implementation that does not support null elements.
40   * @author Zoltan Farkas
41   * @param <T>
42   */
43  @ParametersAreNonnullByDefault
44  public final class SimpleStack<T> extends SimpleStackNullSupport<T> {
45  
46    public SimpleStack(final int size) {
47      super(size);
48    }
49  
50    public SimpleStack() {
51    }
52  
53    /**
54     * take a look at the top of stack
55     * returns null if there is no element.
56     * @return Object
57     */
58    @Nullable
59    @Override
60    public T peek() {
61      if (top > 0) {
62        return elems[top - 1];
63      } else {
64        return null;
65      }
66    }
67  
68    @Nullable
69    public T peekAndPush(final T o) {
70      int t = top + 1;
71      ensureCapacity(t);
72      elems[top] = o;
73      T result;
74      if (top >= 0) {
75        result = elems[top - 1];
76      } else {
77        result = null;
78      }
79      top = t;
80      return result;
81    }
82  
83    @Nullable
84    public T pollLast() {
85      if (size() <= 0) {
86        return null;
87      } else {
88        return pop();
89      }
90    }
91  
92    @Override
93    public boolean remove(final Object o) {
94      for (int i = 0, l = top; i < l; i++) {
95        if (o.equals(elems[i])) {
96          fastRemove(i);
97          return true;
98        }
99      }
100     return false;
101   }
102 
103   /**
104    * Not implemented, can be overwritten.
105    */
106   @Override
107   public int indexOf(final Object o) {
108     for (int i = 0, l = top; i < l; i++) {
109       if (o.equals(elems[i])) {
110         return i;
111       }
112     }
113     return -1;
114   }
115 
116   /**
117    * Not implemented, can be overwritten.
118    */
119   @Override
120   public int lastIndexOf(final Object o) {
121     for (int i = size() - 1; i >=  0; i--) {
122       if (o.equals(elems[i])) {
123         return i;
124       }
125     }
126     return -1;
127   }
128 
129   @Override
130   public boolean contains(final Object o) {
131     for (int i = 0; i < top; i++) {
132       if (elems[i].equals(o)) {
133         return true;
134       }
135     }
136     return false;
137   }
138 
139 
140   public void pushNull() {
141     throw new UnsupportedOperationException();
142   }
143 
144 
145 }