| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ServicesFilterLatestVersionOnly |
|
| 4.333333333333333;4.333 |
| 1 | /* | |
| 2 | * Copyright 2010 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 | import java.util.ArrayList; | |
| 19 | import java.util.List; | |
| 20 | ||
| 21 | import org.kuali.student.contract.model.Service; | |
| 22 | ||
| 23 | /** | |
| 24 | * | |
| 25 | * @author nwright | |
| 26 | */ | |
| 27 | 0 | public class ServicesFilterLatestVersionOnly implements ServicesFilter { |
| 28 | ||
| 29 | @Override | |
| 30 | public List<Service> filter(List<Service> services) { | |
| 31 | 0 | List<Service> list = new ArrayList(); |
| 32 | 0 | for (Service target : services) { |
| 33 | 0 | int i = findService(list, target.getKey()); |
| 34 | 0 | if (i == -1) { |
| 35 | 0 | list.add(target); |
| 36 | 0 | continue; |
| 37 | } | |
| 38 | 0 | Service source = list.get(i); |
| 39 | 0 | if (compare(source.getVersion(), target.getVersion()) < 0) { |
| 40 | 0 | list.set(i, target); |
| 41 | } | |
| 42 | 0 | } |
| 43 | 0 | return list; |
| 44 | } | |
| 45 | ||
| 46 | protected int compare(String source, String target) { | |
| 47 | 0 | if (source.equalsIgnoreCase(target)) { |
| 48 | 0 | return 0; |
| 49 | } | |
| 50 | 0 | if (target.equalsIgnoreCase("dev")) { |
| 51 | 0 | return +1; |
| 52 | } | |
| 53 | 0 | return source.compareToIgnoreCase(target); |
| 54 | } | |
| 55 | ||
| 56 | private int findService(List<Service> list, String key) { | |
| 57 | 0 | for (int i = 0; i < list.size(); i++) { |
| 58 | 0 | Service service = list.get(i); |
| 59 | 0 | if (service.getKey().equals(key)) { |
| 60 | 0 | return i; |
| 61 | } | |
| 62 | } | |
| 63 | 0 | return -1; |
| 64 | } | |
| 65 | } |