Coverage Report - org.jtheque.core.managers.properties.PropertiesManager
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesManager
54 %
29/53
43 %
14/32
4.286
 
 1  
 package org.jtheque.core.managers.properties;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.AbstractActivableManager;
 20  
 import org.jtheque.utils.bean.EqualsUtils;
 21  
 import org.jtheque.utils.bean.ReflectionUtils;
 22  
 import org.jtheque.utils.collections.CollectionUtils;
 23  
 
 24  
 import java.beans.PropertyDescriptor;
 25  
 import java.lang.reflect.InvocationTargetException;
 26  
 import java.util.Collection;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * A properties manager implementation.
 31  
  *
 32  
  * @author Baptiste Wicht
 33  
  */
 34  8
 public final class PropertiesManager extends AbstractActivableManager implements IPropertiesManager {
 35  
     @Override
 36  
     public <T> T createMemento(T bean) {
 37  4
         T instance = null;
 38  
 
 39  
         try {
 40  4
             instance = (T) bean.getClass().newInstance();
 41  
 
 42  16
             for (PropertyDescriptor property : ReflectionUtils.getProperties(bean)) {
 43  12
                 if (property.getReadMethod() == null || property.getWriteMethod() == null) {
 44  0
                     continue;
 45  
                 }
 46  
 
 47  12
                 Object value = property.getReadMethod().invoke(bean);
 48  
 
 49  12
                 setValue(instance, property, value);
 50  
             }
 51  0
         } catch (Exception e) {
 52  0
             getLogger().error(e);
 53  4
         }
 54  
 
 55  4
         return instance;
 56  
     }
 57  
 
 58  
     @Override
 59  
     public void restoreMemento(Object bean, Object memento) {
 60  
         try {
 61  8
             for (PropertyDescriptor property : ReflectionUtils.getProperties(bean)) {
 62  6
                 if (property.getReadMethod() == null || property.getWriteMethod() == null) {
 63  0
                     continue;
 64  
                 }
 65  
 
 66  6
                 Object value = ReflectionUtils.getProperty(memento, property);
 67  
 
 68  6
                 setValue(bean, property, value);
 69  
             }
 70  0
         } catch (InvocationTargetException e) {
 71  0
             getLogger().error(e);
 72  0
         } catch (IllegalAccessException e) {
 73  0
             getLogger().error(e);
 74  2
         }
 75  2
     }
 76  
 
 77  
     @Override
 78  
     public boolean areEquals(Object bean, Object other, String... properties) {
 79  0
         if (bean == other) {
 80  0
             return true;
 81  
         }
 82  
 
 83  0
         if (EqualsUtils.areObjectIncompatible(bean, other)) {
 84  0
             return false;
 85  
         }
 86  
 
 87  0
         for (String property : properties) {
 88  0
             Object propertyBean = getPropertyQuickly(bean, property);
 89  0
             Object propertyOther = getPropertyQuickly(other, property);
 90  
 
 91  0
             if (propertyBean == null) {
 92  0
                 if (propertyOther != null) {
 93  0
                     return false;
 94  
                 }
 95  0
             } else if (!propertyBean.equals(propertyOther)) {
 96  0
                 return false;
 97  
             }
 98  
         }
 99  
 
 100  0
         return true;
 101  
     }
 102  
 
 103  
     @Override
 104  
     public Object getProperty(Object bean, String property){
 105  0
         return ReflectionUtils.getProperty(bean, property);
 106  
     }
 107  
 
 108  
     @Override
 109  
     public Object getPropertyQuickly(Object bean, String property) {
 110  4
         return ReflectionUtils.getPropertyValue(bean, property);
 111  
     }
 112  
 
 113  
     /**
 114  
      * Set the value to the bean on the bean.
 115  
      *
 116  
      * @param bean     The bean to edit.
 117  
      * @param property The property to set.
 118  
      * @param value    The value to set to the property.
 119  
      * @throws IllegalAccessException    If the property cannot be accessed.
 120  
      * @throws InvocationTargetException If there is a problem during the setting process.
 121  
      */
 122  
     private static void setValue(Object bean, PropertyDescriptor property, Object value) throws IllegalAccessException, InvocationTargetException {
 123  18
         if (value != null && List.class.isAssignableFrom(value.getClass())) {
 124  0
             property.getWriteMethod().invoke(bean, CollectionUtils.copyOf((Collection<?>) value));
 125  
         } else {
 126  18
             property.getWriteMethod().invoke(bean, value);
 127  
         }
 128  18
     }
 129  
 
 130  
     @Override
 131  
     public String toString(Object bean) {
 132  4
         StringBuilder builder = new StringBuilder(bean.getClass().getSimpleName());
 133  
 
 134  4
         builder.append('{');
 135  
 
 136  16
         for (PropertyDescriptor property : ReflectionUtils.getProperties(bean)) {
 137  12
             if (property.getReadMethod() == null) {
 138  0
                 continue;
 139  
             }
 140  
 
 141  12
             builder.append(property.getName()).append('=');
 142  12
             builder.append(ReflectionUtils.getProperty(bean, property));
 143  12
             builder.append(", ");
 144  
         }
 145  
 
 146  4
         builder.delete(builder.lastIndexOf(", "), builder.lastIndexOf(", ") + 2);
 147  
 
 148  4
         builder.append('}');
 149  
 
 150  4
         return builder.toString();
 151  
     }
 152  
 }