]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/Testers.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / Testers.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.common;\r
13 \r
14 import org.simantics.browsing.ui.BuiltinKeys;\r
15 import org.simantics.browsing.ui.NodeContext;\r
16 import org.simantics.browsing.ui.NodeContext.ConstantKey;\r
17 import org.simantics.browsing.ui.NodeQueryManager;\r
18 import org.simantics.browsing.ui.Tester;\r
19 import org.simantics.browsing.ui.common.node.StandardNodes;\r
20 \r
21 public final class Testers {\r
22 \r
23     /**\r
24      * Simple pass all tester.\r
25      */\r
26     public static final Tester PASS = new Tester() {\r
27         @Override\r
28         public boolean test(NodeQueryManager manager, NodeContext context) {\r
29             return true;\r
30         }\r
31         @Override\r
32         public String toString() {\r
33             return "PASS test";\r
34         }\r
35     };\r
36 \r
37     /**\r
38      * Simple fail all tester.\r
39      */\r
40     public static final Tester FAIL = new Tester() {\r
41         @Override\r
42         public boolean test(NodeQueryManager manager, NodeContext context) {\r
43             return false;\r
44         }\r
45         @Override\r
46         public String toString() {\r
47             return "FAIL test";\r
48         }\r
49     };\r
50 \r
51     /**\r
52      * @param clazz\r
53      * @return tester for checking whether node context input is an instance of\r
54      *         the specified class\r
55      */\r
56     public static Tester instanceOfTester(final Class<?> clazz) {\r
57         return new Tester() {\r
58             @Override\r
59             public boolean test(NodeQueryManager manager, NodeContext context) {\r
60                 return clazz.isInstance(context.getConstant(BuiltinKeys.INPUT));\r
61             }\r
62             @Override\r
63             public String toString() {\r
64                 return "class " + clazz.getName() + " test";\r
65             }\r
66         };\r
67     }\r
68 \r
69     public static Tester standardFunctionTester(final Object object) {\r
70                 return new Tester() {\r
71                         \r
72                         @Override\r
73                         public boolean test(NodeQueryManager manager, NodeContext context) {\r
74                                 Object function = context.getConstant(StandardNodes.FUNCTION);\r
75                                 return object.equals(function);\r
76                         }\r
77                         \r
78                 };\r
79     }\r
80     \r
81     public static <T> Tester exists(final ConstantKey<T> key) {\r
82                 return new Tester() {\r
83                         \r
84                         @Override\r
85                         public boolean test(NodeQueryManager manager, NodeContext context) {\r
86                                 return context.getConstant(key) != null;\r
87                         }\r
88                         \r
89                 };\r
90     }\r
91 \r
92     public static Tester and(final Tester ... testers) {\r
93                 return new Tester() {\r
94                         \r
95                         @Override\r
96                         public boolean test(NodeQueryManager manager, NodeContext context) {\r
97                                 for(Tester tester : testers) if(!tester.test(manager, context)) return false;\r
98                                 return true;\r
99                         }\r
100                         \r
101                 };\r
102     }\r
103     \r
104 }\r