Skip to content

Commit

Permalink
Merge 5d75dfd into e1ba0e1
Browse files Browse the repository at this point in the history
  • Loading branch information
dickschoeller committed Feb 1, 2017
2 parents e1ba0e1 + 5d75dfd commit 49151fe
Show file tree
Hide file tree
Showing 107 changed files with 1,983 additions and 1,309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,28 @@
public final class AgeEstimator {
/** Which person are we estimating. */
private final Person person;
/** Relative to which date. */
private final Calendar relativeTo;

/**
* Estimator for the specified person, relative to today.
*
* @param person the person whose age we are estimating
*/
public AgeEstimator(final Person person) {
this.person = person;
this.relativeTo = Calendar.getInstance();
}
/** Provides the "today" for use in comparisons. */
private final CalendarProvider provider;

/**
* Estimator for the specified person, relative to the provided date.
* This constructor is mostly for testing. Though we could use it later
* for age at birth of children, etc.
*
* @param person the person whose age we are estimating
* @param relativeTo the date we are comparing against
* @param provider the calendar provider we are using to determine now
*/
public AgeEstimator(final Person person, final Calendar relativeTo) {
public AgeEstimator(final Person person, final CalendarProvider provider) {
this.person = person;
this.relativeTo = relativeTo;
this.provider = provider;
}

/**
* @return the estimated difference in years
*/
public int estimateInYears() {
final String birthDateString = person.getBirthDate();
final LocalDate l0 = new LocalDate(relativeTo);
final LocalDate l0 = provider.nowDate();
final DateParser parser = new DateParser(birthDateString);
final LocalDate l1 = new LocalDate(parser.getEstimateCalendar());
final Period p = new Period(l1, l0);
Expand All @@ -60,23 +50,23 @@ public int estimateInYears() {
*/
public String estimateInYearsMonthsDays() {
final String birthDateString = person.getBirthDate();
final LocalDate l0 = new LocalDate(relativeTo);
final LocalDate l0 = provider.nowDate();
final DateParser parser = new DateParser(birthDateString);
final Calendar estimateCalendar = parser.getEstimateCalendar();
final LocalDate l1 = new LocalDate(estimateCalendar);
final Period p = new Period(l1, l0, PeriodType.yearMonthDay());
final PeriodFormatter ymd = new PeriodFormatterBuilder()
.printZeroAlways()
.appendYears()
.appendSuffix(" year", " years")
.appendSeparator(", ")
.printZeroRarelyLast()
.appendMonths()
.appendSuffix(" month", " months")
.appendSeparator(", ")
.appendDays()
.appendSuffix(" day", " days")
.toFormatter();
.printZeroAlways()
.appendYears()
.appendSuffix(" year", " years")
.appendSeparator(", ")
.printZeroRarelyLast()
.appendMonths()
.appendSuffix(" month", " months")
.appendSeparator(", ")
.appendDays()
.appendSuffix(" day", " days")
.toFormatter();
return ymd.print(p);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.schoellerfamily.gedbrowser.analytics;

import java.util.Calendar;

import org.joda.time.LocalDate;

/**
* @author Dick Schoeller
*/
public interface CalendarProvider {
/**
* @return the provider's understanding of now as a Calendar
*/
Calendar now();

/**
* @return the provider's understanding of now as a LocalDate
*/
LocalDate nowDate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.schoellerfamily.gedbrowser.analytics;

import java.util.Calendar;

import org.joda.time.LocalDate;

/**
* @author Dick Schoeller
*/
public final class CalendarProviderImpl implements CalendarProvider {
/**
* {@inheritDoc}
*/
@Override
public Calendar now() {
return Calendar.getInstance();
}

/**
* {@inheritDoc}
*/
@Override
public LocalDate nowDate() {
return new LocalDate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.schoellerfamily.gedbrowser.analytics;

import java.util.Calendar;
import java.util.Locale;

import org.joda.time.LocalDate;

/**
* @author Dick Schoeller
*/
public final class CalendarProviderStub implements CalendarProvider {
/**
* {@inheritDoc}
*/
@Override
public Calendar now() {
final int birthYear = 2015;
final int birthDay = 14;
final Calendar referenceCalendar = Calendar.getInstance(Locale.US);
referenceCalendar.set(Calendar.YEAR, birthYear);
referenceCalendar.set(Calendar.MONTH, Calendar.DECEMBER);
referenceCalendar.set(Calendar.DAY_OF_MONTH, birthDay);
return referenceCalendar;
}

/**
* {@inheritDoc}
*/
@Override
public LocalDate nowDate() {
return new LocalDate(now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ public final class LivingEstimator {

/** Hold the person we are estimating. */
private final Person person;
/** Provides the "today" for use in comparisons. */
private final CalendarProvider provider;

/**
* Constructor.
*
* @param person the person we are estimating
* @param provider the provider of "today" for comparisons
*/
public LivingEstimator(final Person person) {
public LivingEstimator(final Person person,
final CalendarProvider provider) {
this.person = person;
this.provider = provider;
}

/**
Expand All @@ -56,7 +61,7 @@ public boolean estimate() {
// No death date and no birth date estimate, assume living.
return true;
}
final LocalDate today = new LocalDate();
final LocalDate today = provider.nowDate();
final Period p = new Period(date, today);
return p.getYears() < VERY_OLD_AGE;
}
Expand All @@ -67,18 +72,21 @@ public boolean estimate() {
* @param living the list of people estimated to be living
* @param dead the list of people estimated to be dead
* @param buckets the living people divided into buckets by decade
* @param provider the provider of "today" for comparisons
*/
public static void fillBuckets(final GedObject root,
final List<Person> living, final List<Person> dead,
final Map<Integer, Set<Person>> buckets) {
final Map<Integer, Set<Person>> buckets,
final CalendarProvider provider) {
LOGGER.entering("LivingEstimator", "fillBuckets");
for (final String letter : root.findSurnameInitialLetters()) {
for (final String surname : root.findBySurnamesBeginWith(letter)) {
for (final Person person : root.findBySurname(surname)) {
final LivingEstimator le = createLivingEstimator(person);
final LivingEstimator le =
createLivingEstimator(person, provider);
if (estimate(le)) {
living.add(person);
addToBucket(buckets, person);
addToBucket(buckets, person, provider);
} else {
dead.add(person);
}
Expand All @@ -102,21 +110,25 @@ private static boolean estimate(final LivingEstimator le) {
* Create a new estimator.
*
* @param person the person we're estimating
* @param provider the calendar provider we are using to determine now
* @return the estimator
*/
private static LivingEstimator createLivingEstimator(final Person person) {
return new LivingEstimator(person);
private static LivingEstimator createLivingEstimator(final Person person,
final CalendarProvider provider) {
return new LivingEstimator(person, provider);
}

/**
* @param buckets the collection of people into age ranges
* @param person the person to add
* @param provider the calendar provider we are using to determine now
*/
private static void addToBucket(final Map<Integer, Set<Person>> buckets,
final Person person) {
final Person person,
final CalendarProvider provider) {
final BirthDateEstimator bde = new BirthDateEstimator(person);
final LocalDate date = bde.estimateBirthDate();
final LocalDate today = new LocalDate();
final LocalDate today = provider.nowDate();
final Period p = new Period(date, today);
final int age = p.getYears();
final int bucket = (age / AGE_BUCKET_SIZE) * AGE_BUCKET_SIZE;
Expand Down
Loading

0 comments on commit 49151fe

Please sign in to comment.