Coverage Report - org.kuali.rice.kns.datadictionary.impl.FieldOverrideForListElementBase
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldOverrideForListElementBase
0%
0/29
0%
0/14
1.778
 
 1  
 /*
 2  
  * Copyright 2007-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.opensource.org/licenses/ecl2.php
 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.rice.kns.datadictionary.impl;
 17  
 
 18  
 import java.util.Comparator;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.commons.beanutils.BeanComparator;
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 
 24  
 /**
 25  
  * The super class which implementations of the FieldOverride interface will extend. 
 26  
  * 
 27  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 28  
  *
 29  
  */
 30  
 public class FieldOverrideForListElementBase {
 31  
         
 32  
     private String propertyName;
 33  
     private Object element;
 34  
     protected String propertyNameForElementCompare;
 35  
     
 36  
 
 37  
     public String getPropertyNameForElementCompare() {
 38  0
         return propertyNameForElementCompare;
 39  
     }
 40  
 
 41  
     public void setPropertyNameForElementCompare(String propertyNameForElementCompare) {
 42  0
         this.propertyNameForElementCompare = propertyNameForElementCompare;
 43  0
     }
 44  
 
 45  
     protected int getElementPositionInList(Object object, List theList) {
 46  0
         Comparator comparator = this.getComparator();
 47  0
         int pos = -1;
 48  
         
 49  0
         if ( object != null && theList != null )
 50  
         {
 51  0
             for ( int i = 0; i < theList.size(); ++i )
 52  
             {
 53  0
                 Object item = theList.get(i);
 54  0
                 boolean equalFlag = false;
 55  0
                 if ( comparator != null )
 56  
                 {
 57  0
                     equalFlag = comparator.compare(object, item) == 0;
 58  
                 }
 59  
                 else
 60  
                 {
 61  0
                     equalFlag = item.equals(object);   
 62  
                 }
 63  0
                 if ( equalFlag )
 64  
                 {
 65  0
                     pos = i;
 66  0
                     break;
 67  
                 }
 68  
             }
 69  
         }
 70  0
         return pos;
 71  
     }
 72  
 
 73  
     public String getPropertyName() {
 74  0
         return propertyName;
 75  
     }
 76  
 
 77  
     public void setPropertyName(String propertyName) {
 78  0
         this.propertyName = propertyName;
 79  0
     }
 80  
 
 81  
     public Object getElement() {
 82  0
         return element;
 83  
     }
 84  
 
 85  
     public void setElement(Object value) {
 86  0
         this.element = value;
 87  0
     }
 88  
 
 89  
 
 90  
     public FieldOverrideForListElementBase() {
 91  0
         super();
 92  0
     }
 93  
 
 94  
     protected Comparator getComparator() {
 95  0
         Comparator comparator = null;
 96  0
         if ( StringUtils.isNotBlank(propertyNameForElementCompare))
 97  
         {
 98  0
             comparator = new BeanComparator(propertyNameForElementCompare);
 99  
         }
 100  
         else
 101  
         {
 102  0
             throw new RuntimeException("Missing required comparator definitions.");
 103  
         }
 104  0
         return comparator;
 105  
     }
 106  
 
 107  
 }