|
Groovy example source code file (FactoryBuilderSupport.java)
This example Groovy source code file (FactoryBuilderSupport.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Groovy FactoryBuilderSupport.java source code
/*
* Copyright 2003-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.util;
import groovy.lang.Binding;
import groovy.lang.Closure;
import groovy.lang.DelegatingMetaClass;
import groovy.lang.GroovyClassLoader;
import groovy.lang.MetaClass;
import groovy.lang.MissingMethodException;
import groovy.lang.MissingPropertyException;
import groovy.lang.Reference;
import groovy.lang.Script;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Mix of BuilderSupport and SwingBuilder's factory support.
*
* Warning: this implementation is not thread safe and should not be used
* across threads in a multi-threaded environment. A locking mechanism
* should be implemented by the subclass if use is expected across
* multiple threads.
*
* @author <a href="mailto:james@coredevelopers.net">James Strachan
* @author Andres Almiray <aalmiray@users.sourceforge.com>
* @author Danno Ferrin
*/
public abstract class FactoryBuilderSupport extends Binding {
public static final String CURRENT_FACTORY = "_CURRENT_FACTORY_";
public static final String PARENT_FACTORY = "_PARENT_FACTORY_";
public static final String PARENT_NODE = "_PARENT_NODE_";
public static final String CURRENT_NODE = "_CURRENT_NODE_";
public static final String PARENT_CONTEXT = "_PARENT_CONTEXT_";
public static final String PARENT_NAME = "_PARENT_NAME_";
public static final String CURRENT_NAME = "_CURRENT_NAME_";
public static final String OWNER = "owner";
public static final String PARENT_BUILDER = "_PARENT_BUILDER_";
public static final String CURRENT_BUILDER = "_CURRENT_BUILDER_";
public static final String CHILD_BUILDER = "_CHILD_BUILDER_";
private static final Logger LOG = Logger.getLogger(FactoryBuilderSupport.class.getName());
/**
* Throws an exception if value is null.
*
* @param value the node's value
* @param name the node's name
*/
public static void checkValueIsNull(Object value, Object name) {
if (value != null) {
throw new RuntimeException("'" + name + "' elements do not accept a value argument.");
}
}
/**
* Checks type of value against builder type
*
* @param value the node's value
* @param name the node's name
* @param type a Class that may be assignable to the value's class
* @return true if type is assignable to the value's class, false if value
* is null.
*/
public static boolean checkValueIsType(Object value, Object name, Class type) {
if (value != null) {
if (type.isAssignableFrom(value.getClass())) {
return true;
} else {
throw new RuntimeException("The value argument of '" + name + "' must be of type "
+ type.getName() + ". Found: " + value.getClass());
}
} else {
return false;
}
}
/**
* Checks values against factory's type
*
* @param value the node's value
* @param name the node's name
* @param type a Class that may be assignable to the value's class
* @return Returns true if type is assignable to the value's class, false if value is
* null or a String.
*/
public static boolean checkValueIsTypeNotString(Object value, Object name, Class type) {
if (value != null) {
if (type.isAssignableFrom(value.getClass())) {
return true;
} else if (value instanceof String) {
return false;
} else {
throw new RuntimeException("The value argument of '" + name + "' must be of type "
+ type.getName() + " or a String. Found: " + value.getClass());
}
} else {
return false;
}
}
private ThreadLocal<LinkedList
Other Groovy examples (source code examples)
Here is a short list of links related to this Groovy FactoryBuilderSupport.java source code file:
|