From: Marek P. <ma...@us...> - 2002-03-04 23:58:07
|
Update of /cvsroot/javaprofiler/jpiimpl/net/sourceforge/javaprofiler/jpiimpl/commun In directory usw-pr-cvs1:/tmp/cvs-serv14397/net/sourceforge/javaprofiler/jpiimpl/commun Modified Files: IProf.java Log Message: calltree iterator added Index: IProf.java =================================================================== RCS file: /cvsroot/javaprofiler/jpiimpl/net/sourceforge/javaprofiler/jpiimpl/commun/IProf.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -r1.11 -r1.12 *** IProf.java 28 Feb 2002 11:49:26 -0000 1.11 --- IProf.java 4 Mar 2002 23:58:04 -0000 1.12 *************** *** 1592,1599 **** ** @param optionalArg optional argument (depends on type of object) ** ! ** @return iterator for objects having statistic data ** ** @see getAllThruIterator(), getChangedThruIterator(), ! ** StatDataIterator, getChangedOrAll() */ private Iterator getChangedOrAllThruIterator( int funcType, --- 1592,1599 ---- ** @param optionalArg optional argument (depends on type of object) ** ! ** @return iterator for objects having statistic data (sID objects) ** ** @see getAllThruIterator(), getChangedThruIterator(), ! ** StatDataIterator, getChangedOrAll(), sID */ private Iterator getChangedOrAllThruIterator( int funcType, *************** *** 2071,2075 **** public LinkedList children; ! /// number of children (always zero) public int numOfChildren; --- 2071,2075 ---- public LinkedList children; ! /// number of children (used only for getCallTreeThruIterator()) public int numOfChildren; *************** *** 2114,2131 **** } ! /** Get calltree. This method returns the whole calltree ! ** of methods for given thread. ** ** @param threadObjId thread's object ID ** ! ** @return == null (calltree is empty); ! ** != null (a reference to the root of calltree) */ - public synchronized sCallTree getCallTree( int threadObjId) - throws COMMUN_Exception, BAD_OBJ_ID_Exception, UNKNOWN_Exception { ! _buf.clear(); _buf.putInt( F_GET_CALLTREE); --- 2114,2134 ---- } ! /** Get calltree to binary buffer. This method only reads ! ** the whole calltree to binary buffer. It is used internally ! ** by getCallTree() and getCallTreeThruIterator() methods. ** ** @param threadObjId thread's object ID ** ! ** @return true (calltree is not empty); ! ** false (calltree is empty) ! ** ! ** @see getCallTree(), getCallTreeThruIterator() */ ! ! private boolean getCallTreeAll( int threadObjId) throws COMMUN_Exception, BAD_OBJ_ID_Exception, UNKNOWN_Exception { ! _buf.clear(); _buf.putInt( F_GET_CALLTREE); *************** *** 2134,2155 **** _commun.write( _buf); if( _commun.hasFailed()) throw new COMMUN_Exception(); ! _commun.read( _buf); if( _commun.hasFailed()) throw new COMMUN_Exception(); ! int pos = 0; ! ! int rc = _buf.getInt( pos); ! pos += 4; if( rc != RC_OK) { ! if( rc == RC_BAD_OBJ_ID) throw new BAD_OBJ_ID_Exception(); ! throw new UNKNOWN_Exception(); } ! ! if( _buf.getSize() == 4) return null; sCallTree parent = null; --- 2137,2182 ---- _commun.write( _buf); if( _commun.hasFailed()) throw new COMMUN_Exception(); ! _commun.read( _buf); if( _commun.hasFailed()) throw new COMMUN_Exception(); ! int rc = _buf.getInt( 0); if( rc != RC_OK) { ! if( rc == RC_BAD_OBJ_ID) throw new BAD_OBJ_ID_Exception(); ! throw new UNKNOWN_Exception(); } ! ! return ( _buf.getSize() != 4); ! } ! ! /** Get calltree. This method returns the whole calltree ! ** of methods for given thread. ! ** ! ** @param threadObjId thread's object ID ! ** ! ** @return == null (calltree is empty); ! ** != null (a reference to the root of calltree) ! ** ! ** @see getCallTreeAll(), getCallTreeThruIterator() */ ! ! public synchronized sCallTree getCallTree( int threadObjId) ! ! throws COMMUN_Exception, ! BAD_OBJ_ID_Exception, ! UNKNOWN_Exception { ! ! try { ! ! if( !getCallTreeAll( threadObjId)) return null; ! } ! catch( COMMUN_Exception e) { throw e;} ! catch( BAD_OBJ_ID_Exception e) { throw e;} ! catch( UNKNOWN_Exception e) { throw e;} ! ! int pos = 4; sCallTree parent = null; *************** *** 2193,2196 **** --- 2220,2341 ---- return root; + } + + /** CallTree iterator. This class is an implementation + ** of general Iterator interface. It can be used to gain + ** items of calltree one after one another (eg. when + ** we need to build our own calltree and don't want to + ** use IProf's internal one. remove() method is not + ** implemented. + ** + ** @see Iterator, getCallTreeThruIterator() */ + + private class CallTreeIterator implements Iterator { + + /// internal buffer with binary data + private Buffer _dataBuf; + + /** indication whether still use same object + ** as output object or not */ + + private boolean _sameObject; + + /// actual position in the internal buffer + private int _pos = 4; + + /// output item (one item of the calltree) + private sCallTree _item; + + /// internal buffer size + private int _bufSize; + + /** Constructor. It initializes internal member variables. + ** + ** @param buf buffer with binary data + ** @param sameObject whether use same object as an output (all the time) or not */ + + public CallTreeIterator( Buffer buf, boolean sameObject) { + + _dataBuf = buf; + _sameObject = sameObject; + + _bufSize = _dataBuf.getSize(); + } + + /** Has next item. This method returns true if there is an item (next one) + ** in the buffer. It returns false if there is no other item in the buffer. + ** + ** @return true/false + ** + ** @see next() */ + + public boolean hasNext() { + + return (_pos < _bufSize); + } + + /** Next item. This method returns next (or first) item from the buffer. + ** It throws an exception if there is no other item in the buffer. + ** + ** @return a reference to sCallTree output object + ** + ** @see hasNext() */ + + public Object next() throws NoSuchElementException { + + if( !hasNext()) throw new NoSuchElementException(); + + if( !_sameObject || _item == null) _item = new sCallTree(); + + _pos = getCallTreeItem( _dataBuf, _pos, _item); + + return _item; + } + + /** Remove item. This method should remove an item from the list. + ** But it is not implemented and throws an exception everytime. */ + + public void remove() throws UnsupportedOperationException { + + throw new UnsupportedOperationException(); + } + }; + + /** Get calltree item thru iterator. This method returns an Iterator + ** object which can be used for iterating thru items of calltree. + ** This can be used if someone is not comfortable with calltree structure + ** returned by IProf class (by getCallTree() method) and wants to implement + ** its own one. An iterator returns structures of sCallTree type. + ** + ** @param threadObjId thread's object ID + ** @param sameOutputObject store output to same structure everytime + ** + ** @return an iterator of objects of sCallTree class; + ** or null (if calltree is empty) + ** + ** @see getCallTree(), getCallTreeThruIterator(), sCallTree */ + + public synchronized + Iterator getCallTreeThruIterator( int threadObjId, + boolean sameOutputObject) + + throws COMMUN_Exception, + BAD_OBJ_ID_Exception, + UNKNOWN_Exception { + + try { + + if( !getCallTreeAll( threadObjId)) return null; + } + catch( COMMUN_Exception e) { throw e;} + catch( BAD_OBJ_ID_Exception e) { throw e;} + catch( UNKNOWN_Exception e) { throw e;} + + Iterator iterator = + new CallTreeIterator( _buf, sameOutputObject); + + _buf = new Buffer(); + + return iterator; } }; |