KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > ClassAttributes


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle;
26
27 import classycle.graph.NameAttributes;
28
29 /**
30  * Immutable class holding the attributes of a class vertex. They are
31  * <ul><li>fully-qualified class name
32  * <li>type (interface, abstract, concrete, unknown)
33  * <li>flag <tt>innerClass</tt>
34  * <li>size of the class file
35  * </ul>
36  *
37  * @author Franz-Josef Elmer
38  */

39 public class ClassAttributes extends NameAttributes {
40   /** Type constant. */
41   public static final String JavaDoc INTERFACE = "interface",
42                       ABSTRACT_CLASS = "abstract class",
43                       CLASS = "class",
44                       UNKNOWN = "unknown external class";
45
46   private final String JavaDoc _type;
47   private final boolean _innerClass;
48   private final int _size;
49
50   /**
51    * Creates an instance based on the specified name, type, and size.
52    * The innerclass flag will be set if the name contains a '$' character.
53    * @param name Fully-qualified class name.
54    * @param type Type.
55    * @param size Size.
56    */

57   public ClassAttributes(String JavaDoc name, String JavaDoc type, int size) {
58     super(name);
59     _type = type;
60     _innerClass = name != null && name.indexOf('$') > 0;
61     _size = size;
62   }
63   
64   /**
65    * Creates an instance of the type {@link #INTERFACE}.
66    * @param name Fully-qualified class name.
67    * @param size Size of the class file.
68    * @return a new instance.
69    */

70   public static ClassAttributes createInterface(String JavaDoc name, int size) {
71     return new ClassAttributes(name, INTERFACE, size);
72   }
73
74   /**
75    * Creates an instance of the type {@link #ABSTRACT_CLASS}.
76    * @param name Fully-qualified class name.
77    * @param size Size of the class file.
78    * @return a new instance.
79    */

80   public static ClassAttributes createAbstractClass(String JavaDoc name, int size) {
81     return new ClassAttributes(name, ABSTRACT_CLASS, size);
82   }
83
84   /**
85    * Creates an instance of the type {@link #CLASS}.
86    * @param name Fully-qualified class name.
87    * @param size Size of the class file.
88    * @return a new instance.
89    */

90   public static ClassAttributes createClass(String JavaDoc name, int size) {
91     return new ClassAttributes(name, CLASS, size);
92   }
93
94   /**
95    * Creates an instance of the type {@link #UNKNOWN}.
96    * @param name Fully-qualified class name.
97    * @param size Size of the class file.
98    * @return a new instance.
99    */

100   public static ClassAttributes createUnknownClass(String JavaDoc name, int size) {
101     return new ClassAttributes(name, UNKNOWN, size);
102   }
103
104   /**
105    * Returns the class type.
106    * @return either {@link #INTERFACE}, {@link #ABSTRACT_CLASS},
107    * {@link #CLASS}, or {@link #UNKNOWN}.
108    */

109   public String JavaDoc getType() {
110     return _type;
111   }
112
113   /** Returns <tt>true</tt> in the case of an inner class. */
114   public boolean isInnerClass() {
115     return _innerClass;
116   }
117
118   /** Returns the size of the class file in bytes. */
119   public int getSize() {
120     return _size;
121   }
122
123   /** Returns the attributes as a string for pretty printing. */
124   public String JavaDoc toString() {
125     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(_innerClass ? "inner " : "");
126     buffer.append(_type).append(' ').append(getName());
127     if (_size > 0) {
128       buffer.append(" (").append(_size).append(" bytes)");
129     }
130     return new String JavaDoc(buffer);
131   }
132 } //class
Popular Tags