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 */
017package org.apache.bcel.util;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.bcel.classfile.JavaClass;
023import org.apache.commons.lang3.ArrayUtils;
024
025/**
026 * Utility class implementing a (type-safe) set of JavaClass objects. Since JavaClass has no equals() method, the name of the class is used for comparison.
027 *
028 * @see ClassStack
029 */
030public class ClassSet {
031
032    private final Map<String, JavaClass> map = new HashMap<>();
033
034    public boolean add(final JavaClass clazz) {
035        return map.putIfAbsent(clazz.getClassName(), clazz) != null;
036    }
037
038    public boolean empty() {
039        return map.isEmpty();
040    }
041
042    public String[] getClassNames() {
043        return map.keySet().toArray(ArrayUtils.EMPTY_STRING_ARRAY);
044    }
045
046    public void remove(final JavaClass clazz) {
047        map.remove(clazz.getClassName());
048    }
049
050    public JavaClass[] toArray() {
051        return map.values().toArray(JavaClass.EMPTY_ARRAY);
052    }
053}