KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > vladium > emma > report > Item


1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: Item.java,v 1.1.1.1.2.1 2004/06/20 20:14:39 vlad_r Exp $
8  */

9 package com.vladium.emma.report;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import com.vladium.util.asserts.$assert;
17
18 // ----------------------------------------------------------------------------
19
/**
20  * @author Vlad Roubtsov, (C) 2003
21  */

22 abstract class Item implements IItem
23 {
24     // public: ................................................................
25

26     
27     // IItem:
28

29     public final int getChildCount ()
30     {
31         return m_children.size ();
32     }
33
34     public final IItem getParent ()
35     {
36         return m_parent;
37     }
38
39     public final Iterator JavaDoc getChildren ()
40     {
41         return m_children.iterator ();
42     }
43     
44     public final Iterator JavaDoc getChildren (final ItemComparator /* IItem */ order)
45     {
46         // TODO: soft caching keyed off 'order'
47

48         if (order == null)
49             return getChildren ();
50         else
51         {
52             final IItem [] items = new IItem [m_children.size ()];
53             m_children.toArray (items);
54             
55             Arrays.sort (items, order);
56             
57             return Arrays.asList (items).iterator ();
58         }
59     }
60     
61     public final IItemAttribute getAttribute (final int attributeID, final int unitsID)
62     {
63         //if ($assert.ENABLED) $assert.ASSERT ((attributeID & getMetadata ().getAttributeIDs ()) != 0, "invalid attribute ID [" + attributeID + "] for type [" + getMetadata ().getTypeID () + "]");
64

65         if ((getMetadata ().getAttributeIDs () & (1 << attributeID)) == 0)
66             return null;
67         else
68             return IItemAttribute.Factory.getAttribute (attributeID, unitsID);
69     }
70     
71     public int getAggregate (final int type)
72     {
73         final int [] aggregates = m_aggregates;
74         int value = aggregates [type];
75         
76         if (value < 0)
77         {
78             // don't fault aggregate types all at once since there are
79
// plenty of exceptions to the additive roll up rule:
80

81             value = 0;
82             for (Iterator JavaDoc children = m_children.iterator (); children.hasNext (); )
83             {
84                 value += ((IItem) children.next ()).getAggregate (type);
85             }
86             aggregates [type] = value;
87             
88             return value;
89         }
90         
91         return value;
92     }
93
94     // protected: .............................................................
95

96     
97     protected static final class ItemMetadata implements IItemMetadata
98     {
99         public int getTypeID ()
100         {
101             return m_typeID;
102         }
103         
104         public String JavaDoc getTypeName ()
105         {
106             return m_typeName;
107         }
108         
109         public long getAttributeIDs ()
110         {
111             return m_attributeIDs;
112         }
113         
114         ItemMetadata (final int typeID, final String JavaDoc typeName, final long attributeIDs)
115         {
116             if ($assert.ENABLED) $assert.ASSERT (typeID >= TYPE_ID_ALL && typeID <= TYPE_ID_METHOD, "invalid type ID: " + typeID);
117             if ($assert.ENABLED) $assert.ASSERT (typeName != null, "typeName = null");
118             
119             
120             m_typeID = typeID;
121             m_typeName = typeName;
122             m_attributeIDs = attributeIDs;
123         }
124
125
126         private final int m_typeID;
127         private final String JavaDoc m_typeName;
128         private final long m_attributeIDs;
129         
130     } // end of nested class
131

132
133     protected void addChild (final IItem item)
134     {
135         if (item == null) throw new IllegalArgumentException JavaDoc ("null input: item");
136         
137         m_children.add (item);
138     }
139     
140     
141     protected final IItem m_parent;
142     protected final int [] m_aggregates;
143
144     // package: ...............................................................
145

146
147     Item (final IItem parent)
148     {
149         m_parent = parent;
150         m_children = new ArrayList JavaDoc ();
151         
152         m_aggregates = new int [NUM_OF_AGGREGATES];
153         for (int i = 0; i < m_aggregates.length; ++ i) m_aggregates [i] = -1;
154     }
155     
156     // private: ...............................................................
157

158     
159     private final List JavaDoc m_children;
160
161 } // end of class
162
// ----------------------------------------------------------------------------
Popular Tags