public void optimize(){
  for (int i=operations.size() - 1; i >= 0; i--) {
    Operation o=operations.get(i);
    if (o instanceof ClearOperation) {
      while (i > 0) {
        operations.remove(i);
        i--;
      }
      return;
    }
  }
}

public void optimize ( ) { for ( int i = operations . size ( ) - 1 ; i >= 0 ; i -- ) { Operation o = operations . get ( i ) ; if ( o instanceof ClearOperation ) { while ( i > 0 ) { operations . remove ( i ) ; i -- ; } return ; } } }
optimize will remove any changes that are done before a clear ( )
