ClassSet xref

View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.util;
    18
    19  import java.util.Collection;
    20  import java.util.HashMap;
    21  import java.util.Map;
    22  import org.apache.bcel.classfile.JavaClass;
    23
    24  /***
    25   * Utility class implementing a (typesafe) set of JavaClass objects.
    26   * Since JavaClass has no equals() method, the name of the class is
    27   * used for comparison.
    28   *
    29   * @version $Id: ClassSet.java 386056 2006-03-15 11:31:56Z tcurdt $
    30   * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    31   * @see ClassStack
    32   */
    33  public class ClassSet implements java.io.Serializable {
    34
    35      private Map _map = new HashMap();
    36
    37
    38      public boolean add( 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( 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          Collection values = _map.values();
    60          JavaClass[] classes = new JavaClass[values.size()];
    61          values.toArray(classes);
    62          return classes;
    63      }
    64
    65
    66      public String[] getClassNames() {
    67          return (String[]) _map.keySet().toArray(new String[_map.keySet().size()]);
    68      }
    69  }