Question

I am working on a data loader, implemented using Spring Batch i.e. reading multiple flat files, processing and writing the list of pojo at commit interval of 1000 to database.
Each line read from file is converted to a pojo object containing property that needs to be set on the outcome of processing.

I have a lookup table that contains three columns with 180 rows. I keep each column values in a separate lists and iterate the list in a predicate to match with the each POJO item property.if a match is found in all the lists , then a property will be set.The following is the predicates i use,

public class LogicProcessor<I, O> implements ItemProcessor<I, O> {

    private Map[] params ;

    public void setParams( Map[] params )
    {
        this.params = params;
    }

    public O process(I item) throws Exception 
    {
        System.out.println(params  );

        List container      =  (List) params[1].get("SRVC");
        final List callInd      =  (List) container.get(0);
        final List totaltype        =  (List) container.get(1);
        final List servicetype  =   (List) container.get(2);

        Predicate<I> callIndipredicate = new Predicate<I>() {
            public boolean apply(I input) 
            {
                boolean flag=false;
                for (int i=0;i<callInd.size();i++)
                {
                    if ( "*".equals(callInd.get(i)))
                    {
                        flag= true;
                        break;
                    } else
                    {
                        try 
                        {
                            if (BeanUtils.getProperty(input,"CALLINDICATOR").equals(callInd.get(i)))
                            {
                                flag = true;
                                break;
                            } else
                            {
                                flag= false;
                                break;
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        }

                    }
                }
                return flag;
            }
        };

        Predicate<I> totaltyppredicate = new Predicate<I>() {
            public boolean apply(I input) 
            {
                boolean flag=false;
                for (int i=0;i<totaltype.size();i++)
                {
                    try 
                    {
                        if (BeanUtils.getProperty(input,"TOTALTYPE").equals(totaltype.get(i)))
                        {
                            flag = true;
                            break;
                        } else
                        {
                            flag= false;
                            break;
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }
                return flag; 
            }
        };

        Predicate<I>srvctypepredicate = new Predicate<I>() {
            public boolean apply(I input) 
            {
                boolean flag=false;
                for (int i=0;i<servicetype.size();i++)
                {
                    try 
                    {
                        if (BeanUtils.getProperty(input,"SERVICETYPE").equals(servicetype.get(i)))
                        {
                            flag = true;
                            break;
                        } else
                        {
                            flag= false;
                            break;
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }
                return flag;                
            }
        };

        Predicate<I> isFound = Predicates.and(callIndipredicate,totaltyppredicate,srvctypepredicate);
        int preorPost=  Iterables.indexOf(Arrays.asList(item), isFound) ;
        System.out.println(preorPost);
        if (preorPost >=0)
        {
            BeanUtils.setProperty(item, "PREPOST", '0');
        } else
        {
            BeanUtils.setProperty(item, "PREPOST", 'X');
        }
        return (O) item;
    }

}

Is there a better way to filter the items and modify using guava.

Was it helpful?

Solution

Guava: Use Iterables.find with a Predicate that does the DB lookup. Apply transform to the result with a Function.

Commons: Use a FilterListIterator (or FilterIterator) combined with a Predicate, then a TransformIterator with a Transformer.

Don't know about LambdaJ.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top