|
From: Max M. <max...@us...> - 2003-07-10 16:51:34
|
Update of /cvsroot/wonder/Wonder/Common/Frameworks/ERExtensions/Sources/er/extensions
In directory sc8-pr-cvs1:/tmp/cvs-serv12839/er/extensions
Modified Files:
Tag: Wonder_1_0_1_Branch
ERXArrayUtilities.java ERXExtensions.java
Log Message:
Changing init order on branch
Index: ERXArrayUtilities.java
===================================================================
RCS file: /cvsroot/wonder/Wonder/Common/Frameworks/ERExtensions/Sources/er/extensions/ERXArrayUtilities.java,v
retrieving revision 1.23
retrieving revision 1.23.2.1
diff -C2 -d -r1.23 -r1.23.2.1
*** ERXArrayUtilities.java 27 Feb 2003 22:46:37 -0000 1.23
--- ERXArrayUtilities.java 10 Jul 2003 16:47:46 -0000 1.23.2.1
***************
*** 11,14 ****
--- 11,15 ----
import java.util.*;
import java.io.*;
+ import java.math.BigDecimal;
/**
***************
*** 22,25 ****
--- 23,27 ----
public static final String NULL_GROUPING_KEY="**** NULL GROUPING KEY ****";
+ /** caches if array utilities have been initialized */
private static boolean initialized = false;
***************
*** 284,306 ****
* all of the arrays contained within the array
* passed in.
! */
! // ENHANCEME: Should add option to filter duplicates
public static NSArray flatten(NSArray array) {
! NSMutableArray newArray=null;
! for (int i=0; i<array.count(); i++) {
! Object element=array.objectAtIndex(i);
if (element instanceof NSArray) {
if (newArray==null) {
! newArray=new NSMutableArray();
! for (int j=0; j<i; j++) {
! if(array.objectAtIndex(j)!=null){
! newArray.addObject(array.objectAtIndex(j));
}
}
}
! NSArray a=flatten((NSArray)element);
! for (int j=0; j<a.count();j++) {
! if(a.objectAtIndex(j)!=null){
! newArray.addObject(a.objectAtIndex(j));
}
}
--- 286,335 ----
* all of the arrays contained within the array
* passed in.
! */
public static NSArray flatten(NSArray array) {
! return flatten(array, false);
! }
!
! /**
! * Recursively flattens an array of arrays into a single
! * array of elements.<br/>
! * <br/>
! * For example:<br/>
! * <code>NSArray foos;</code> //Assume exists<br/>
! * <code>NSArray bars = (NSArray)foos.valueForKey("toBars");</code>
! * In this case if <code>foos</code> contained five elements
! * then the array <code>bars</code> will contain five arrays
! * each corresponding to what <code>aFoo.toBars</code> would
! * return. To have the entire collection of <code>bars</code>
! * in one single arra you would call:
! * <code>NSArray allBars = flatten(bars)</code>
! * @param array to be flattened
! * @param filterDuplicates determines if the duplicate values
! * should be filtered
! * @return an array containing all of the elements from
! * all of the arrays contained within the array
! * passed in.
! */
! public static NSArray flatten(NSArray array, boolean filterDuplicates) {
! NSMutableArray newArray = null;
! for (int i=0; i < array.count(); i++) {
! Object element = array.objectAtIndex(i);
if (element instanceof NSArray) {
if (newArray==null) {
! newArray = new NSMutableArray();
! for (int j = 0; j < i; j++) {
! if (array.objectAtIndex(j) != null) {
! if (!filterDuplicates || !newArray.containsObject(array.objectAtIndex(j))) {
! newArray.addObject(array.objectAtIndex(j));
! }
}
}
}
! NSArray a = flatten((NSArray)element);
! for (int j = 0; j < a.count(); j++) {
! if (a.objectAtIndex(j) != null) {
! if (!filterDuplicates || !newArray.containsObject(array.objectAtIndex(j))) {
! newArray.addObject(a.objectAtIndex(j));
! }
}
}
***************
*** 328,337 ****
* every keypath.
*/
! public static NSArray valuesForKeyPaths(NSArray array, NSArray paths) {
NSMutableArray result = new NSMutableArray();
Enumeration e = paths.objectEnumerator();
while(e.hasMoreElements()) {
! result.addObject((NSArray)array.valueForKeyPath((String)e.nextElement()));
}
return result;
--- 357,366 ----
* every keypath.
*/
! public static NSArray valuesForKeyPaths(Object array, NSArray paths) {
NSMutableArray result = new NSMutableArray();
Enumeration e = paths.objectEnumerator();
while(e.hasMoreElements()) {
! result.addObject(NSKeyValueCodingAdditions.Utility.valueForKeyPath(array, (String)e.nextElement()));
}
return result;
***************
*** 445,449 ****
*/
public Object compute(NSArray array, String keypath) {
! return flatten(array);
}
}
--- 474,482 ----
*/
public Object compute(NSArray array, String keypath) {
! array = flatten(array);
! if(keypath != null && keypath.length() > 0) {
! array = (NSArray)NSKeyValueCodingAdditions.Utility.valueForKeyPath(array, keypath);
! }
! return array;
}
}
***************
*** 532,540 ****
public Object compute(NSArray array, String keypath) {
synchronized (array) {
! return arrayWithoutDuplicates(array);
}
}
}
/**
* Will register new NSArray operators
--- 565,651 ----
public Object compute(NSArray array, String keypath) {
synchronized (array) {
! array = arrayWithoutDuplicates(array);
! if(keypath != null && keypath.length() > 0) {
! array = (NSArray)NSKeyValueCodingAdditions.Utility.valueForKeyPath(array, keypath);
! }
! return array;
}
}
}
+
+ /**
+ * Define an {@link NSArray$Operator} for the key <b>objectAtIndex</b>.<br/>
+ * <br/>
+ * This allows for key value paths like:<br/>
+ * <br/>
+ * <code>myArray.valueForKey("@objectAtIndex.3.firstName");</code><br/>
+ * <br/>
+ *
+ */
+ static class ObjectAtIndexOperator implements NSArray.Operator {
+ /** public empty constructor */
+ public ObjectAtIndexOperator() {}
+
+ /**
+ * returns the keypath value for n-ths object.
+ * @param array array to be checked.
+ * @param keypath integer value of index (zero based).
+ * @return <code>null</code> if array is empty or value is not in index, <code>keypath</code> value otherwise.
+ */
+ public Object compute(NSArray array, String keypath) {
+ synchronized (array) {
+ int end = keypath.indexOf(".");
+ int index = Integer.parseInt(keypath.substring(0, end == -1 ? keypath.length() : end));
+ Object value = null;
+ if(index < array.count() )
+ value = array.objectAtIndex(index);
+ if(end != -1 && value != null) {
+ value = NSKeyValueCodingAdditions.Utility.valueForKeyPath(value, keypath.substring(end+1));
+ }
+ return value;
+ }
+ }
+ }
+
+ /**
+ * Define an {@link NSArray$Operator} for the key <b>avgNonNull</b>.<br/>
+ * <br/>
+ * This allows for key value paths like:<br/>
+ * <br/>
+ * <code>myArray.valueForKey("@avgNonNull.revenue");</code><br/>
+ * <br/>
+ * which will sum up all values and divide by the number of nun-null entries.
+ */
+ static class AvgNonNullOperator implements NSArray.Operator {
+ /** public empty constructor */
+ public AvgNonNullOperator() {}
+
+ /**
+ * returns the average value for over all non-null values.
+ * @param array array to be checked.
+ * @param keypath value of average.
+ * @return computed average as double or <code>NULL</code>.
+ */
+ public Object compute(NSArray array, String keypath) {
+ synchronized (array) {
+ BigDecimal result = new BigDecimal(0L);
+ int count = 0;
+
+ for(Enumeration e = array.objectEnumerator(); e.hasMoreElements();) {
+ Object value = NSKeyValueCodingAdditions.Utility.valueForKeyPath(e.nextElement(), keypath);
+ if(value != null && value != NSKeyValueCoding.NullValue) {
+ count = count+1;
+ result = result.add(ERXValueUtilities.bigDecimalValue(value));
+ }
+ }
+ if(count == 0) {
+ return null;
+ }
+ return result.divide(BigDecimal.valueOf((long) count), result.scale() + 4, 6);
+ }
+ }
+ }
+
/**
* Will register new NSArray operators
***************
*** 547,560 ****
}
initialized = true;
! NSArray.setOperatorForKey("sort", new SortOperator(EOSortOrdering.CompareAscending));
! NSArray.setOperatorForKey("sortAsc", new SortOperator(EOSortOrdering.CompareAscending));
! NSArray.setOperatorForKey("sortDesc", new SortOperator(EOSortOrdering.CompareDescending));
! NSArray.setOperatorForKey("sortInsensitiveAsc", new SortOperator(EOSortOrdering.CompareCaseInsensitiveAscending));
! NSArray.setOperatorForKey("sortInsensitiveDesc", new SortOperator(EOSortOrdering.CompareCaseInsensitiveDescending));
! NSArray.setOperatorForKey("flatten", new FlattenOperator());
! NSArray.setOperatorForKey("fetchSpec", new FetchSpecOperator());
! NSArray.setOperatorForKey("unique", new UniqueOperator());
! NSArray.setOperatorForKey("isEmpty", new IsEmptyOperator());
! NSArray.setOperatorForKey("subarrayWithRange", new SubarrayWithRangeOperator());
}
--- 658,675 ----
}
initialized = true;
! if (ERXProperties.booleanForKeyWithDefault("er.extensions.ERXArrayUtilities.ShouldRegisterOperators", true)) {
! NSArray.setOperatorForKey("sort", new SortOperator(EOSortOrdering.CompareAscending));
! NSArray.setOperatorForKey("sortAsc", new SortOperator(EOSortOrdering.CompareAscending));
! NSArray.setOperatorForKey("sortDesc", new SortOperator(EOSortOrdering.CompareDescending));
! NSArray.setOperatorForKey("sortInsensitiveAsc", new SortOperator(EOSortOrdering.CompareCaseInsensitiveAscending));
! NSArray.setOperatorForKey("sortInsensitiveDesc", new SortOperator(EOSortOrdering.CompareCaseInsensitiveDescending));
! NSArray.setOperatorForKey("flatten", new FlattenOperator());
! NSArray.setOperatorForKey("fetchSpec", new FetchSpecOperator());
! NSArray.setOperatorForKey("unique", new UniqueOperator());
! NSArray.setOperatorForKey("isEmpty", new IsEmptyOperator());
! NSArray.setOperatorForKey("subarrayWithRange", new SubarrayWithRangeOperator());
! NSArray.setOperatorForKey("objectAtIndex", new ObjectAtIndexOperator());
! NSArray.setOperatorForKey("avgNonNull", new AvgNonNullOperator());
! }
}
***************
*** 578,581 ****
--- 693,718 ----
}
return result;
+ }
+
+ /**
+ * Batches an NSArray into sub-arrays of the given size.
+ * @param array array to batch
+ * @param batchSize number of items in each batch
+ * @return NSArray of NSArrays, each with at most batchSize items
+ */
+ public static NSArray batchedArrayWithSize(NSArray array, int batchSize) {
+ if(array == null || array.count() == 0)
+ return NSArray.EmptyArray;
+
+ NSMutableArray batchedArray = new NSMutableArray();
+ int count = array.count();
+
+ for(int i = 0; i < count; i+=batchSize) {
+ int length = batchSize;
+ if(i + length > count)
+ length = count - i;
+ batchedArray.addObject(array.subarrayWithRange(new NSRange(i, length)));
+ }
+ return batchedArray;
}
Index: ERXExtensions.java
===================================================================
RCS file: /cvsroot/wonder/Wonder/Common/Frameworks/ERExtensions/Sources/er/extensions/ERXExtensions.java,v
retrieving revision 1.50.2.1
retrieving revision 1.50.2.2
diff -C2 -d -r1.50.2.1 -r1.50.2.2
*** ERXExtensions.java 21 Mar 2003 01:19:02 -0000 1.50.2.1
--- ERXExtensions.java 10 Jul 2003 16:47:46 -0000 1.50.2.2
***************
*** 30,34 ****
*/
public class ERXExtensions {
!
/** Notification name, posted before object will change in an editing context */
public final static String objectsWillChangeInEditingContext= "ObjectsWillChangeInEditingContext";
--- 30,35 ----
*/
public class ERXExtensions {
! public static Observer observer;
!
/** Notification name, posted before object will change in an editing context */
public final static String objectsWillChangeInEditingContext= "ObjectsWillChangeInEditingContext";
***************
*** 103,107 ****
ERXLocalizer.initialize();
ERXValidationFactory.defaultFactory().configureFactory();
- ERXArrayUtilities.initialize();
}
--- 104,107 ----
***************
*** 141,146 ****
ERXConfigurationManager.defaultManager().initialize();
ERXLogger.configureLogging(System.getProperties());
!
! log().info("Initializing framework: ERXExtensions");
NSLog.setDebug(new ERXNSLogLog4jBridge(ERXNSLogLog4jBridge.DEBUG));
--- 141,146 ----
ERXConfigurationManager.defaultManager().initialize();
ERXLogger.configureLogging(System.getProperties());
!
! log().debug("Initializing framework: ERXExtensions");
NSLog.setDebug(new ERXNSLogLog4jBridge(ERXNSLogLog4jBridge.DEBUG));
***************
*** 148,151 ****
--- 148,153 ----
NSLog.setErr(new ERXNSLogLog4jBridge(ERXNSLogLog4jBridge.ERR));
+ ERXArrayUtilities.initialize();
+
// False by default
if (ERXValueUtilities.booleanValue(System.getProperty(ERXSharedEOLoader.PatchSharedEOLoadingPropertyKey))) {
***************
*** 157,162 ****
}
! Observer observer = new Observer();
! ERXRetainer.retain(observer); // has to be retained
try {
--- 159,163 ----
}
! observer = new Observer();
try {
***************
*** 659,667 ****
* new object.
*/
- // ENHANCEME: Should be able to differentiate between a deleted eo and a new eo by looking
- // at the EOGlobalID
// MOVEME: ERXEOFUtilities (when we have them)
public static boolean isNewObject(EOEnterpriseObject eo) {
! return eo.editingContext()==null || eo.editingContext().insertedObjects().containsObject(eo);
}
--- 660,669 ----
* new object.
*/
// MOVEME: ERXEOFUtilities (when we have them)
public static boolean isNewObject(EOEnterpriseObject eo) {
! if (eo.editingContext() == null) return true;
!
! EOGlobalID gid = eo.editingContext().globalIDForObject(eo);
! return gid.isTemporary();
}
***************
*** 687,691 ****
return result.lastObject();
} else if (result!=null && result.count() > 1) {
! log().warn("Attempting to get a raw primary key from an object with a compound key: " + eo);
}
return result;
--- 689,693 ----
return result.lastObject();
} else if (result!=null && result.count() > 1) {
! log().warn("Attempting to get a raw primary key from an object with a compound key: " + eo.eoShallowDescription());
}
return result;
***************
*** 1263,1269 ****
// FIXME: Should check to make sure that the key 'r' isn't already present in the url.
public static String randomizeDirectActionURL(String daURL) {
! int r=_random.nextInt();
! char c=daURL.indexOf('?')==-1 ? '?' : '&';
! return daURL+c+"r="+r;
}
/**
--- 1265,1273 ----
// FIXME: Should check to make sure that the key 'r' isn't already present in the url.
public static String randomizeDirectActionURL(String daURL) {
! synchronized(_random) {
! int r=_random.nextInt();
! char c=daURL.indexOf('?')==-1 ? '?' : '&';
! return daURL+c+"r="+r;
! }
}
/**
***************
*** 1276,1289 ****
// FIXME: Should check to make sure that the key 'r' isn't already present in the url.
public static void addRandomizeDirectActionURL(StringBuffer daURL) {
! int r=_random.nextInt();
! char c='?';
! for (int i=0; i<daURL.length(); i++) {
! if (daURL.charAt(i)=='?') {
! c='&'; break;
! }
! }
! daURL.append(c);
! daURL.append("r=");
! daURL.append(r);
}
/**
--- 1280,1295 ----
// FIXME: Should check to make sure that the key 'r' isn't already present in the url.
public static void addRandomizeDirectActionURL(StringBuffer daURL) {
! synchronized(_random) {
! int r=_random.nextInt();
! char c='?';
! for (int i=0; i<daURL.length(); i++) {
! if (daURL.charAt(i)=='?') {
! c='&'; break;
! }
! }
! daURL.append(c);
! daURL.append("r=");
! daURL.append(r);
! }
}
/**
|