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 java.util.Map;
 import java.util.Set;

Used to find differences between Maps

Author(s):
Daniel Bechler
 
 public final class MapDiffer implements Differ
 {
 
 	public MapDiffer(final DifferDispatcher differDispatcherfinal ComparisonStrategyResolver comparisonStrategyResolver)
 	{
 		Assert.notNull(differDispatcher"differDispatcher");
 		this. = differDispatcher;
 		this. = comparisonStrategyResolver;
 	}
 
 	private static Collection<?> findAddedKeys(final Instances instances)
 	{
 		final Set<?> source = instances.getWorking(Map.class).keySet();
 		final Set<?> filter = instances.getBase(Map.class).keySet();
 		return Collections.filteredCopyOf(sourcefilter);
 	}
 
 	private static Collection<?> findRemovedKeys(final Instances instances)
 	{
 		final Set<?> source = instances.getBase(Map.class).keySet();
 		final Set<?> filter = instances.getWorking(Map.class).keySet();
 		return Collections.filteredCopyOf(sourcefilter);
 	}
 
 	private static Iterable<?> findKnownKeys(final Instances instances)
 	{
 		final Set<?> keys = instances.getWorking(Map.class).keySet();
 		final Collection<?> changed = Collections.setOf(keys);
 		changed.removeAll(findAddedKeys(instances));
 		changed.removeAll(findRemovedKeys(instances));
 		return changed;
 	}
 
 	public boolean accepts(final Class<?> type)
 	{
 		if (type != null)
 		{
 			return Map.class.isAssignableFrom(type);
 		}
 		return false;
 	}
 
 	public final DiffNode compare(final DiffNode parentNodefinal Instances instances)
 	{
 		final DiffNode mapNode = new DiffNode(parentNodeinstances.getSourceAccessor(), instances.getType());
 		if (instances.hasBeenAdded())
 		{
 			compareEntries(mapNodeinstancesinstances.getWorking(Map.class).keySet());
 		}
 		else if (instances.hasBeenRemoved())
 		{
 			compareEntries(mapNodeinstancesinstances.getBase(Map.class).keySet());
 		}
 		else if (instances.areSame())
 		{
 		}
 		{
 			.resolveComparisonStrategy(mapNode).compare(mapNodeinstances.getType(), instances.getWorking(Map.class), instances.getBase(Map.class));
 		}
		else
		{
			compareEntries(mapNodeinstancesfindAddedKeys(instances));
			compareEntries(mapNodeinstancesfindRemovedKeys(instances));
			compareEntries(mapNodeinstancesfindKnownKeys(instances));
		}
		return mapNode;
	}
	private void compareEntries(final DiffNode mapNodefinal Instances mapInstancesfinal Iterable<?> keys)
	{
		for (final Object key : keys)
		{
			.dispatch(mapNodemapInstancesnew MapEntryAccessor(key));
		}
	}
New to GrepCode? Check out our FAQ X