1 /* 2 * Copyright 2006 Edward Kuns 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * $Id: SafeStack.java 0000 2006-12-21 22:00:00Z ekuns $ 17 */ 18 package org.exolab.castor.util; 19 20 import java.util.Stack; 21 22 /** 23 * The {@link Stack#search} method of {@link Stack} can throw a 24 * ClassCastException if the items on the stack are not all the same type. We 25 * override that method so it compares on identity and not using 26 * <code>equals()</code>. 27 * 28 * @author <a href="mailto:edward.kuns@aspect.com">Edward Kuns</a> 29 * @version $Revision: 0000 $ $Date: $ 30 */ 31 public class SafeStack<E> extends Stack<E> { 32 /** 33 * Serial Version UID. 34 */ 35 private static final long serialVersionUID = 4964881847051572321L; 36 37 /** 38 * {@inheritDoc} 39 * <br/> 40 * Searches for the given Object in the stack and returns its position 41 * relative to the top of the Stack (ie the number of calls to #pop() 42 * before the object is returned by #pop()) 43 */ 44 public synchronized int search(Object object) { 45 for (int i = 0; i < size(); i++) { 46 if (object == get(i)) { 47 return i + 1; 48 } 49 } 50 return -1; 51 } 52 }