/** * {@inheritDoc} * * <p><strong>Subclasses must override {@link #accept(java.lang.Object)} not visit.</strong> * I goofed and named the method incorrectly.</p> * * @param <T> * @param visitor * @return * @deprecated */ @Override @Deprecated @SuppressWarnings( "deprecation" ) public final <T> T visit( final V visitor ) { return this.<T>accept( visitor ); } }
@Override public final <T extends Node<V>> void addAll( final Collection<T> child ) { if( !canHaveChildren() ) { throw new UnsupportedOperationException( "This Node cannot have children: " + getNodePath() ); } for( T c : child ) { add( c ); } }
protected final void toString( final int depth, final StringBuilder sb ) { if( depth > 0 ) { sb.append( '\n' ); for( int i = 0; i < depth; i++ ) { sb.append( ' ' ); } sb.append( '+' ); } sb.append( getClass().getName() ).append( "[leaf=" ).append( isLeaf() ); sb.append( ",index=" ).append( getIndex() ); sb.append( ",children=" ).append( childCount() ); toStringImpl( sb ); sb.append( ']' ); if( !isLeaf() ) { for( Node<V> child : children ) { if( child instanceof AbstractNode ) { this.cast( child ).toString( depth + 2, sb ); } } } }
@Override public final <T extends Node<V>> T add( final T child ) { if( !canHaveChildren() ) { throw new UnsupportedOperationException( "This Node cannot have children: " + getNodePath() ); } if( children == null ) { children = new LinkedList<Node<V>>(); } if( child.getParent() != null ) { child.getParent().remove( child ); } children.add( child ); if( child instanceof AbstractNode ) { this.cast( child ).setParent( this ); } return child; }
@Override protected void toStringImpl( StringBuilder sb ) { sb.append( ",text=\"" ).append( getText() ).append( '\"' ); sb.append( ",properties=" ).append( getProperties() ); super.toStringImpl( sb ); }
@Override public final int getIndex() { if( parent == null || !(parent instanceof AbstractNode) ) { return -1; } return this.cast( parent ).children.indexOf( this ); }
public final String getNodePath() { final StringBuilder sb = new StringBuilder(); sb.append( getClass().getName() ); Node<V> n = getParent(); while( n != null ) { sb.insert( 0, '/' ).insert( 0, n.getClass().getName() ); n = n.getParent(); } return sb.toString(); }
@Override public final <T extends Node<V>> T insert( final int index, final T child ) { if( !canHaveChildren() ) { throw new UnsupportedOperationException( "This Node cannot have children: " + getNodePath() ); } if( children == null ) { children = new LinkedList<Node<V>>(); } if( child.getParent() != null ) { child.getParent().remove( child ); } children.add( index, child ); if( child instanceof AbstractNode ) { this.cast( child ).setParent( this ); } return child; }
@Override public final void removeChildren() { if( children != null ) { for( Node<V> child : children ) { if( child instanceof AbstractNode ) { this.cast( child ).setParent( null ); } } // Set children to null, freeing the list. This does two things: // 1: frees up memory if the node never gets any more children // 2: if the children have been moved to another node (see addAll // and TexMathParser for examples) means that we don't corrupt // the node tree children = null; } }