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