Coverage Report - org.simpleframework.xml.stream.Stack
 
Classes in this File Line Coverage Branch Coverage Complexity
Stack
81%
13/16
50%
3/6
2.2
 
 1  
 /*
 2  
  * Stack.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
 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 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.stream;
 20  
 
 21  
 import java.util.ArrayList;
 22  
 
 23  
 /**
 24  
  * The <code>Stack</code> object is used to provide a lightweight 
 25  
  * stack implementation. To ensure top performance this stack is not
 26  
  * synchronized and keeps track of elements using an array list. 
 27  
  * A null from either a <code>pop</code> or <code>top</code> means
 28  
  * that the stack is empty. This allows the stack to be peeked at
 29  
  * even if it has not been populated with anything yet.
 30  
  *
 31  
  * @author Niall Gallagher
 32  
  */ 
 33  
 class Stack<T> extends ArrayList<T> {
 34  
 
 35  
    /**
 36  
     * Constructor for the <code>Stack</code> object. This is used 
 37  
     * to create a stack that can be used to keep track of values
 38  
     * in a first in last out manner. Typically this is used to 
 39  
     * determine if an XML element is in or out of context.
 40  
     * 
 41  
     * @param size this is the initial size of the stack to use
 42  
     */         
 43  
    public Stack(int size) {
 44  148143
       super(size);
 45  148143
    }
 46  
 
 47  
    /**
 48  
     * This is used to remove the element from the top of this 
 49  
     * stack. If the stack is empty then this will return null, as
 50  
     * such it is not advisable to push null elements on the stack.
 51  
     *
 52  
     * @return this returns the node element the top of the stack
 53  
     */ 
 54  
    public T pop() {
 55  3532292
       int size = size();
 56  
       
 57  3532292
       if(size <= 0) {
 58  0
          return null;               
 59  
       }           
 60  3532292
       return remove(size - 1);
 61  
    }
 62  
    
 63  
    /**
 64  
     * This is used to peek at the element from the top of this 
 65  
     * stack. If the stack is empty then this will return null, as
 66  
     * such it is not advisable to push null elements on the stack.
 67  
     *
 68  
     * @return this returns the node element the top of the stack
 69  
     */  
 70  
    public T top() {
 71  9085852
       int size = size();
 72  
       
 73  9085852
       if(size <= 0) {
 74  0
          return null;              
 75  
       }           
 76  9085852
       return get(size - 1);
 77  
    }
 78  
    
 79  
    /**
 80  
     * This is used to acquire the node from the bottom of the stack.
 81  
     * If the stack is empty then this will return null, as such it
 82  
     * is not advisable to push null elements on the stack.
 83  
     *
 84  
     * @return this returns the element from the bottom of the stack
 85  
     */ 
 86  
    public T bottom() {
 87  13402
       int size = size();
 88  
       
 89  13402
       if(size <= 0) {
 90  0
          return null;              
 91  
       }           
 92  13402
       return get(0);           
 93  
    }
 94  
    
 95  
    /**
 96  
     * This method is used to add an element to the top of the stack. 
 97  
     * Although it is possible to add a null element to the stack it 
 98  
     * is not advisable, as null is returned when the stack is empty.
 99  
     *
 100  
     * @param value this is the element to add to the stack
 101  
     * 
 102  
     * @return this returns the actual node that has just been added
 103  
     */ 
 104  
    public T push(T value) {
 105  3680623
       add(value);
 106  3680623
       return value;
 107  
    }
 108  
 }