You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Chris Shayan (JIRA)" <ji...@apache.org> on 2008/11/08 13:10:44 UTC
[jira] Created: (COLLECTIONS-306) Use Predicate in subtract
Use Predicate in subtract
-------------------------
Key: COLLECTIONS-306
URL: https://issues.apache.org/jira/browse/COLLECTIONS-306
Project: Commons Collections
Issue Type: New Feature
Components: Collection
Environment: all OSs
Reporter: Chris Shayan
It is good idea to use Predicate in subtract method, I've developed myself the mentioned method and now I am testing it. I mean we should have following methods:
The one already exist is:
public static Collection subtract(Collection a, Collection b)
I offer to have one more which is:
public static Collection subtract(Collection a, Collection b, Predicate predicate)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (COLLECTIONS-306) Use Predicate in subtract
Posted by "Chris Shayan (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/COLLECTIONS-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12652337#action_12652337 ]
Chris Shayan commented on COLLECTIONS-306:
------------------------------------------
package com.chrisshayan;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Created by IntelliJ IDEA.
* User: Chris Shayan and Nasehzade
* Date: Nov 8, 2008
* Time: 1:59:36 PM
*/
public class TestSubtract {
public static class Entity {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String toString() {
return String.valueOf(getNumber());
}
}
/**
*
* @param a
* @param b
* @param p
* @return
*/
public static Collection subtract(final Collection a, final Collection b, Predicate p) {
ArrayList list = new ArrayList(a);
for (int i = 0; i < list.size(); i++) {
Object o = list.get(i);
if(!p.evaluate(o)) {
list.remove(i);
}
}
return list;
}
public static void main(String[] args) {
// simple();
List all = new ArrayList(3);
Entity entity1 = new TestSubtract.Entity();
entity1.setNumber(1);
Entity entity2 = new TestSubtract.Entity();
entity2.setNumber(2);
Entity entity3 = new TestSubtract.Entity();
entity3.setNumber(3);
all.add(entity1);
all.add(entity2);
all.add(entity3);
final List odd = new ArrayList();
Entity entity4 = new TestSubtract.Entity();
entity4.setNumber(1);
Entity entity5 = new TestSubtract.Entity();
entity5.setNumber(3);
odd.add(entity4);
odd.add(entity5);
List all2 = new ArrayList(all);
/*
CollectionUtils.filter(all2, new Predicate() {
public boolean evaluate(Object o) {
Entity entity = (Entity) o;
for (int i = 0; i < odd.size(); i++) {
Entity oddEntity = (Entity) odd.get(i);
if (entity.getNumber() == oddEntity.getNumber())
return false;
}
return true;
}
});
*/
Collection c = subtract(all2, odd, new Predicate() {
public boolean evaluate(Object o) {
Entity entity = (Entity) o;
for (int i = 0; i < odd.size(); i++) {
Entity oddEntity = (Entity) odd.get(i);
if (entity.getNumber() == oddEntity.getNumber())
return false;
}
return true;
}
});
List even = new ArrayList(c);
for (int i = 0; i < even.size(); i++) {
Object o = even.get(i);
System.out.println("o = " + o);
}
}
private static void simple() {
List all = new ArrayList(3);
all.add(Integer.valueOf("1"));
all.add(Integer.valueOf("2"));
all.add(Integer.valueOf("3"));
List odd = new ArrayList();
odd.add(Integer.valueOf("1"));
odd.add(Integer.valueOf("3"));
Collection evenCollection = CollectionUtils.subtract(all, odd);
List even = new ArrayList(evenCollection);
for (int i = 0; i < even.size(); i++) {
Object o = even.get(i);
System.out.println("o = " + o);
}
}
}
> Use Predicate in subtract
> -------------------------
>
> Key: COLLECTIONS-306
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-306
> Project: Commons Collections
> Issue Type: New Feature
> Components: Collection
> Environment: all OSs
> Reporter: Chris Shayan
> Original Estimate: 0.75h
> Remaining Estimate: 0.75h
>
> It is good idea to use Predicate in subtract method, I've developed myself the mentioned method and now I am testing it. I mean we should have following methods:
> The one already exist is:
> public static Collection subtract(Collection a, Collection b)
> I offer to have one more which is:
> public static Collection subtract(Collection a, Collection b, Predicate predicate)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (COLLECTIONS-306) Use Predicate in subtract
Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/COLLECTIONS-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12654739#action_12654739 ]
Henri Yandell commented on COLLECTIONS-306:
-------------------------------------------
I'm missing something here - I don't see where the b list is used in your proposed method.
It looks like you only use it in the Predicate itself and the commented out filter method is cleaner.
> Use Predicate in subtract
> -------------------------
>
> Key: COLLECTIONS-306
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-306
> Project: Commons Collections
> Issue Type: New Feature
> Components: Collection
> Environment: all OSs
> Reporter: Chris Shayan
> Original Estimate: 0.75h
> Remaining Estimate: 0.75h
>
> It is good idea to use Predicate in subtract method, I've developed myself the mentioned method and now I am testing it. I mean we should have following methods:
> The one already exist is:
> public static Collection subtract(Collection a, Collection b)
> I offer to have one more which is:
> public static Collection subtract(Collection a, Collection b, Predicate predicate)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (COLLECTIONS-306) Use Predicate in subtract
Posted by "Chris Shayan (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/COLLECTIONS-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12645976#action_12645976 ]
Chris Shayan commented on COLLECTIONS-306:
------------------------------------------
I've developed the method myself please test it by other developers, the code is:
public static Collection subtract(final Collection a, final Collection b, Predicate p) {
ArrayList list = new ArrayList(a);
for (int i = 0; i < list.size(); i++) {
Object o = list.get(i);
if(!p.evaluate(o)) {
list.remove(i);
}
}
return list;
}
> Use Predicate in subtract
> -------------------------
>
> Key: COLLECTIONS-306
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-306
> Project: Commons Collections
> Issue Type: New Feature
> Components: Collection
> Environment: all OSs
> Reporter: Chris Shayan
> Original Estimate: 0.75h
> Remaining Estimate: 0.75h
>
> It is good idea to use Predicate in subtract method, I've developed myself the mentioned method and now I am testing it. I mean we should have following methods:
> The one already exist is:
> public static Collection subtract(Collection a, Collection b)
> I offer to have one more which is:
> public static Collection subtract(Collection a, Collection b, Predicate predicate)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (COLLECTIONS-306) Use Predicate in subtract
Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/COLLECTIONS-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12645982#action_12645982 ]
Henri Yandell commented on COLLECTIONS-306:
-------------------------------------------
Thanks Chris - could we have a unit test please (either Chris or someone else)?
> Use Predicate in subtract
> -------------------------
>
> Key: COLLECTIONS-306
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-306
> Project: Commons Collections
> Issue Type: New Feature
> Components: Collection
> Environment: all OSs
> Reporter: Chris Shayan
> Original Estimate: 0.75h
> Remaining Estimate: 0.75h
>
> It is good idea to use Predicate in subtract method, I've developed myself the mentioned method and now I am testing it. I mean we should have following methods:
> The one already exist is:
> public static Collection subtract(Collection a, Collection b)
> I offer to have one more which is:
> public static Collection subtract(Collection a, Collection b, Predicate predicate)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.