package lambdas;

import java.util.Collections;
import java.util.List;

/**
 * Example of exception transparency
 *
 * @author Howard Lovatt
 */
public interface SortableList<T extends Comparable<? super T>> extends List<T> {
  void sort() default Collections.sort;

  <throws E> void forEach( Method1<T, T, E> method ) throws E default Trait.forEach;

  static class Trait {
    public static <T extends Comparable<? super T>, throws E> void forEach(
           final SortableList<T> self, final Method1<T, T, E> method ) throws E {
      for ( int i = 0; i < self.size(); i++ ) {
        final T oldValue = self.get( i );
        final T newValue = method.call( oldValue );
        self.set( i, newValue );
      }
    }
  }
}