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

net.projectmonkey.object.mapper.construction.rule.PropertyGroupRule Maven / Gradle / Ivy

Go to download

Object mapping implementation written as an alternative to modelmapper which is able to support inheritance, handles flattening / expanding in a precise way, and is extensible / configurable

The newest version!
package net.projectmonkey.object.mapper.construction.rule;

import net.projectmonkey.object.mapper.annotations.group.GroupBehaviour;
import net.projectmonkey.object.mapper.construction.PopulationContext;
import net.projectmonkey.object.mapper.construction.PropertyPopulationContext;
import net.projectmonkey.object.mapper.context.ConversionConfiguration;
import net.projectmonkey.object.mapper.context.ExecutionContext;

/*
 *
 *  * Copyright 2012 the original author or authors.
 *  *
 *  * 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.
 *
 */

/**
 * Rule which evaluates whether the property currently being mapped
 * should be included according to the combination of
 * {@link net.projectmonkey.object.mapper.annotations.group.GroupBehaviour} and @link{Group} / @link{Groups}
 *
 * @author Andy Moody
 */
public class PropertyGroupRule implements MappingRule
{

	public static PropertyGroupRule INSTANCE = new PropertyGroupRule();

	PropertyGroupRule()
	{
	}

	@Override
	public boolean isIncluded(PopulationContext context)
	{

		Class group = getConfiguration().getGroup();

		boolean inclusiveGroup = false;
		if (group.isAnnotationPresent(GroupBehaviour.class))
		{
			inclusiveGroup = group.getAnnotation(GroupBehaviour.class).inclusive();
		}
		boolean sourcePathIncluded = false;
		boolean destPathIncluded = false;
		if(context instanceof PropertyPopulationContext)
		{
			sourcePathIncluded = ((PropertyPopulationContext) context).getSourceProperty().isIncludedInGroup(group);
			destPathIncluded = ((PropertyPopulationContext) context).getDestinationProperty().isIncludedInGroup(group);
		}
		return inclusiveGroup ? sourcePathIncluded && destPathIncluded : sourcePathIncluded || destPathIncluded;
	}

	@Override
	public boolean isApplicableFor(PopulationContext context)
	{
		return true;
	}

	protected ConversionConfiguration getConfiguration()
	{
		return ExecutionContext.getConfiguration();
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy