commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Shayan (JIRA)" <j...@apache.org>
Subject [jira] Commented: (COLLECTIONS-306) Use Predicate in subtract
Date Tue, 02 Dec 2008 11:30:44 GMT

    [ 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.


Mime
View raw message