1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.util;
  23 
  24 import java.util.Collection;
  25 import java.util.HashMap;
  26 import java.util.Map;
  27 
  28 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  29 
  30 /**
  31  * Utility class implementing a (typesafe) set of JavaClass objects.
  32  * Since JavaClass has no equals() method, the name of the class is
  33  * used for comparison.
  34  *
  35  * @version $Id$
  36  * @see ClassStack
  37  */
  38 public class ClassSet {
  39 
  40     private final Map<String, JavaClass> map = new HashMap<>();
  41 
  42 
  43     public boolean add( final JavaClass clazz ) {
  44         boolean result = false;
  45         if (!map.containsKey(clazz.getClassName())) {
  46             result = true;
  47             map.put(clazz.getClassName(), clazz);
  48         }
  49         return result;
  50     }
  51 
  52 
  53     public void remove( final JavaClass clazz ) {
  54         map.remove(clazz.getClassName());
  55     }
  56 
  57 
  58     public boolean empty() {
  59         return map.isEmpty();
  60     }
  61 
  62 
  63     public JavaClass[] toArray() {
  64         final Collection<JavaClass> values = map.values();
  65         final JavaClass[] classes = new JavaClass[values.size()];
  66         values.toArray(classes);
  67         return classes;
  68     }
  69 
  70 
  71     public String[] getClassNames() {
  72         return map.keySet().toArray(new String[map.size()]);
  73     }
  74 }