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