Created
July 23, 2011 14:57
-
-
Save philipschwarz/1101512 to your computer and use it in GitHub Desktop.
2nd solution to Naresh Jain's challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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