Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Daniel Bechler
   *
   * 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 de.danielbechler.diff.differ;
 
 
 import static de.danielbechler.diff.circular.CircularReferenceDetector.CircularReferenceException;

Author(s):
Daniel Bechler
 
 public class DifferDispatcher
 {
 	private static final Logger logger = LoggerFactory.getLogger(DifferDispatcher.class);
 	private final DifferProvider differProvider;
 
 	public DifferDispatcher(final DifferProvider differProvider,
 							final CircularReferenceDetectorFactory circularReferenceDetectorFactory,
 							final CircularReferenceExceptionHandler circularReferenceExceptionHandler,
 							final IsIgnoredResolver ignoredResolver,
 							final IsReturnableResolver returnableResolver,
 							final PropertyAccessExceptionHandlerResolver propertyAccessExceptionHandlerResolver)
 	{
 		Assert.notNull(differProvider"differFactory");
 		this. = differProvider;
 
 		Assert.notNull(ignoredResolver"ignoredResolver");
 		this. = ignoredResolver;
 
 		this. = circularReferenceDetectorFactory;
 		this. = circularReferenceExceptionHandler;
 		this. = returnableResolver;
 		this. = propertyAccessExceptionHandlerResolver;
 
 	}
 
 	protected final void resetInstanceMemory()
 	{
 	}

Delegates the call to an appropriate Differ.

Returns:
A node representing the difference between the given de.danielbechler.diff.access.Instances.
 
 	public DiffNode dispatch(final DiffNode parentNode,
 							 final Instances parentInstances,
 							 final Accessor accessor)
 	{
 		Assert.notNull(parentInstances"parentInstances");
 		Assert.notNull(accessor"accessor");
 
 		final DiffNode node = compare(parentNodeparentInstancesaccessor);
 		if (parentNode != null && .isReturnable(node))
 		{
 			parentNode.addChild(node);
 		}
 		return node;
 	}
	private DiffNode compare(final DiffNode parentNode,
							 final Instances parentInstances,
							 final Accessor accessor)
	{
		final DiffNode node = new DiffNode(parentNodeaccessornull);
		{
			return node;
		}
		final Instances accessedInstances;
		if (accessor instanceof PropertyAwareAccessor)
		{
			final PropertyAwareAccessor propertyAwareAccessor = (PropertyAwareAccessoraccessor;
			try
			{
				accessedInstances = parentInstances.access(accessor);
			}
			catch (final PropertyReadException e)
			{
				final Class<?> parentType = parentInstances.getType();
				final String propertyName = propertyAwareAccessor.getPropertyName();
						.resolvePropertyAccessExceptionHandler(parentTypepropertyName);
				if (exceptionHandler != null)
				{
					exceptionHandler.onPropertyReadException(enode);
				}
				return node;
			}
		}
		else
		{
			accessedInstances = parentInstances.access(accessor);
		}
		if (accessedInstances.areNull())
		{
			return new DiffNode(parentNodeaccessedInstances.getSourceAccessor(), accessedInstances.getType());
		}
		else
		{
			return compareWithCircularReferenceTracking(parentNodeaccessedInstances);
		}
	}
														  final Instances instances)
	{
		DiffNode node = null;
		try
		{
			rememberInstances(parentNodeinstances);
			try
			{
				node = compare(parentNodeinstances);
			}
			finally
			{
				if (node != null)
				{
					forgetInstances(parentNodeinstances);
				}
			}
		}
		catch (final CircularReferenceException e)
		{
			node = newCircularNode(parentNodeinstancese.getNodePath());
		}
		if (parentNode == null)
		{
		}
		return node;
	}
	private DiffNode compare(final DiffNode parentNodefinal Instances instances)
	{
		final Differ differ = .retrieveDifferForType(instances.getType());
		if (differ == null)
		{
			throw new IllegalStateException("Couldn't create Differ for type '" + instances.getType() +
					"'. This mustn't happen, as there should always be a fallback differ.");
		}
		return differ.compare(parentNodeinstances);
	}
	protected void forgetInstances(final DiffNode parentNodefinal Instances instances)
	{
		final NodePath nodePath;
		if (parentNode != null)
		{
			final NodePath parentPath = parentNode.getPath();
			final ElementSelector elementSelector = instances.getSourceAccessor().getElementSelector();
			nodePath = NodePath.startBuildingFrom(parentPath).element(elementSelector).build();
		}
		else
		{
			nodePath = NodePath.withRoot();
		}
		.debug("[ {} ] Forgetting --- WORKING: {} <=> BASE: {}"nodePathinstances.getWorking(), instances.getBase());
	}
	protected void rememberInstances(final DiffNode parentNodefinal Instances instances)
	{
		final NodePath nodePath;
		if (parentNode != null)
		{
			final NodePath parentPath = parentNode.getPath();
			final ElementSelector elementSelector = instances.getSourceAccessor().getElementSelector();
			nodePath = NodePath.startBuildingFrom(parentPath).element(elementSelector).build();
		}
		else
		{
			nodePath = NodePath.withRoot();
		}
		.debug("[ {} ] Remembering --- WORKING: {} <=> BASE: {}"nodePathinstances.getWorking(), instances.getBase());
	}
	private void transactionalPushToCircularReferenceDetectors(final NodePath nodePathfinal Instances instances)
	{
		// TODO This needs to be solved more elegantly. If the push for one of these detectors fails,
		// we need to make sure to revert the push to the other one, if it already happened.
		try
		{
		}
		catch (final CircularReferenceException e)
		{
			throw e;
		}
	}
	private static DiffNode findNodeMatchingPropertyPath(final DiffNode nodefinal NodePath nodePath)
	{
		if (node == null)
		{
			return null;
		}
		if (node.matches(nodePath))
		{
			return node;
		}
		return findNodeMatchingPropertyPath(node.getParentNode(), nodePath);
	}
	private static DiffNode newCircularNode(final DiffNode parentNode,
											final Instances instances,
											final NodePath circleStartPath)
	{
		final DiffNode node = new DiffNode(parentNodeinstances.getSourceAccessor(), instances.getType());
		node.setCircleStartPath(circleStartPath);
		node.setCircleStartNode(findNodeMatchingPropertyPath(parentNodecircleStartPath));
		return node;
	}
New to GrepCode? Check out our FAQ X