1 2 package org.weda.store.impl; 3 4 import java.util.HashMap; 5 import java.util.Map; 6 import org.apache.commons.lang.ObjectUtils; 7 import org.apache.hivemind.util.Defense; 8 import org.weda.property.PropertyValue; 9 import org.weda.property.PropertyValueException; 10 11 /**Цель: чтение/редактирование свойств о множества объектов одного класса 12 * 13 * @author tim 14 */ 15 public class ObjectSet { 16 private Object[] objects; 17 private Class objectClass; 18 private PropertyValue propertyValue; 19 private Map<String, Boolean> uniqueFlag = new HashMap<String, Boolean>(); 20 21 public ObjectSet(Class objectClass, Object[] objects 22 , PropertyValue propertyValue) 23 throws ObjectSetException 24 { 25 if (objects==null || objects.length==0) 26 throw new ObjectSetException( 27 "Parameter objects can not be null or empty"); 28 this.objects = objects; 29 if (objectClass==null) 30 throw new ObjectSetException( 31 "Parameter objectClass can not be null"); 32 this.objectClass=objectClass; 33 if (propertyValue==null) 34 throw new ObjectSetException( 35 "Parameter propertyValue can not be null"); 36 this.propertyValue=propertyValue; 37 } 38 39 public Class getBaseClass(){ 40 return objects[0].getClass(); 41 } 42 43 /**Метод возвращает объекты множества 44 */ 45 public Object[] getObjects(){ 46 return objects; 47 } 48 49 /**Метод устанавливает значение свойства для группы объектов. 50 */ 51 public void setValue(String propertyPath, Object value) 52 throws ObjectSetException 53 { 54 try{ 55 Integer propId=propertyValue.compileSetter(objectClass, propertyPath); 56 for (Object obj: objects) 57 propertyValue.setValue(obj, propId, value); 58 uniqueFlag.put(propertyPath, true); 59 }catch(PropertyValueException e){ 60 throw new ObjectSetException( 61 String.format( 62 "Can't set value (%s) for object set property (%s)" 63 , propertyPath, value)); 64 } 65 66 } 67 68 /**Метод возврщает значение для группы объектов. 69 *@return значения свойства если все значения заданного свойства в группе 70 * объектов одинаковые, иначе <code>null</code> 71 *@see #setValue(String, Object) 72 *@see #isValueUnique(String) 73 */ 74 public Object getValue(String propertyPath) throws ObjectSetException { 75 if (isValueUnique(propertyPath)) { 76 try{ 77 Integer propId=propertyValue.compileGetter( 78 objectClass, propertyPath); 79 return propertyValue.getValue(objects[0], propId); 80 }catch(PropertyValueException e){ 81 throw new ObjectSetException ( 82 "Can't get value for property set.", e); 83 } 84 } 85 return null; 86 } 87 88 /**Метод вернет <code>true</code> если все значения указанного свойства 89 * в группе объектов одинаковые. 90 */ 91 public boolean isValueUnique(String propertyPath) 92 throws ObjectSetException 93 { 94 if (uniqueFlag.containsKey(propertyPath)) 95 return uniqueFlag.get(propertyPath); 96 else{ 97 boolean unique=true; 98 if (objects.length!=1){ 99 try{ 100 Integer propId=propertyValue.compileGetter( 101 objectClass, propertyPath); 102 Object val=propertyValue.getValue(objects[0], propId); 103 for (int i=1; i<objects.length; ++i){ 104 Object nextVal=propertyValue.getValue( 105 objects[i], propId); 106 if (!ObjectUtils.equals(val, nextVal)){ 107 unique=false; 108 break; 109 } 110 } 111 }catch(PropertyValueException e){ 112 throw new ObjectSetException ( 113 String.format( 114 "Can't detect property value uniqueness " + 115 "for property (%s)", propertyPath) 116 , e); 117 } 118 } 119 uniqueFlag.put(propertyPath, unique); 120 return unique; 121 } 122 } 123 }