用于过滤和修改列表项的 Lambdaj 或 apache 谓词
我正在开发一个数据加载器,使用 Spring Batch 实现,即读取多个平面文件,处理 pojo 列表并将其以 1000 的提交间隔写入数据库。
从文件读取的每一行都会转换为一个 pojo 对象,其中包含需要在处理结果上设置的属性。
我有一个查找表,其中包含 3 列 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;
}
}
有没有更好的方法来过滤项目并使用番石榴进行修改。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Guava:使用Iterables.find 带有 执行数据库查找的谓词。应用 转换 为带有 函数。
Commons:使用FilterListIterator(或
FilterIterator
)与 谓词,然后是 TransformIterator 与 变压器。不知道 LambdaJ 怎么样。
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.