| 1 | // $Id$ |
|---|
| 2 | /** |
|---|
| 3 | * Copyright (C) 2009 EDIT |
|---|
| 4 | * European Distributed Institute of Taxonomy |
|---|
| 5 | * http://www.e-taxonomy.eu |
|---|
| 6 | * |
|---|
| 7 | * The contents of this file are subject to the Mozilla Public License Version 1.1 |
|---|
| 8 | * See LICENSE.TXT at the top of this package for the full license terms. |
|---|
| 9 | */ |
|---|
| 10 | package eu.etaxonomy.cdm.model.common; |
|---|
| 11 | |
|---|
| 12 | import java.util.Comparator; |
|---|
| 13 | import java.util.HashMap; |
|---|
| 14 | import java.util.Map; |
|---|
| 15 | import java.util.regex.Matcher; |
|---|
| 16 | import java.util.regex.Pattern; |
|---|
| 17 | |
|---|
| 18 | import org.springframework.stereotype.Component; |
|---|
| 19 | |
|---|
| 20 | import eu.etaxonomy.cdm.model.taxon.TaxonNode; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Abstract Comparator for Strings which allows define substitution rules which |
|---|
| 24 | * are applied to the String to be compared before the actual comparison takes |
|---|
| 25 | * place. By this it is e.g. possible to influence the position of objects in sorted lists etc. |
|---|
| 26 | * <p> |
|---|
| 27 | * <b>Intended usage</b>: To allow maximum flexibility the property |
|---|
| 28 | * {@link #setSubstitutionRules(Map)} should be set in the spring application |
|---|
| 29 | * context. |
|---|
| 30 | * |
|---|
| 31 | * @author a.kohlbecker |
|---|
| 32 | * @date 24.06.2009 |
|---|
| 33 | */ |
|---|
| 34 | @Component |
|---|
| 35 | public abstract class AbstractStringComparator implements Comparator<TaxonNode> { |
|---|
| 36 | |
|---|
| 37 | protected Map<Pattern, String> substitutionRules = null; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Set the private field substitutionRules. The substitutionRules consist of |
|---|
| 41 | * a regular expression as key and a string to be prepended as value. |
|---|
| 42 | * |
|---|
| 43 | * @param rules |
|---|
| 44 | */ |
|---|
| 45 | public void setSubstitutionRules(Map<String, String> substitutionRules) { |
|---|
| 46 | this.substitutionRules = new HashMap<Pattern, String>(substitutionRules.size()); |
|---|
| 47 | for (String regex : substitutionRules.keySet()) { |
|---|
| 48 | this.substitutionRules.put(Pattern.compile(regex), substitutionRules.get(regex)); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Applies the first matching <code>substitutionRules</code> set by |
|---|
| 55 | * {@link #setSubstitutionRules()} to the given String. A rules is applied |
|---|
| 56 | * in the following way: If the regular expression matches the given string |
|---|
| 57 | * <code>s</code> the String mapped by the regular expression is prepended |
|---|
| 58 | * to <code>s</code>. |
|---|
| 59 | * |
|---|
| 60 | * @param s |
|---|
| 61 | * @return |
|---|
| 62 | */ |
|---|
| 63 | protected String applySubstitutionRules(String s) { |
|---|
| 64 | if (substitutionRules == null) { |
|---|
| 65 | return s; |
|---|
| 66 | } |
|---|
| 67 | StringBuffer sb = new StringBuffer(); |
|---|
| 68 | for (Pattern pattern : substitutionRules.keySet()) { |
|---|
| 69 | if (pattern.matcher(s).matches()) { |
|---|
| 70 | sb.append(substitutionRules.get(pattern)).append(s); |
|---|
| 71 | return sb.toString(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | } |
|---|
| 75 | return s; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | } |
|---|