deepdiff.core.DiffUnitProcessorFactory 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.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import deepdiff.scope.FileDiffScope;
/**
* Supports the creation of instances of {@link DiffUnitProcessor} with an associated ID, and later
* look-up of instances by that ID.
*/
public class DiffUnitProcessorFactory {
public static final String DEFAULT_ID = "default";
private static final Logger log = Logger.getLogger(DiffUnitProcessorFactory.class);
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 DiffUnitProcessor get(String id) {
return instances.get(id);
}
public static DiffUnitProcessor set(String id, DiffUnitProcessor 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 DiffUnitProcessor instantiate(String className, String id)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class> clazz = Class.forName(className);
Object obj = clazz.newInstance();
DiffUnitProcessor instance = (DiffUnitProcessor) obj;
instances.put(id, instance);
return instance;
}
/**
* Returns the default instance of {@link DiffUnitProcessor}, or null if none was configured
*
* @return the default instance of {@link DiffUnitProcessor}, or null if none was configured
*/
public static DiffUnitProcessor getDefault() {
return get(DEFAULT_ID);
}
public static DiffScope createInitialScope(File leftFile, File rightFile) {
DiffScope scope = null;
if (leftFile.isFile() && rightFile.isFile()) {
scope = createScopeFromFileContents(leftFile, rightFile);
}
if (scope == null) {
scope = new FileDiffScope(leftFile, rightFile);
}
return scope;
}
private static DiffScope createScopeFromFileContents(File leftFile, File rightFile) {
DiffScope scope = null;
DiffUnitProcessor unitProcessor = getDefault();
if (unitProcessor instanceof ScopedDiffUnitProcessor) {
ScopedDiffUnitProcessor sdup = (ScopedDiffUnitProcessor) unitProcessor;
String leftName = leftFile.getName();
String rightName = rightFile.getName();
int suffixLen = longestCommonSuffixLength(leftName, rightName);
String path;
if (suffixLen == leftName.length()) {
path = leftName;
} else {
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append("[");
pathBuilder.append(leftName.substring(0, leftName.length() - suffixLen));
pathBuilder.append(",");
pathBuilder.append(rightName.substring(0, rightName.length() - suffixLen));
pathBuilder.append("]");
pathBuilder.append(leftName.substring(leftName.length() - suffixLen));
path = pathBuilder.toString();
}
try {
InputStream leftStream = new FileInputStream(leftFile);
InputStream rightStream = new FileInputStream(rightFile);
scope = sdup.createScope(path, leftStream, rightStream);
} catch (IOException ex) {
log.error("Failure reading from specified files", ex);
}
}
return scope;
}
public static int longestCommonSuffixLength(String s, String t) {
int i = s.length() - 1;
int j = t.length() - 1;
for (; i >= 0 && j >= 0; --i, --j)
if (s.charAt(i) != t.charAt(j))
return s.length() - (i + 1);
return s.length() - (i + 1);
}
}