Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004-2008 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.
  */
 package org.springframework.binding.mapping.impl;
 
A single mapping definition, encapsulating the information necessary to map the result of evaluating an expression on a source object to a property on a target object, optionally applying a type conversion during the mapping process.

Author(s):
Keith Donald
Scott Andrews
 
 public class DefaultMapping implements Mapping {

The source expression to evaluate against a source object to map from.
 
 	private final Expression sourceExpression;

The target expression to set on a target object to map to.
 
 	private final Expression targetExpression;

Whether or not this is a required mapping; if true, the source expression must return a non-null value.
 
 	private boolean required;

A specific type conversion routine to apply during the mapping process.
 
Creates a new mapping.

Parameters:
sourceExpression the source expression
targetExpression the target expression
 
 	public DefaultMapping(Expression sourceExpressionExpression targetExpression) {
 		Assert.notNull(sourceExpression"The source expression is required");
 		Assert.notNull(targetExpression"The target expression is required");
 		this. = sourceExpression;
 		this. = targetExpression;
 	}
 
 	// implementing mapping
 
 		return ;
 	}
 
 		return ;
 	}
 
 	public boolean isRequired() {
 		return ;
 	}
 
 	// optional impl getters/setters
 
Returns the type conversion executor to use during mapping execution. May be null.
 
 		return ;
 	}

Sets a specific type conversion executor to use during mapping execution.

Parameters:
typeConverter the type converter
 
 	public void setTypeConverter(ConversionExecutor typeConverter) {
 		this. = typeConverter;
 	}

Indicates if this mapping is a required mapping. Default is false.

Parameters:
required required status
	public void setRequired(boolean required) {
		this. = required;
	}

Execute this mapping.

Parameters:
context the mapping context
	public void map(DefaultMappingContext context) {
		context.setCurrentMapping(this);
		Object sourceValue;
		try {
			sourceValue = .getValue(context.getSource());
catch (EvaluationException e) {
			return;
		}
		if ( && (sourceValue == null || isEmptyString(sourceValue))) {
			context.setRequiredErrorResult(sourceValue);
			return;
		}
		Object targetValue = sourceValue;
		if (sourceValue != null) {
			if ( != null) {
				try {
					targetValue = .execute(sourceValue);
					return;
				}
			}
		}
		try {
			.setValue(context.getTarget(), targetValue);
			context.setSuccessResult(sourceValuetargetValue);
catch (ValueCoercionException e) {
catch (EvaluationException e) {
			context.setTargetAccessError(sourceValuee);
		}
	}
	private boolean isEmptyString(Object sourceValue) {
		if (sourceValue instanceof CharSequence) {
			return ((CharSequencesourceValue).length() == 0;
else {
			return false;
		}
	}
	public boolean equals(Object o) {
		if (!(o instanceof DefaultMapping)) {
			return false;
		}
		return .equals(other.sourceExpression) && .equals(other.targetExpression);
	}
	public int hashCode() {
	}
	public String toString() {
		return  + " -> " + ;
	}
New to GrepCode? Check out our FAQ X