1 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 22 abstract class Item implements IItem 23 { 24 26 27 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 getChildren () 40 { 41 return m_children.iterator (); 42 } 43 44 public final Iterator getChildren (final ItemComparator order) 45 { 46 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 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 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 96 97 protected static final class ItemMetadata implements IItemMetadata 98 { 99 public int getTypeID () 100 { 101 return m_typeID; 102 } 103 104 public String 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 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 } 132 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 146 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 158 159 private final List m_children; 160 161 } | Popular Tags |