]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/Testers.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / Testers.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.browsing.ui.common;
13
14 import org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.NodeContext.ConstantKey;
17 import org.simantics.browsing.ui.NodeQueryManager;
18 import org.simantics.browsing.ui.Tester;
19 import org.simantics.browsing.ui.common.node.StandardNodes;
20
21 public final class Testers {
22
23     /**
24      * Simple pass all tester.
25      */
26     public static final Tester PASS = new Tester() {
27         @Override
28         public boolean test(NodeQueryManager manager, NodeContext context) {
29             return true;
30         }
31         @Override
32         public String toString() {
33             return "PASS test";
34         }
35     };
36
37     /**
38      * Simple fail all tester.
39      */
40     public static final Tester FAIL = new Tester() {
41         @Override
42         public boolean test(NodeQueryManager manager, NodeContext context) {
43             return false;
44         }
45         @Override
46         public String toString() {
47             return "FAIL test";
48         }
49     };
50
51     /**
52      * @param clazz
53      * @return tester for checking whether node context input is an instance of
54      *         the specified class
55      */
56     public static Tester instanceOfTester(final Class<?> clazz) {
57         return new Tester() {
58             @Override
59             public boolean test(NodeQueryManager manager, NodeContext context) {
60                 return clazz.isInstance(context.getConstant(BuiltinKeys.INPUT));
61             }
62             @Override
63             public String toString() {
64                 return "class " + clazz.getName() + " test";
65             }
66         };
67     }
68
69     public static Tester standardFunctionTester(final Object object) {
70                 return new Tester() {
71                         
72                         @Override
73                         public boolean test(NodeQueryManager manager, NodeContext context) {
74                                 Object function = context.getConstant(StandardNodes.FUNCTION);
75                                 return object.equals(function);
76                         }
77                         
78                 };
79     }
80     
81     public static <T> Tester exists(final ConstantKey<T> key) {
82                 return new Tester() {
83                         
84                         @Override
85                         public boolean test(NodeQueryManager manager, NodeContext context) {
86                                 return context.getConstant(key) != null;
87                         }
88                         
89                 };
90     }
91
92     public static Tester and(final Tester ... testers) {
93                 return new Tester() {
94                         
95                         @Override
96                         public boolean test(NodeQueryManager manager, NodeContext context) {
97                                 for(Tester tester : testers) if(!tester.test(manager, context)) return false;
98                                 return true;
99                         }
100                         
101                 };
102     }
103     
104 }