| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GroupTypeStatePatternMatcher |
|
| 6.5;6.5 |
| 1 | /* | |
| 2 | * Copyright 2009 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.osedu.org/licenses/ECL-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.student.contract.model.util; | |
| 17 | ||
| 18 | /** | |
| 19 | * Matches patterns that define groups of types or groups of states | |
| 20 | * Currently supports comma separated lists and wildcard * at the end | |
| 21 | * @author nwright | |
| 22 | */ | |
| 23 | public class GroupTypeStatePatternMatcher { | |
| 24 | ||
| 25 | private String pattern; | |
| 26 | ||
| 27 | 0 | public GroupTypeStatePatternMatcher(String pattern) { |
| 28 | 0 | this.pattern = pattern; |
| 29 | 0 | } |
| 30 | ||
| 31 | public boolean matches(String key) { | |
| 32 | 0 | if (pattern.equals("*")) { |
| 33 | 0 | return true; |
| 34 | } | |
| 35 | // check for wildcard * at the end | |
| 36 | 0 | if (pattern.endsWith("*")) { |
| 37 | 0 | if (key.toLowerCase().startsWith(pattern.substring(0, pattern.length() |
| 38 | - 1).toLowerCase())) { | |
| 39 | 0 | return true; |
| 40 | } | |
| 41 | } | |
| 42 | // Check if key is in a comma separated list of keys | |
| 43 | // changed it so the list could contain a wildard as one of it's elements | |
| 44 | // for example kuali.type.foo.*,kuali.type.bar.joe | |
| 45 | 0 | if (pattern.indexOf(",") != -1) { |
| 46 | 0 | String[] patterns = pattern.split(","); |
| 47 | 0 | for (String pat : patterns) { |
| 48 | 0 | GroupTypeStatePatternMatcher matcher = |
| 49 | new GroupTypeStatePatternMatcher(pat); | |
| 50 | 0 | if (matcher.matches(key)) { |
| 51 | 0 | return true; |
| 52 | } | |
| 53 | } | |
| 54 | } | |
| 55 | //ok the pattern is the key so just compare | |
| 56 | 0 | if (pattern.equalsIgnoreCase(key)) { |
| 57 | 0 | return true; |
| 58 | } | |
| 59 | 0 | return false; |
| 60 | } | |
| 61 | } |