Search in sources :

Example 1 with ExitLoopException

use of com.revolsys.util.ExitLoopException in project com.revolsys.open by revolsys.

the class Graph method forEachEdge.

public void forEachEdge(final Predicate<Edge<T>> filter, final Consumer<Edge<T>> action, final Comparator<Edge<T>> comparator) {
    final LinkedList<Edge<T>> edges = new LinkedList<>(getEdges(filter));
    final EdgeEventListener<T> listener;
    if (comparator == null) {
        listener = (edgeEvent) -> {
            final Edge<T> edge = edgeEvent.getEdge();
            if (edgeEvent.isAddAction()) {
                if (filter == null || filter.test(edge)) {
                    edges.addFirst(edge);
                }
            }
        };
    } else {
        Collections.sort(edges, comparator);
        listener = (edgeEvent) -> {
            final Edge<T> edge = edgeEvent.getEdge();
            final String eventAction = edgeEvent.getAction();
            if (eventAction.equals(EdgeEvent.EDGE_ADDED)) {
                if (filter == null || filter.test(edge)) {
                    edges.addFirst(edge);
                }
                if (comparator != null) {
                    Collections.sort(edges, comparator);
                }
            } else if (eventAction.equals(EdgeEvent.EDGE_REMOVED)) {
                if (comparator != null) {
                    edges.remove(edge);
                }
            }
        };
    }
    this.edgeListeners.add(listener);
    try {
        while (!edges.isEmpty()) {
            final Edge<T> edge = edges.remove(0);
            if (!edge.isRemoved()) {
                action.accept(edge);
            }
        }
    } catch (final ExitLoopException e) {
    } finally {
        this.edgeListeners.remove(listener);
    }
}
Also used : LineString(com.revolsys.geometry.model.LineString) ExitLoopException(com.revolsys.util.ExitLoopException) LinkedList(java.util.LinkedList)

Example 2 with ExitLoopException

use of com.revolsys.util.ExitLoopException in project com.revolsys.open by revolsys.

the class StrTree method forEach.

@Override
public boolean forEach(final double minX, final double minY, final double maxX, final double maxY, final Consumer<? super I> action) {
    try {
        final BoundingBox boundingBox = getGeometryFactory().newBoundingBox(minX, minY, maxX, maxY);
        query(boundingBox, action);
        return true;
    } catch (final ExitLoopException e) {
        return false;
    }
}
Also used : BoundingBox(com.revolsys.geometry.model.BoundingBox) ExitLoopException(com.revolsys.util.ExitLoopException)

Example 3 with ExitLoopException

use of com.revolsys.util.ExitLoopException in project com.revolsys.open by revolsys.

the class Graph method forEachNode.

// TODO make this work with cached nodes
public void forEachNode(final Consumer<Node<T>> action, final Predicate<Node<T>> filter, final Comparator<Node<T>> comparator) {
    final List<Node<T>> nodes = new LinkedList<>();
    if (filter == null) {
        nodes.addAll(getNodes());
    } else {
        for (final Node<T> node : getNodes()) {
            if (filter.test(node)) {
                nodes.add(node);
            }
        }
    }
    NodeEventListener<T> listener;
    if (comparator == null) {
        listener = (nodeEvent) -> {
            if (nodeEvent.isAddAction()) {
                final Node<T> node = nodeEvent.getNode();
                if (filter == null || filter.test(node)) {
                    nodes.add(node);
                }
            }
        };
    } else {
        Collections.sort(nodes, comparator);
        listener = (nodeEvent) -> {
            final Node<T> node = nodeEvent.getNode();
            final String eventAction = nodeEvent.getAction();
            if (eventAction.equals(NodeEvent.NODE_ADDED)) {
                if (filter == null || filter.test(node)) {
                    nodes.add(node);
                }
                if (comparator != null) {
                    Collections.sort(nodes, comparator);
                }
            } else if (eventAction.equals(NodeEvent.NODE_REMOVED)) {
                nodes.remove(node);
            }
        };
    }
    this.nodeListeners.add(listener);
    try {
        while (!nodes.isEmpty()) {
            final Node<T> node = nodes.remove(0);
            if (!node.isRemoved()) {
                action.accept(node);
            }
        }
    } catch (final ExitLoopException e) {
    } finally {
        this.nodeListeners.remove(listener);
    }
}
Also used : LineString(com.revolsys.geometry.model.LineString) ExitLoopException(com.revolsys.util.ExitLoopException) LinkedList(java.util.LinkedList)

Aggregations

ExitLoopException (com.revolsys.util.ExitLoopException)3 LineString (com.revolsys.geometry.model.LineString)2 LinkedList (java.util.LinkedList)2 BoundingBox (com.revolsys.geometry.model.BoundingBox)1