This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-validator. See https://gitlab.nuiton.org/nuiton/nuiton-validator.git commit fdbbe05781353ccacf7ab4eaa60c0e666ed749fe Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Jul 27 09:51:32 2016 +0200 Utilisation d'un vrai equals pour calculer l'unicité (fixes #3630) --- .../field/CollectionFieldExpressionValidator.java | 105 +++++++++++++++++---- 1 file changed, 89 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/nuiton/validator/xwork2/field/CollectionFieldExpressionValidator.java b/src/main/java/org/nuiton/validator/xwork2/field/CollectionFieldExpressionValidator.java index eae1fab..a34b9ae 100644 --- a/src/main/java/org/nuiton/validator/xwork2/field/CollectionFieldExpressionValidator.java +++ b/src/main/java/org/nuiton/validator/xwork2/field/CollectionFieldExpressionValidator.java @@ -21,6 +21,7 @@ */ package org.nuiton.validator.xwork2.field; +import com.google.common.base.Objects; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.validator.ValidationException; import com.opensymphony.xwork2.validator.validators.FieldExpressionValidator; @@ -28,8 +29,9 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; +import java.util.Comparator; import java.util.Set; +import java.util.TreeSet; /** * Un validateur basé sur {@link FieldExpressionValidator} qui valide sur une @@ -166,11 +168,11 @@ public class CollectionFieldExpressionValidator extends NuitonFieldExpressionVal if (useFirst && mode != Mode.ALL) { throw new ValidationException("can only use expressionForFirst in " + - "mode ALL but was " + mode); + "mode ALL but was " + mode); } if (useLast && mode != Mode.ALL) { throw new ValidationException("can only use expressionForLast in " + - "mode ALL but was " + mode); + "mode ALL but was " + mode); } String fieldName = getFieldName(); @@ -305,27 +307,47 @@ public class CollectionFieldExpressionValidator extends NuitonFieldExpressionVal return count == 1; } +// protected Boolean validateUniqueKey(Collection<?> col) throws ValidationException { +// boolean answer = true; +// +// Set<Integer> hashCodes = new HashSet<Integer>(); +// int index = -1; +// for (Object entry : col) { +// index++; +// // construction du hash de la clef d'unicite +// Integer hash = getUniqueKeyHashCode(entry); +// if (!hashCodes.contains(hash)) { +// hashCodes.add(hash); +// continue; +// } +// // une entree avec ce hash a deja ete trouvee +// // on est donc en violation sur la clef unique +// answer = false; +// if (log.isDebugEnabled()) { +// log.debug("duplicated uniquekey " + hash + " for entry " + index); +// } +// } +// hashCodes.clear(); +// return answer; +// } + protected Boolean validateUniqueKey(Collection<?> col) throws ValidationException { boolean answer = true; - Set<Integer> hashCodes = new HashSet<Integer>(); + Comparator<? super Object> comparator1 = getComparator(); + Set<? super Object> hashCodes = new TreeSet<Object>(comparator1); int index = -1; for (Object entry : col) { index++; - // construction du hash de la clef d'unicite - Integer hash = getUniqueKeyHashCode(entry); - if (!hashCodes.contains(hash)) { - hashCodes.add(hash); - continue; - } - // une entree avec ce hash a deja ete trouvee - // on est donc en violation sur la clef unique - answer = false; - if (log.isDebugEnabled()) { - log.debug("duplicated uniquekey " + hash + " for entry " + index); + boolean wasAdded = hashCodes.add(entry); + if (!wasAdded) { + answer = false; + if (log.isDebugEnabled()) { + log.debug("duplicated unique entry at position: " + index); + } + break; } } - hashCodes.clear(); return answer; } @@ -484,4 +506,55 @@ public class CollectionFieldExpressionValidator extends NuitonFieldExpressionVal return index == size - 1; } } + + Comparator<? super Object> comparator; + + private Comparator<? super Object> getComparator() { + if (comparator == null) { + comparator = new MyComparator<Object>(keys); + } + return comparator; + } + + private class MyComparator<O> implements Comparator<O> { + + private final String[] keys; + + public MyComparator(String... keys) { + this.keys = keys; + } + + @Override + public int compare(O o1, O o2) { + + boolean equals = true; + + for (String key : keys) { + + Object property1 = getPropertyValue(key, o1); + Object property2 = getPropertyValue(key, o2); + + equals = Objects.equal(property1, property2); + + if (!equals) { + break; + } + } + + return equals ? 0 : -1; + } + } + + protected Object getPropertyValue(String key, Object o) { + + try { + return getFieldValue(key, o); + + } catch (ValidationException e) { + if (log.isErrorEnabled()) { + log.error("Can't get property '" + key + "'value on oject: " + o); + } + return null; + } + } } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.