• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
12 import java.util.Arrays;
13 import java.util.Iterator;
14 import java.util.List;
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 
getChildCount()29     public final int getChildCount ()
30     {
31         return m_children.size ();
32     }
33 
getParent()34     public final IItem getParent ()
35     {
36         return m_parent;
37     }
38 
getChildren()39     public final Iterator getChildren ()
40     {
41         return m_children.iterator ();
42     }
43 
getChildren(final ItemComparator order)44     public final Iterator 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 
getAttribute(final int attributeID, final int unitsID)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 
getAggregate(final int type)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 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     {
getTypeID()99         public int getTypeID ()
100         {
101             return m_typeID;
102         }
103 
getTypeName()104         public String getTypeName ()
105         {
106             return m_typeName;
107         }
108 
getAttributeIDs()109         public long getAttributeIDs ()
110         {
111             return m_attributeIDs;
112         }
113 
ItemMetadata(final int typeID, final String typeName, final long attributeIDs)114         ItemMetadata (final int typeID, final String 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 m_typeName;
128         private final long m_attributeIDs;
129 
130     } // end of nested class
131 
132 
addChild(final IItem item)133     protected void addChild (final IItem item)
134     {
135         if (item == null) throw new IllegalArgumentException ("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 
Item(final IItem parent)147     Item (final IItem parent)
148     {
149         m_parent = parent;
150         m_children = new ArrayList ();
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 m_children;
160 
161 } // end of class
162 // ----------------------------------------------------------------------------