<s> package org . eclipse . jdt . internal . core ; import java . util . ArrayList ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IParent ; import org . eclipse . jdt . core . IRegion ; public class Region implements IRegion { protected ArrayList rootElements ; public Region ( ) { this . rootElements = new ArrayList ( 1 ) ; } public void add ( IJavaElement element ) { if ( ! contains ( element ) ) { removeAllChildren ( element ) ; this . rootElements . add ( element ) ; this . rootElements . trimToSize ( ) ; } } public boolean contains ( IJavaElement element ) { int size = this . rootElements . size ( ) ; ArrayList parents = getAncestors ( element ) ; for ( int i = 0 ; i < size ; i ++ ) { IJavaElement aTop = ( IJavaElement ) this . rootElements . get ( i ) ; if ( aTop . equals ( element ) ) { return true ; } for ( int j = 0 , pSize = parents . size ( ) ; j < pSize ; j ++ ) { if ( aTop . equals ( parents . get ( j ) ) ) { return true ; } } } return false ; } private ArrayList getAncestors ( IJavaElement element ) { ArrayList parents = new ArrayList ( ) ; IJavaElement parent = element . getParent ( ) ; while ( parent != null ) { parents . add ( parent ) ; parent = parent . getParent ( ) ; } parents . trimToSize ( ) ; return parents ; } public IJavaElement [ ] getElements ( ) { int size = this . rootElements . size ( ) ; IJavaElement [ ] roots = new IJavaElement [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { roots [ i ] = ( IJavaElement ) this . rootElements . get ( i ) ; } return roots ; } public boolean remove ( IJavaElement element ) { removeAllChildren ( element ) ; return this . rootElements . remove ( element ) ; } protected void removeAllChildren ( IJavaElement element ) { if ( element instanceof IParent ) { ArrayList newRootElements = new ArrayList ( ) ; for ( int i = 0 , size = this . rootElements . size ( ) ; i < size ; i ++ ) { IJavaElement currentRoot = ( IJavaElement ) this . rootElements . get ( i ) ; IJavaElement parent = currentRoot . getParent ( ) ; boolean isChild = false ; while ( parent != null ) { if ( parent . equals ( element ) ) { isChild = true ; break ; } parent = parent . getParent ( ) ; } if ( ! isChild ) { newRootElements . add ( currentRoot ) ; } } this . rootElements = newRootElements ; } } public String toString ( ) { StringBuffer buffer = new StringBuffer ( ) ; IJavaElement [ ] roots = getElements ( ) ; buffer . append ( '[' ) ; for ( int i = 0 ; i < roots . length ; i ++ ) { buffer . append ( roots [ i ] . getElementName ( ) ) ; if ( i < ( roots . length - 1 ) ) { buffer . append ( ", " ) ; } } buffer . append ( ']' ) ; return buffer . toString ( ) ; } } </s>