deepdiff.core.DiffPointProcessorFactory Maven / Gradle / Ivy
/*
* Copyright 2011 DeepDiff Contributors
*
* 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 deepdiff.core;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Supports the creation of instances of {@link DiffPointProcessor} with an associated ID, and later
* look-up of instances by that ID.
*/
public class DiffPointProcessorFactory {
private static final String DEFAULT_ID = "default";
private static Map instances = Collections
.synchronizedMap(new HashMap());
/**
* Returns the instance with the specified ID, or null if none found
*
* @param id the ID of the instance to retrieve
*
* @return the instance with the specified ID, or null if none found
*/
public static DiffPointProcessor get(String id) {
return instances.get(id);
}
public static DiffPointProcessor set(String id, DiffPointProcessor processor) {
return instances.put(id, processor);
}
public static void clear() {
instances.clear();
}
/**
* Instantiates a new instance of the specified class using reflection, and stores it with the
* specified ID.
*
* @param className the name of the class to instantiate
* @param id the ID to store the instance under
*
* @return the new instance
*
* @throws ClassNotFoundException if the specified class could not be found
* @throws InstantiationException if the specified class could not be instantiated
* @throws IllegalAccessException if the specified class could not be instantiated due to access
* issues
*/
public static DiffPointProcessor instantiate(String className, String id)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class> clazz = Class.forName(className);
Object obj = clazz.newInstance();
DiffPointProcessor instance = (DiffPointProcessor) obj;
instances.put(id, instance);
return instance;
}
/**
* Returns the default instance of {@link DiffPointProcessor}, or null if none was configured
*
* @return the default instance of {@link DiffPointProcessor}, or null if none was configured
*/
public static DiffPointProcessor getDefault() {
return get(DEFAULT_ID);
}
}