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

nyla.solutions.global.patterns.validation.spring.ArrayableUniqueValidator Maven / Gradle / Ivy

Go to download

Nyla Solutions Global Java API provides support for basic application utilities (application configuration, data encryption, debugger and text processing).

The newest version!
package nyla.solutions.global.patterns.validation.spring;

import java.util.HashSet;
import java.util.Set;

import nyla.solutions.global.data.Arrayable;
import nyla.solutions.global.exception.DuplicateRowException;

import org.springframework.validation.Errors;

/**
 * @author Gregory Green
 *
 */
public class ArrayableUniqueValidator extends AbstractValidation
{


	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void validate(Object obj, Errors errors)
	{
		if(set == null)
			set = new HashSet();
		
		Arrayable arrayable = (Arrayable)obj;
		
		Object key = arrayable.toArray()[keyPosition];
		
		if(set.contains(key))
			throw new DuplicateRowException(String.valueOf(key));

		set.add(key);
		
	}// --------------------------------------------------------
	
	
	/**
	 * @return the keyPosition
	 */
	public int getKeyPosition()
	{
		return keyPosition;
	}
	/**
	 * @param keyPosition the keyPosition to set
	 */
	public void setKeyPosition(int keyPosition)
	{
		this.keyPosition = keyPosition;
	}


	private Set set = null;
	private int keyPosition = 0;

}