Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 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.batch.item.file.mapping;
 
 import java.util.List;
 
Helper class for calculating bean property matches, according to. Used by BeanWrapperImpl to suggest alternatives for an invalid property name.
Copied and slightly modified from Spring core,

Author(s):
Alef Arendsen
Arjen Poutsma
Juergen Hoeller
Dave Syer
Since:
1.0
See also:
forProperty(java.lang.String,java.lang.Class)
 
 final class PropertyMatches {
 
 	//---------------------------------------------------------------------
 	// Static section
 	//---------------------------------------------------------------------
 
Default maximum property distance: 2
 
 	public static final int DEFAULT_MAX_DISTANCE = 2;


Create PropertyMatches for the given bean property.

Parameters:
propertyName the name of the property to find possible matches for
beanClass the bean class to search for matches
 
 	public static PropertyMatches forProperty(String propertyNameClass<?> beanClass) {
 		return forProperty(propertyNamebeanClass);
 	}

Create PropertyMatches for the given bean property.

Parameters:
propertyName the name of the property to find possible matches for
beanClass the bean class to search for matches
maxDistance the maximum property distance allowed for matches
 
 	public static PropertyMatches forProperty(String propertyNameClass<?> beanClassint maxDistance) {
 		return new PropertyMatches(propertyNamebeanClassmaxDistance);
 	}
 
 
 	//---------------------------------------------------------------------
 	// Instance section
 	//---------------------------------------------------------------------
 
 	private final String propertyName;
 
 	private String[] possibleMatches;


Create a new PropertyMatches instance for the given property.
 
 	private PropertyMatches(String propertyNameClass<?> beanClassint maxDistance) {
 		this. = propertyName;
 		this. = calculateMatches(BeanUtils.getPropertyDescriptors(beanClass), maxDistance);
 	}


Return the calculated possible matches.
 
 	public String[] getPossibleMatches() {
 		return ;
 	}

Build an error message for the given invalid property name, indicating the possible property matches.
		StringBuffer buf = new StringBuffer();
		buf.append("Bean property '");
		buf.append("' is not writable or has an invalid setter method. ");
		if (ObjectUtils.isEmpty(this.)) {
			buf.append("Does the parameter type of the setter match the return type of the getter?");
		}
		else {
			buf.append("Did you mean ");
			for (int i = 0; i < this..lengthi++) {
				buf.append('\'');
				buf.append(this.[i]);
				if (i < this..length - 2) {
					buf.append("', ");
				}
				else if (i == this..length - 2){
					buf.append("', or ");
				}
	 		}
			buf.append("'?");
		}
		return buf.toString();
	}


Generate possible property alternatives for the given property and class. Internally uses the getStringDistance method, which in turn uses the Levenshtein algorithm to determine the distance between two Strings.

Parameters:
propertyDescriptors the JavaBeans property descriptors to search
maxDistance the maximum distance to accept
	private String[] calculateMatches(PropertyDescriptor[] propertyDescriptorsint maxDistance) {
		List<Stringcandidates = new ArrayList<String>();
		for (int i = 0; i < propertyDescriptors.lengthi++) {
			if (propertyDescriptors[i].getWriteMethod() != null) {
				String possibleAlternative = propertyDescriptors[i].getName();
				int distance = calculateStringDistance(this.possibleAlternative);
				if (distance <= maxDistance) {
					candidates.add(possibleAlternative);
				}
			}
		}
		Collections.sort(candidates);
		return StringUtils.toStringArray(candidates);
	}

Calculate the distance between the given two Strings according to the Levenshtein algorithm.

Parameters:
s1 the first String
s2 the second String
Returns:
the distance value
	private int calculateStringDistance(String s1String s2) {
		if (s1.length() == 0) {
			return s2.length();
		}
		if (s2.length() == 0) {
			return s1.length();
		}
		int d[][] = new int[s1.length() + 1][s2.length() + 1];
		for (int i = 0; i <= s1.length(); i++) {
			d[i][0] = i;
		}
		for (int j = 0; j <= s2.length(); j++) {
			d[0][j] = j;
		}
		for (int i = 1; i <= s1.length(); i++) {
			char s_i = s1.charAt(i - 1);
			for (int j = 1; j <= s2.length(); j++) {
				int cost;
				char t_j = s2.charAt(j - 1);
				if (Character.toLowerCase(s_i) == Character.toLowerCase(t_j)) {
					cost = 0;
else {
					cost = 1;
				}
				d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1),
						d[i - 1][j - 1] + cost);
			}
		}
		return d[s1.length()][s2.length()];
	}
New to GrepCode? Check out our FAQ X