frizbog / gedcom4j

Java library for reading/writing genealogy files in GEDCOM format
http://gedcom4j.org
53 stars 36 forks source link

Finding a specific FamilyEvent such as Marriage #188

Open gjwo opened 6 years ago

gjwo commented 6 years ago

To find an individual event I can use the code

Individual person;
...
List<IndividualEvent> events = person.getEventsOfType(IndividualEventType.BIRTH);

but I can't find an equivalent construct for families such as

Family family
...
List<FamilyEvent> events = family.getEventsOfType(FamilyEventType.MARRIAGE);

there is no method getEventsOfType, what is the normal way to do this?

frizbog commented 6 years ago

You're correct, of course, there isn't such a method. However, you can simply loop through the FamilyEvent collection looking for a matching type, which is exactly what Individual.getEventsOfType does

On Sat, Apr 7, 2018, 5:35 PM Graham Wood notifications@github.com wrote:

To find an individual event I can use the code

Individual person; ... List events = person.getEventsOfType(IndividualEventType.BIRTH);

but I can't find an equivalent construct for families such as

Family family ... List events = family.getEventsOfType(FamilyEventType.MARRIAGE);

there is no method getEventsOfType, what is the normal way to do this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/frizbog/gedcom4j/issues/188, or mute the thread https://github.com/notifications/unsubscribe-auth/ACxHYdJrfmDzq0s1iSML1ajSFPiW0ie1ks5tmTEIgaJpZM4TLQ0U .

gjwo commented 6 years ago

Thanks, this code worked fine

    /**
     * Get a list of events of the supplied type for the specified family
     * @param family    the family to be searched
     * @param type      the type of event to get
     * @return          a list of events of the specified type
     */

    public static List<FamilyEvent> getEventsOfType(Family family,FamilyEventType type) {
        List<FamilyEvent> result = new ArrayList<>(0);
        List<FamilyEvent> events = family.getEvents();
        if (events != null) {
            for (FamilyEvent fe : events) {
                if (fe.getType() == type) {
                    result.add(fe);
                }
            }
        }
        return Collections.unmodifiableList(result);
    }

If you find the time it might be worth enhancing the code by adding methods in Family to match Individual where appropriate, or adding an event abstraction common. to both.