Skip to content

Instantly share code, notes, and snippets.

@philipschwarz
Created July 23, 2011 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipschwarz/1101512 to your computer and use it in GitHub Desktop.
Save philipschwarz/1101512 to your computer and use it in GitHub Desktop.
2nd solution to Naresh Jain's challenge
int calculateAveragePreviousPercentageComplete() { return sumAndAverageTheValueOfField(PREVIOUS_PERCENTAGE_COMPLETE); }
int calculateAverageCurrentPercentageComplete() { return sumAndAverageTheValueOfField(CURRENT_PERCENTAGE_COMPLETE); }
int calculateAverageProgressPercentage() { return sumAndAverageTheValueOfField(PROGRESS_PERCENTAGE); }
int sumAndAverageTheValueOfField( Functor<StudentActivityByAlbum, Integer> fieldGetter){
return addAll(collect( activities, fieldGetter)) / activities.size(); }
<T> int addAll(Iterable<Integer> items){ return reduce( items, PLUS); }
static final Functor<StudentActivityByAlbum, Integer> PREVIOUS_PERCENTAGE_COMPLETE = new Functor<StudentActivityByAlbum, Integer>(){
@Override public Integer execute(StudentActivityByAlbum value) { return value.getPreviousPercentageCompleted(); }};
static final Functor<StudentActivityByAlbum, Integer> CURRENT_PERCENTAGE_COMPLETE =new Functor<StudentActivityByAlbum, Integer>(){
@Override public Integer execute(StudentActivityByAlbum value) { return value.getPercentageCompleted(); }};
static final Functor<StudentActivityByAlbum, Integer> PROGRESS_PERCENTAGE =new Functor<StudentActivityByAlbum, Integer>(){
@Override public Integer execute(StudentActivityByAlbum value) { return value.getProgressPercentage(); }};
static final Functor2<Integer, Integer, Integer> PLUS = new Functor2<Integer, Integer, Integer>(){
@Override public Integer execute(Integer accumulator, Integer value) { return accumulator + value; }};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment