de.danielbechler.diff.category.CategoryService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-object-diff Show documentation
Show all versions of java-object-diff Show documentation
Framework to detect and handle differences between Java objects
/*
* 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.category;
import de.danielbechler.diff.ObjectDifferBuilder;
import de.danielbechler.diff.node.DiffNode;
import de.danielbechler.diff.path.NodePath;
import de.danielbechler.diff.path.NodePathValueHolder;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
/**
*
*/
public class CategoryService implements CategoryConfigurer, CategoryResolver
{
private final NodePathValueHolder nodePathCategories = NodePathValueHolder.of(String[].class);
private final Map, String[]> typeCategories = new HashMap, String[]>();
private final ObjectDifferBuilder objectDifferBuilder;
public CategoryService(final ObjectDifferBuilder objectDifferBuilder)
{
this.objectDifferBuilder = objectDifferBuilder;
}
public Set resolveCategories(final DiffNode node)
{
final Set categories = new TreeSet();
categories.addAll(categoriesFromNodePathConfiguration(node));
categories.addAll(categoriesFromTypeConfiguration(node));
categories.addAll(categoriesFromNode(node));
return categories;
}
private Collection categoriesFromNodePathConfiguration(final DiffNode node)
{
final Collection allCategories = new HashSet();
final List accumulatedValues = nodePathCategories.accumulatedValuesForNodePath(node.getPath());
for (final String[] categoriesForElement : accumulatedValues)
{
allCategories.addAll(asList(categoriesForElement));
}
return allCategories;
}
private Collection categoriesFromTypeConfiguration(final DiffNode node)
{
final Class> nodeType = node.getValueType();
if (nodeType != null)
{
final String[] categories = typeCategories.get(nodeType);
if (categories != null)
{
return asList(categories);
}
}
return emptySet();
}
private static Collection categoriesFromNode(final DiffNode node)
{
return node.getCategories();
}
public Of ofNode(final NodePath nodePath)
{
return new Of()
{
public CategoryConfigurer toBe(final String... categories)
{
nodePathCategories.put(nodePath, categories);
return CategoryService.this;
}
};
}
public Of ofType(final Class> type)
{
return new Of()
{
public CategoryConfigurer toBe(final String... categories)
{
typeCategories.put(type, categories);
return CategoryService.this;
}
};
}
public ObjectDifferBuilder and()
{
return objectDifferBuilder;
}
}