Total Pageviews

2016/02/03

[Apache Commons BeanUtils] How to exclude an Object from a Collection

Problem
Assume I have a dropdown list which name 基金法人類別. 
Our customer requested to remove the first option, i.e. 普通基金, from the dropdownlist


Original code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
    @RequestMapping(value = "/getAgeTypeList", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Dbm002fbVo> getAgeTypeList(
            @RequestBody(required = true) final Dbm038eFormBean formBean, Alerter alerter) {
        List<Dbm002fbVo> result = new ArrayList<Dbm002fbVo>();

        List<Dbm002fb> dbm002fbs = dbm038eService.findDropdownList("AGTPE");

        if (dbm002fbs != null && dbm002fbs.size() > 0) {
            for (Dbm002fb dbm002fb : dbm002fbs) {

                Dbm002fbVo vo = new Dbm002fbVo();
                convertToDbm002fbVo(dbm002fb, vo);

                result.add(vo);
            }
        } else {
            throw new RuntimeException("查無 基金法人類別 下拉單 (kind_code='AGTPE'");
        }

        return result;
    }


How to
We can use Apache commons BeanUtils to remove the specific object from a Collection.
Updated code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    @RequestMapping(value = "/getAgeTypeList", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Dbm002fbVo> getAgeTypeList(
            @RequestBody(required = true) final Dbm038eFormBean formBean, Alerter alerter) {
        List<Dbm002fbVo> result = new ArrayList<Dbm002fbVo>();

        List<Dbm002fb> dbm002fbs = dbm038eService.findDropdownList("AGTPE");

        List<Dbm002fb> newDbm002fbs = excludeCodeNoIsZero(dbm002fbs);

        if (newDbm002fbs != null && newDbm002fbs.size() > 0) {
            for (Dbm002fb dbm002fb : newDbm002fbs) {

                Dbm002fbVo vo = new Dbm002fbVo();
                convertToDbm002fbVo(dbm002fb, vo);

                result.add(vo);
            }
        } else {
            throw new RuntimeException("查無 基金法人類別 下拉單 (kind_code='AGTPE'");
        }

        return result;
    }

    private List<Dbm002fb> excludeCodeNoIsZero(List<Dbm002fb> dbm002fbs) {
        /**
         * The BeanPropertyValueEqualsPredicate constructor takes two parameters which determine
         * what property will be evaluated on the target object and what its expected value should
         * be.
         */
        BeanPropertyValueEqualsPredicate codeNoPredicate = new BeanPropertyValueEqualsPredicate(
                "codeNo", BigInteger.ZERO);
        /**
         * Create a new Predicate that returns true if none of the specified predicates are true. If
         * the array of predicates is empty, then this predicate returns true.
         */
        Predicate predicate = PredicateUtils.nonePredicate(new Predicate[] { codeNoPredicate });

        /**
         * Selects all elements from input collection which match the given predicate into an output
         * collection.
         */
        @SuppressWarnings("unchecked")
        List<Dbm002fb> newDbm002fbs = (List<Dbm002fb>) CollectionUtils.select(dbm002fbs, predicate);

        return newDbm002fbs;
    }



No comments: