source: osm/applications/editors/josm/plugins/opendata/includes/org/geotools/filter/FunctionFinder.java@ 28000

Last change on this file since 28000 was 28000, checked in by donvip, 12 years ago

Import new "opendata" JOSM plugin

File size: 4.8 KB
Line 
1/*
2 * GeoTools - The Open Source Java GIS Toolkit
3 * http://geotools.org
4 *
5 * (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation;
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
17package org.geotools.filter;
18
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22import java.util.Set;
23
24import org.geotools.factory.CommonFactoryFinder;
25import org.geotools.factory.Hints;
26import org.opengis.filter.capability.FunctionName;
27import org.opengis.filter.expression.Function;
28import org.opengis.filter.expression.Literal;
29
30/**
31 * Isolate function lookup code from Factory implementation(s).
32 * <p>
33 * This is done to look for two things:
34 * <ul>
35 * <li>org.geotools.filter.Function
36 * <li>org.opengis.filter.expression.Function
37 * </ul>
38 * This is done as a proper utility class that accepts Hints.
39 *
40 * @author Jody Garnett
41 *
42 *
43 * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/modules/library/main/src/main/java/org/geotools/filter/FunctionFinder.java $
44 */
45public class FunctionFinder {
46 private volatile Map<String,FunctionFactory> functionFactoryCache;
47
48 public FunctionFinder(Hints hints) {
49 // currently hints are not used, need help :-P
50 }
51
52 /**
53 * Look up a function for the provided name.
54 *
55 * @param name Function name; this will need to be an exact match
56 * @param parameters Set of parameters required
57 * @return Generated function
58 * @throws a RuntimeException if an implementation for name could not be found
59 */
60 public Function findFunction(String name, List/* <Expression> */parameters){
61 return findFunction(name, parameters, null );
62 }
63
64 /**
65 * Look up a function for the provided name, may return a FallbackFunction if
66 * an implementation could not be found.
67 * <p>
68 * You can create a function to represent an SQL function or a function hosted on
69 * an external service; the fallback value will be used if you evulate
70 * by a Java implementation on the classpath.
71 * @param name Function name; this will need to be an exact match
72 * @param parameters Set of Expressions to use as function parameters
73 * @param fallbackValue Literal to use if an implementation could not be found
74 * @return Function for the provided name, may be a FallbackFunction if an implementation could not be found
75 */
76 public Function findFunction(String name, List parameters, Literal fallback) {
77 //try name as is
78 Function f = findFunctionInternal(name, parameters, fallback);
79 if (f == null) {
80 //try by trimming "Function" off of name
81 if (name.endsWith("Function")) {
82 name = name.substring(0, name.length()-"Function".length());
83 f = findFunctionInternal(name, parameters, fallback);
84 }
85 }
86 if( f == null && fallback != null ){
87 return new FallbackFunction( name, parameters, fallback );
88 }
89
90
91 if (f != null) {
92 return f;
93 }
94
95 throw new RuntimeException("Unable to find function " + name);
96
97 }
98
99 Function findFunctionInternal(String name, List parameters, Literal fallback) {
100 if (functionFactoryCache == null) {
101 synchronized (this) {
102 if (functionFactoryCache == null) {
103 functionFactoryCache = lookupFunctions();
104 }
105 }
106 }
107
108 if (functionFactoryCache.containsKey(name)) {
109 return functionFactoryCache.get(name).function(name, parameters, fallback);
110 }
111
112 //do a lookup from all factories, this is because of the name tricks the default
113 // factory does
114 Function f = null;
115 for (FunctionFactory ff : CommonFactoryFinder.getFunctionFactories(null)) {
116 f = ff.function(name, parameters, fallback);
117 if (f != null) return f;
118 }
119
120 return null;
121 }
122
123 private HashMap lookupFunctions() {
124 // get all filter functions via function factory
125 HashMap result = new HashMap();
126
127 Set<FunctionFactory> functionFactories =
128 CommonFactoryFinder.getFunctionFactories(null);
129 for (FunctionFactory ff : functionFactories) {
130 for (FunctionName functionName : ff.getFunctionNames()) {
131 result.put(functionName.getName(), ff);
132 }
133 }
134
135 return result;
136 }
137
138}
Note: See TracBrowser for help on using the repository browser.