]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/GroupFilters.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / GroupFilters.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.project;
13
14 import java.util.Set;
15
16 import org.simantics.project.features.registry.GroupReference;
17
18 /**
19  * Utilities for constructing {@link GroupFilter} instances.
20  * 
21  * @author Tuukka Lehtonen
22  */
23 public final class GroupFilters {
24
25     public static GroupFilter constant(final boolean answer) {
26         return new GroupFilter() {
27             @Override
28             public boolean accept(GroupReference ref) {
29                 return answer;
30             }
31         };
32     }
33
34     private static final GroupFilter ACCEPT_ALL = constant(true);
35     private static final GroupFilter DENY_ALL = constant(false);
36
37     public static GroupFilter acceptAll() {
38         return ACCEPT_ALL;
39     }
40
41     public static GroupFilter denyAll() {
42         return DENY_ALL;
43     }
44
45     /**
46      * Constructs a group filter that accepts only equal matches of all the
47      * group references provided in the argument set.
48      * 
49      * @param refs the group references to accept
50      * @return a filter accepting the specified set of group references
51      */
52     public static GroupFilter containsEqual(final Set<GroupReference> refs) {
53         return new GroupFilter() {
54             @Override
55             public boolean accept(GroupReference ref) {
56                 return refs.contains(ref);
57             }
58         };
59     }
60
61     /**
62      * Constructs a group filter that accepts groups that include one or
63      * more of the specified groups. Inclusion means that:
64      * <ol>
65      * <li>group ID's are equal</li>
66      * <li>if tested group has a version or version range it must include
67      * the version in the group of the specified set</li>
68      * </ol>
69      * 
70      * @param refs
71      * @return
72      */
73     public static GroupFilter includesVersion(final Set<GroupReference> refs) {
74         return new GroupFilter() {
75             @Override
76             public boolean accept(GroupReference range) {
77                 for (GroupReference ref : refs)
78                     if (range.includes(ref))
79                         return true;
80                 return false;
81             }
82         };
83     }
84
85 }