阅读该书时,才发现,平时一些很费劲的代码,通过Apache.common的 工具包,可以得到很大的简化。下面以问题、平时做法,推荐做法(调用common这个包来实现)来做记录
问题 1
实现toString方法,输出bean的各个属性值
平时做法
属性名+get方法,例如 "name:" + bean.getName + ";value:" + bean.getValue
推荐做法
1
|
return ReflectionToStringBuilder.toString( this ); |
问题 2
在方法一开始,要判断参数是否为空
平时做法
自己写代码做判断以及异常的处理,例如
1
2
3
4
5
|
if (StringUtils.isEmpty(para)) { log.error( "para
is null or empty" ; return null ; } |
推荐做法
使用Validate类,例如
Validate.notEmpty(para, "para must not be empty"),当para为empty时,会往外抛出异常
问题 3
计算方法执行时间
平时做法
1
2
3
|
long start
= System.currentTimeMillis(); long end
= System.currentTimeMillis(); System.out.println(end
- start); |
推荐做法
使用StopWatch,简单的情况下,这两种方法没太大区别,但当你的计算场景越复杂,StopWatch,可以让代码更清晰,更灵活些
1
2
3
4
|
StopWatch
clock = new StopWatch(); clock.start(); clock.stop(); System.out.println(clock.getTime()); |
问题 4
需要对文本进行处理
平时做法
自己写方法,以下省略血泪史四千字……
推荐做法
醒醒吧!!StringUtils 基本都帮你做了!
检查空字符串,isBlank()
缩减字符串,abbreviate()
搜索嵌套字符串串,substringBetween()
去掉字符串尾部的换行符和回车符, chomp()
……总之,基本没你什么事,直接用吧。。
问题 5
需要一组数据根据多个排序条件进行排序,且该数据不是来源于数据库,因此只能在程序中进行排序
平时做法
自己写comparator,而且要根据多个条件进行排序,因此comparator里面,就要if后又if,把自己都给绕晕了
推荐做法
将两个东西组到一起,就可以方便地解决问题:1、beanutil所带的BeanComparator,可以进行基本的排序,例如字符串默认按A-Z正序排序,数字默认按从小到大排序;2、ComparatorChain,可以把多个Comparator组到一起,按添加的先后顺序,进行排序,例如如果通过第一个comparator对比时,两者相同,则再用下一个comparator进行对比。例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Person
person1 = new Person(); person1.setLastName( "Cayne" ); person1.setFirstName( "James" ); person1.setAge( 21 ); Person
person2 = new Person(); person2.setLastName( "Aayne" ); person2.setFirstName( "James" ); person2.setAge( 85 ); Person
person3 = new Person(); person3.setLastName( "Payne" ); person3.setFirstName( "Susan" ); person3.setAge( 29 ); Person[]
persons = new Person[]
{person1,person2,person3}; ComparatorChain
comparatorChain = new ComparatorChain(); comparatorChain.addComparator( new BeanComparator( "lastName" )); comparatorChain.addComparator( new BeanComparator( "firstName" )); comparatorChain.addComparator( new BeanComparator( "age" ), true ); //设为true,是将默认的排序规则进行反转,如果之前是按正序,设为true之后则按正序 Arrays.sort(persons,comparatorChain); |
问题 6
需要关闭各种流(InputStream等)
平时做法
在finally块中关闭流。但为防止close方法抛出异常,需要再包一层try/catch。例如
1
2
3
4
5
6
|
try { if (inStream
!= null )
{ inStream.close(); } } catch (IOException
ioe) { } |
推荐做法
同样还是在finally块中关闭流,但可以通过
IOUtils.closeQuietly(inStream)
一行代码来解决上面五行代码做的事情(1、前提是你不用对catch到的异常做处理;2、这个方法的实现,其实就是那五行代码。)
问题 7
有一组数据,作为一个值写到了配置文件中,之后需要再读出来,还原成一组数据
配置文件可能像这样:macToImport = iPhone1,iPhone2,iPhone3。
平时做法
小组有人封装过读取配置文件的工具类,但读出来的值都为String类型的。例如我只能读到“iPhone1,iPhone2,iPhone3”,然后我再用spilt(",")把这个str转换成list
推荐做法
通过Configuration类的getList方法。
例如:
Configuration config = new PropertiesConfiguration("test.properties");
List names = config.getList("names");
问题 8
需要统计list中,有多少个元素满足给定条件
平时做法
//伪代码
int num = 0;
for(bean:list)
{
if(某个条件)
{
keum ++;
}
}
推荐做法
使用CollectionUtils.countMatches(list,Predicate);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
List<Person>
persons = new ArrayList<Person>(); Predicate
isAdult = new Predicate(){ @Override public boolean evaluate(Object
arg0) { Person
person = (Person) arg0; if (person.getAge()
< 19 ) { return false ; } else { return true ; } } }; int adoutNum
= CollectionUtils.countMatches(persons, isAdult); |
问题 9
页面的表单(form)提交到后台,后台收到值后,根据值类型的不同,需做不同的处理,再填到一个bean中
平时做法
//如果表单提交的值和dbVersionInfo中的对应值不一样,则将表单值设到dbVersionInfo中
if(!copyRight.equals(dbVersionInfo.getCopyRight()))
{
dbVersionInfo.setCopyRight(copyRight);
}
if(!firstName.equals(dbVersionInfo.getFirstName()))
{
dbVersionInfo.setFirstName(firstName);
}
推荐做法
先定义方法
private void isValueModify(VersionDetailInfo dbVersionInfo, String valueInput, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
if(null != valueInput && !valueInput.equals(BeanUtils.getProperty(dbVersionInfo, name)))
{
BeanUtils.setProperty(dbVersionInfo, name, valueInput);
valueChange ++;
}
}
isValueModify(dbVersionInfo, copyRight, "copyRight");
isValueModify(dbVersionInfo,firstName, "firstName");
注意使用该方法必须有一个前提,你的数据bean(如本例中的VersionDetailInfo)需要按照java bean的定义,有set和get方法,例如你的bean中有copyRight这个属性,那也要有setCopyRight和getCopyRight这两个方法,然后BeanUtils.getProperty中的参数name就是"copyRight"