1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.requests;
14 import java.util.Collection;
15 import java.util.Comparator;
16 import java.util.List;
19 import java.util.Vector;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentSkipListSet;
23 import org.eclipse.jface.viewers.IFilter;
24 import org.simantics.db.Resource;
25 import org.simantics.scl.runtime.function.Function1;
28 * @author Tuukka Lehtonen
30 public class CollectionResult {
32 public static final IFilter DIAGRAM_RESOURCE_FILTER = o -> Nodes.DIAGRAM_RESOURCE_PREDICATE.test((Node) o);
33 public static final IFilter DIAGRAM_RESOURCE_AND_RVI_FILTER = o -> Nodes.DIAGRAM_RESOURCE_AND_RVI_PREDICATE.test((Node) o);
35 public class DiagramFilter implements IFilter {
37 private final IFilter proxy;
39 public DiagramFilter(IFilter proxy) {
44 public boolean select(Object node) {
45 return diagramSet.contains(node) && (proxy == null || proxy.select(node));
50 public final Set<Node> roots;
51 private final Set<Node> diagramSet;
52 public final List<Node> diagramList;
53 public final Map<Resource, Node> diagrams;
55 public CollectionResult() {
56 this.roots = new ConcurrentSkipListSet<Node>();
57 this.diagramSet = new ConcurrentSkipListSet<Node>();
58 this.diagramList = new Vector<Node>();
59 this.diagrams = new ConcurrentHashMap<Resource, Node>();
62 private CollectionResult(Set<Node> roots, Set<Node> diagramSet, List<Node> diagramList, Map<Resource, Node> diagrams) {
64 this.diagramSet = diagramSet;
65 this.diagramList = diagramList;
66 this.diagrams = diagrams;
69 public CollectionResult withRoots(Set<Node> roots) {
70 return new CollectionResult(roots, diagramSet, diagramList, diagrams);
73 public void addDiagram(Resource r, Node n) {
79 public Collection<Node> breadthFirstFlatten() {
80 return breadthFirstFlatten(null);
83 public Collection<Node> breadthFirstFlatten(IFilter filter) {
84 return Nodes.breadthFirstFlatten(new DiagramFilter(filter), roots);
87 public Collection<Node> depthFirstFlatten() {
88 return depthFirstFlatten(null, null);
91 public Collection<Node> depthFirstFlatten(IFilter filter, Comparator<? super Node> comparator) {
92 return Nodes.depthFirstFlatten(new DiagramFilter(filter), roots, comparator);
97 * function that takes the walked Node as argument and returns a
98 * boolean to describe whether to continue the walk or cancel the
99 * walk. The returned value cannot be <code>null</code>.
100 * @return <code>true</code> if the walk was completed or <code>false</code>
101 * if the walk was cancelled
103 public boolean walkTree(Function1<Node, Boolean> f) {
104 return Nodes.walkTree(f, roots);