| 1 | |
package org.jtheque.core.managers.properties; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 31 | |
|
| 32 | |
|
| 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 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 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 | |
} |