Re: Removing Duplicate Objects from Combined Arrays?
Re: Removing Duplicate Objects from Combined Arrays?
- Subject: Re: Removing Duplicate Objects from Combined Arrays?
- From: David Aspinall <email@hidden>
- Date: Thu, 21 Oct 2004 10:29:57 -0400
I have a class Called ArrayTool with several useful methods. You could
use;
/** only adds objects to the array if they are NOT already in the
array */
public static void AddObjectsToArrayIfAbsent( NSMutableArray array,
NSArray list )
{
if ((array != null) && (list != null))
{
for ( int i = 0; i < list.count(); i++ )
{
if (ContainsObject( array, list.objectAtIndex(i)) ==
false)
{
array.addObject( list.objectAtIndex(i) );
}
}
}
}
/** This method checks for an object in the array with the same
EOKeyGlobalID as the target object. This is done because 2 identical
objects from different EditingContext's are equals() == false */
public static boolean ContainsObject( NSArray array, Object value )
{
int foundMatchIndex = IndexOfEquivalentObject( array, value );
return (foundMatchIndex != NSArray.NotFound);
}
/** Finds the first instance of an object with the same GID in the
array, returns NSArray.NotFound if not found */
public static int IndexOfEquivalentObject( NSArray array, Object
value )
{
int index = NSArray.NotFound;
if ((value != null) && (array != null))
{
if ( value instanceof EOEnterpriseObject )
{
for ( int i = 0; (i < array.count()) && (index ==
NSArray.NotFound); i++)
{
Object other = array.objectAtIndex( i );
if (other instanceof EOEnterpriseObject)
{
if( EquivalentObjectsByGID(
(EOEnterpriseObject)value, (EOEnterpriseObject)other) == true )
{
index = i;
}
}
}
}
else
{
index = array.indexOfObject( value );
}
}
return index;
}
/** Compares two Enterprise Objects and confirms they are equvilant
bases on the EOKeyGlobalID's being equal (ie. same entity and primary
key) */
public static boolean EquivalentObjectsByGID( EOEnterpriseObject
one, EOEnterpriseObject two )
{
boolean isEqual = false;
/** Trim the test down a little */
if ((one != null) && (two != null) &&
(one.getClass().isInstance( two ) == true))
{
isEqual = one.equals( two );
if ( isEquals == false )
{
EOEditingContext oneEc = one.editingContext();
EOEditingContext twoEc = two.editingContext();
if ((oneEc != null) && (twoEc != null))
{
/** Global ID's are only visible within the Editing
Context that contains them. */
EOGlobalID oneGID = (EOGlobalID)oneEc.globalIDForObject(
one );
EOGlobalID twoGID =
(EOGlobalID)twoEc.globalIDForObject( two );
if ((oneGID != null) && (twoGID != null))
isEqual = oneGID.equals( twoGID );
}
}
}
return isEqual;
}
On 20-Oct-04, at 2:31 PM, Janice Cheung wrote:
Greetings!
I have a question concerning removing duplicate objects from
two (combined) arrays:
I have the following code to combine two arrays (flattened
relationships), select
the Unique Set of objects, and then do a sortOrdering to list
these objects in alphabetical
(ascending) order. Please view below:
public NSArray getAllDepartments() {
NSArray a =
(NSArray)storedValueForKey("affiliations_department");
NSArray b =
(NSArray)storedValueForKey("deptAdmins_department");
if (a == null && b ==null)
return NSArray.EmptyArray;
NSMutableSet ms = new NSMutableSet();
ms.addObjectsFromArray(a);
ms.addObjectsFromArray(b);
NSArray originalListWithDuplicates = ms.allObjects();
NSSet uniqueingSet=new NSSet(originalListWithDuplicates);
NSArray
newListWithoutDuplicates=uniqueingSet.allObjects();
EOSortOrdering ordering1 = new
EOSortOrdering("departmentName", EOSortOrdering.CompareAscending);
NSMutableArray sortOrdering = new NSMutableArray();
sortOrdering.addObject(ordering1);
return
EOSortOrdering.sortedArrayUsingKeyOrderArray(newListWithoutDuplicates,
sortOrdering);
}
Basically, "affiliations_department" and
"deptAdmins_department" are two flattened relationships in
the Person object class. I was hoping that the code authored
by James O'Loughlin on Duplicate Objects
http://wocode.com/cgi-bin/WebObjects/WOCode.woa/2/wa/ShareCodeItem?
itemId=334&wosid=NLBn8lO0hpPmSvpCCgeqR0
would work, but I've deduced that I'm something in the
implementation, as duplicate objects are appearing here,
there, and everywhere! I have been tinkering with this code,
to no avail.
Am I inappropriately manipulating these flattened relationship
NSArrays?
Any advice or guidance would be greatly appreciated!
Thanks for your help!
Best regards,
Janice
P.S. I initially attempted to add the Project Wonder ERX
framework to my application in XCode, (to remove duplicates) .. but
I came across some inexplicable (and downright
intimidating) compilation errors, so I temporarily fled that fear of
the unknown to return to my current (and yes, equally
sad) state of befuddlement.
Thanks for helping me!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden