All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.atemsource.atem.utility.compare.JsonComparator Maven / Gradle / Ivy

The newest version!
package org.atemsource.atem.utility.compare;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.atemsource.atem.api.EntityTypeRepository;
import org.atemsource.atem.api.type.EntityType;
import org.atemsource.atem.utility.common.EntityOperationReference;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;


public class JsonComparator
{

	private Comparison arrayComparison;

	private Comparison comparison;

	private ComparisonBuilderFactory comparisonBuilderFactory;

	@Inject
	private EntityTypeRepository entityTypeRepository;

	public Comparison getArrayComparison()
	{
		return arrayComparison;
	}

	public Comparison getNodeComparison()
	{
		return comparison;
	}

	@PostConstruct
	public void initialize()
	{
		EntityType arrayType = entityTypeRepository.getEntityType(ArrayNode.class.getName());
		EntityType jsonType = entityTypeRepository.getEntityType(ObjectNode.class.getName());

		ComparisonBuilder arrayBuilder = comparisonBuilderFactory.create(arrayType);
		ComparisonBuilder builder = comparisonBuilderFactory.create(jsonType);

		EntityOperationReference arrayComparisonRef = arrayBuilder.getReference();
		EntityOperationReference nodeComparisonRef = builder.getReference();

		ComparisonAttributeBuilder arrayComparisonBuilder = arrayBuilder.include("children");
		arrayComparisonBuilder.cascadeDynamically(jsonType, nodeComparisonRef);
		arrayComparisonBuilder.cascadeDynamically(arrayType, arrayComparisonRef);
		arrayComparison = arrayBuilder.create();

		ComparisonAttributeBuilder propertiesComparisonBuilder = builder.include("properties");
		propertiesComparisonBuilder.cascadeDynamically(jsonType, nodeComparisonRef);
		propertiesComparisonBuilder.cascadeDynamically(arrayType, arrayComparisonRef);
		comparison = builder.create();

	}

	public void setArrayComparison(Comparison arrayComparison)
	{
		this.arrayComparison = arrayComparison;
	}

	public void setComparisonBuilderFactory(ComparisonBuilderFactory comparisonBuilderFactory)
	{
		this.comparisonBuilderFactory = comparisonBuilderFactory;
	}
}