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   * @see ClassStack
32   */
33  public class ClassSet {
34  
35      private final Map<String, JavaClass> map = new HashMap<>();
36  
37  
38      public boolean add( final JavaClass clazz ) {
39          boolean result = false;
40          if (!map.containsKey(clazz.getClassName())) {
41              result = true;
42              map.put(clazz.getClassName(), clazz);
43          }
44          return result;
45      }
46  
47  
48      public void remove( final JavaClass clazz ) {
49          map.remove(clazz.getClassName());
50      }
51  
52  
53      public boolean empty() {
54          return map.isEmpty();
55      }
56  
57  
58      public JavaClass[] toArray() {
59          final Collection<JavaClass> values = map.values();
60          final JavaClasss.html#JavaClass">JavaClass[] classes = new JavaClass[values.size()];
61          values.toArray(classes);
62          return classes;
63      }
64  
65  
66      public String[] getClassNames() {
67          return map.keySet().toArray(new String[map.size()]);
68      }
69  }