I need to implement a filter function using Hibernate Conjunction or Criterion that:
Accepts a String containing any of the letters 'A', 'F', 'C', 'I' at most one time in a random order.
Returns a Hibernate criterion that evaluates as true if and only if the input occurrences match the String applied upon.
E.g.:
1) Input 'AFCI' should return true only if applied on 'AFCI', 'ACIF', 'AICF', 'FIAC', etc.
2) Input 'AI' should return true only if applied on 'AI' or 'IA'. Should return false if the field is 'AIC', 'AIF', 'ACI', 'AFCI', etc.
3) Input 'F' should return true only if applied on 'F'. Should return false in any other case, even if it contains 'F', such as 'AF' or 'FC'.
Any help will be deeply appreciated, because I'm stuck in this for too long.
There are not many permutations in the worst (AFCI) case, so we can generate all permutations and check equality of it
String toCheck = "AFCI";
List<String> permutations = createPermutations(toCheck);
Criterion result = Restrictions.in("propertyToCheck", permuations);
Method createPermutations()
can use Collections2.permutations()
from guava.
Updated
I think, this solution is professional because of it is easy to understand. It will work with any database. It can be easy changed in the future or extended with additional logic. It doesn't have a good scalability for large strings of course. But for small strings it has a good performance.
It will be interesting to know opinion of true professionals of course.