java-工具类

EventBus

maven

1
2
3
4
5
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>

post事件,注册的 listener 中注解了@Subscribe的方法会被执行,该方法的参数的类型需要与event类型一致,若没有类型一致的@Subscribe,则由参数类型DeadEventlistener统一处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class EventBusTest {
@Test
public void test() {
EventBus eventBus = new EventBus();
EventListener eventListener = new EventListener();
eventBus.register(eventListener);
eventBus.post("hello");
eventBus.post(123);
}
public static class EventListener {
@Subscribe
public void stringEvent(String event) {
System.out.println("event = " + event);
}
@Subscribe
public void handleDeadEvent(DeadEvent deadEvent) {
System.out.println("deadEvent = " + deadEvent);
}
}
}

junit 断言异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Student {
public boolean canVote(int age) {
if (i<=0) throw new IllegalArgumentException("age should be +ve");
if (i<18) return false;
else return true;
}
}
public class TestStudent{

@Rule
public ExpectedException thrown= ExpectedException.none();

@Test
public void canVote_throws_IllegalArgumentException_for_zero_age() {
Student student = new Student();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("age should be +ve");
student.canVote(0);
}
}

poi

在生成excel时,当为单元格填充内容为数字时,生成的 excel 的数字单元格左上角提示绿色小三角。可在填充单元格值时使用Double类型

1
2
3
4
5
6
7
XSSFCell cell = row.createCell(cellNum);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(value.matches("\\d+")){
cell.setCellValue(Double.valueOf(value));
}else{
cell.setCellValue(value);
}