1 wbeebee 1.1.2.1 // QuadsCounter.java, created Mon Jan 22 by wbeebee 2 wbeebee 1.1.2.1 // Copyright (C) 2001 Wes Beebee <wbeebee@mit.edu> 3 wbeebee 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 wbeebee 1.1.2.1 package harpoon.Analysis.Quads; 5 wbeebee 1.1.2.1 6 wbeebee 1.1.2.1 import harpoon.ClassFile.HCode; 7 wbeebee 1.1.2.1 import harpoon.ClassFile.HCodeFactory; 8 wbeebee 1.1.2.1 import harpoon.ClassFile.HMethod; 9 wbeebee 1.1.2.1 10 wbeebee 1.1.2.1 /** 11 wbeebee 1.1.2.1 * <code>QuadCounter</code> is an <code>harpoon.ClassFile.HCodeFactory</code> 12 wbeebee 1.1.2.1 * which counts the number of quads that have been <code>convert</code>ed 13 wbeebee 1.1.2.1 * thus far. <code>count()</code> returns the current count. 14 wbeebee 1.1.2.1 * Use in conjunction with a <code>harpoon.ClassFile.CachingCodeFactory</code> 15 wbeebee 1.1.2.1 * to create an accurate count: 16 wbeebee 1.1.2.1 * <code>hcf = new CachingCodeFactory(new QuadCounter(hcf));</code> 17 wbeebee 1.1.2.1 * 18 cananian 1.1.2.4 * @author Wes Beebee <wbeebee@mit.edu> 19 wbeebee 1.3 * @version $Id: QuadCounter.java,v 1.3 2002/08/14 20:50:11 wbeebee Exp $ 20 wbeebee 1.1.2.1 */ 21 wbeebee 1.1.2.1 22 salcianu 1.1.2.2 public class QuadCounter implements HCodeFactory, java.io.Serializable { 23 wbeebee 1.1.2.1 private long numQuads; 24 wbeebee 1.1.2.1 private HCodeFactory parent; 25 wbeebee 1.1.2.1 26 wbeebee 1.1.2.1 /** Creates a new <code>QuadCounter</code> with <code>hcf</code> as its 27 wbeebee 1.1.2.1 * parent. 28 wbeebee 1.1.2.1 */ 29 wbeebee 1.1.2.1 public QuadCounter(HCodeFactory hcf) { 30 wbeebee 1.1.2.1 numQuads = 0; 31 wbeebee 1.1.2.1 parent = hcf; 32 wbeebee 1.1.2.1 } 33 wbeebee 1.1.2.1 34 wbeebee 1.1.2.1 /** Converts <code>HMethod</code> <code>m</code> into an 35 wbeebee 1.1.2.1 * <code>HCode</code> and counts the number of <code>Quad</code>s 36 wbeebee 1.1.2.1 * returned. 37 wbeebee 1.1.2.1 */ 38 wbeebee 1.1.2.1 public HCode convert(HMethod m) { 39 wbeebee 1.1.2.1 HCode hc = parent.convert(m); 40 wbeebee 1.1.2.1 if (hc != null) { 41 wbeebee 1.3 numQuads += hc.getElementsL().size(); 42 wbeebee 1.1.2.1 } 43 wbeebee 1.1.2.1 return hc; 44 wbeebee 1.1.2.1 } 45 wbeebee 1.1.2.1 46 wbeebee 1.1.2.1 /** Gets the code name of this <code>HCodeFactory</code>. */ 47 wbeebee 1.1.2.1 48 wbeebee 1.1.2.1 public String getCodeName() { 49 wbeebee 1.1.2.1 return parent.getCodeName(); 50 wbeebee 1.1.2.1 } 51 wbeebee 1.1.2.1 52 wbeebee 1.1.2.1 /** Clears <code>HMethod</code> <code>m</code> from the cache. 53 wbeebee 1.1.2.1 * Note: this does not remove the <code>Quad</code>s from the total count. 54 wbeebee 1.1.2.1 */ 55 wbeebee 1.1.2.1 public void clear(HMethod m) { 56 wbeebee 1.1.2.1 parent.clear(m); 57 wbeebee 1.1.2.1 } 58 wbeebee 1.1.2.1 59 wbeebee 1.1.2.1 /** Returns the current count of the number of <code>Quad</code>s 60 wbeebee 1.1.2.1 * converted. 61 wbeebee 1.1.2.1 */ 62 wbeebee 1.1.2.1 63 wbeebee 1.1.2.1 public long count() { 64 wbeebee 1.1.2.1 return numQuads; 65 wbeebee 1.1.2.1 } 66 wbeebee 1.1.2.1 67 wbeebee 1.1.2.1 /** Print a textual representation of this <code>QuadCounter</code>. */ 68 wbeebee 1.1.2.1 69 wbeebee 1.1.2.1 public String toString() { 70 wbeebee 1.1.2.1 return numQuads + " quads"; 71 wbeebee 1.1.2.1 } 72 wbeebee 1.1.2.1 73 wbeebee 1.1.2.1 74 cananian 1.2 }