Coverage Report - org.kuali.student.contract.model.util.ServicesFilterLatestVersionOnly
 
Classes in this File Line Coverage Branch Coverage Complexity
ServicesFilterLatestVersionOnly
0%
0/22
0%
0/14
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  
 
 30  
  @Override
 31  
  public List<Service> filter (List<Service> services)
 32  
  {
 33  0
    List<Service> list = new ArrayList ();
 34  0
   for (Service target : services)
 35  
   {
 36  0
    int i = findService (list, target.getKey ());
 37  0
    if (i == -1)
 38  
    {
 39  0
     list.add (target);
 40  0
     continue;
 41  
    }
 42  0
    Service source = list.get (i);
 43  0
    if (compare (source.getVersion (), target.getVersion ()) < 0)
 44  
    {
 45  0
     list.set (i, target);
 46  
    }
 47  0
   }
 48  0
   return list;
 49  
  }
 50  
 
 51  
  protected int compare (String source, String target)
 52  
  {
 53  0
   if (source.equalsIgnoreCase (target))
 54  
   {
 55  0
    return 0;
 56  
   }
 57  0
   if (target.equalsIgnoreCase ("dev"))
 58  
   {
 59  0
    return  + 1;
 60  
   }
 61  0
   return source.compareToIgnoreCase (target);
 62  
  }
 63  
 
 64  
  private int findService (List<Service> list, String key)
 65  
  {
 66  0
   for (int i = 0; i < list.size (); i ++)
 67  
   {
 68  0
    Service service = list.get (i);
 69  0
    if (service.getKey ().equals (key))
 70  
    {
 71  0
     return i;
 72  
    }
 73  
   }
 74  0
   return -1;
 75  
  }
 76  
 
 77  
 }