All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.royale.utils.FastStack Maven / Gradle / Ivy

There is a newer version: 0.9.10
Show newest version
/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.royale.utils;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Stack implementation based on ArrayList instead of a Vector.  Improves performance when threading is not needed.
 * Also allows for decoration of values coming back from the stack
 *
 * @param  The type of the stack elements.
 */
public class FastStack extends ArrayList {
	
	/**
	 * Decorator used to change values as they come off this stack
	 *
	 * @param  The type of the stack elemetns.
	 */
	public interface IFastStackDecorator {
		/**
		 * Decorates the value as it is popped or peeked from the stack
		 * @param e the value to decorate.  Value can be null
		 * @return the decorated value
		 */
		public E decorate(E e);
	}
	
	
	private static final long serialVersionUID = 7491447816791648090L;
	private IFastStackDecorator decorator;

	public FastStack() {
		super(3);
	}
	
	public FastStack(int size) {
		super(size);
	}
	
	public FastStack(Collection c) {
		super(c);
	}
	
	public void setStackDecorator(IFastStackDecorator decorator) {
		this.decorator = decorator;
	}
	
	/**
	 * Pushes the entry onto the end of the stack
	 * @param entry the entry to add
	 */
	public void push(E entry) {
		add(entry);
	}
	
	/**
	 * Pops the entry from the end of the stack
	 * @return the entry
	 */
	public E pop() {
		E retVal = null;
		int size = size();
		if(size > 0)
			retVal = remove(size - 1);
		if(decorator != null) 
			return decorator.decorate(retVal);
		return retVal;
	}
	
	/**
	 * Gets the entry at the head of the stack
	 * @return head of the stack
	 */
	public E peek() {
		E retVal = null;
		int size = size();
		if(size > 0)
			retVal = get(size - 1);
		if(decorator != null) 
			return decorator.decorate(retVal);
		return retVal;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		FastStack st = new FastStack((ArrayList)super.clone());
		return st;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy