我正在使用Spring Batch实现的数据加载器,即读取多个平面文件,以提交间隔1000处理和写入pojo列表到数据库。
从文件读取的每一行都转换为包含需要在处理结果上设置的属性的pojo对象。
我有一个包含三列180行的查找表。我将每个列值保存在单独的列表中,并在谓词中迭代列表以匹配每个POJO项属性。如果在所有列表中找到匹配项,则将设置属性。以下是我使用的谓词,
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;
}
}
是否有更好的方法来过滤项目并使用番石榴进行修改。
答案 0 :(得分:2)
Guava:使用Iterables.find和Predicate进行数据库查找。使用transform将Function应用于结果。
Commons:使用FilterListIterator(或FilterIterator
)与Predicate相结合,然后TransformIterator与Transformer结合使用
不了解LambdaJ。