001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License. 
016     *
017     */
018    package org.apache.bcel.util;
019    
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.Map;
023    import org.apache.bcel.classfile.JavaClass;
024    
025    /** 
026     * Utility class implementing a (typesafe) set of JavaClass objects.
027     * Since JavaClass has no equals() method, the name of the class is
028     * used for comparison.
029     *
030     * @version $Id: ClassSet.java 1149459 2011-07-22 04:34:27Z dbrosius $
031     * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A> 
032     * @see ClassStack
033     */
034    public class ClassSet implements java.io.Serializable {
035    
036        private static final long serialVersionUID = -7476907380350035254L;
037        private Map<String, JavaClass> _map = new HashMap<String, JavaClass>();
038    
039    
040        public boolean add( JavaClass clazz ) {
041            boolean result = false;
042            if (!_map.containsKey(clazz.getClassName())) {
043                result = true;
044                _map.put(clazz.getClassName(), clazz);
045            }
046            return result;
047        }
048    
049    
050        public void remove( JavaClass clazz ) {
051            _map.remove(clazz.getClassName());
052        }
053    
054    
055        public boolean empty() {
056            return _map.isEmpty();
057        }
058    
059    
060        public JavaClass[] toArray() {
061            Collection<JavaClass> values = _map.values();
062            JavaClass[] classes = new JavaClass[values.size()];
063            values.toArray(classes);
064            return classes;
065        }
066    
067    
068        public String[] getClassNames() {
069            return _map.keySet().toArray(new String[_map.keySet().size()]);
070        }
071    }