class MostRecentGeometryFilter implements Comparator<Geometry>, Serializable {

    public <E extends Geometry> List<E> filterMostRecent(Collection<? extends E> geometries, TimeManager timeManager,
            boolean checkTimeConstraint)
    {
        List<E> filtered = New.list();

        Map<Object, E> mostRecents = New.map();

        for (E geometry : geometries)
        {
            boolean addGeometry = true;
            if (geometry instanceof ConstrainableGeometry)
            {
                ConstrainableGeometry constrainGeom = (ConstrainableGeometry)geometry;
                Constraints constraints = constrainGeom.getConstraints();
                if (constraints != null)
                {
                    TimeConstraint mostRecentConstraint = constraints.getTimeConstraint();

                    if (mostRecentConstraint != null && mostRecentConstraint.isMostRecent())
                    {
                        if (!checkTimeConstraint || mostRecentConstraint.check(timeManager.getPrimaryActiveTimeSpans()))
                        {
                            Object groupId = mostRecentConstraint.getKey();

                            E mostRecent = mostRecents.get(groupId);

                            if (mostRecent == null || compare(geometry, mostRecent) > 0)
                            {
                                mostRecents.put(groupId, geometry);
                            }
                        }

                        addGeometry = false;
                    }
                }
            }

            if (addGeometry)
            {
                filtered.add(geometry);
            }
        }

        for (Entry<Object, E> entry : mostRecents.entrySet())
        {
            filtered.add(entry.getValue());
        }

        return filtered;
    }

}