Skip to content

Commit 31ee2f0

Browse files
committed
Polish Stage 7 Lesson 2
1 parent 0bf24b6 commit 31ee2f0

19 files changed

+698
-0
lines changed

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/stage-7/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<name>「一入 Java 深似海 」系列 :: 第七期</name>
1515
<modules>
1616
<module>stage-7-lesson-1</module>
17+
<module>stage-7-lesson-2</module>
1718
</modules>
1819

1920
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>stage-7</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-7-lesson-2</artifactId>
13+
<name>「一入 Java 深似海 」系列 :: 第七期 :: 第二节</name>
14+
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.segmentfault.deep.in.java.beans.customization;
2+
3+
import java.beans.PropertyEditor;
4+
import java.beans.PropertyEditorSupport;
5+
6+
import static com.segmentfault.deep.in.java.beans.properties.Person.isNumeric;
7+
8+
/**
9+
* Person id 属性编辑器
10+
* Id String -> Long
11+
*/
12+
public class IdPropertyEditor extends PropertyEditorSupport {
13+
14+
public void setAsText(String text) {
15+
if (isNumeric(text)) {
16+
setValue(Long.valueOf(text));
17+
}
18+
}
19+
20+
/**
21+
* 覆盖父类方法
22+
*
23+
* @return
24+
*/
25+
@Override
26+
public Long getValue() {
27+
return (Long) super.getValue();
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.segmentfault.deep.in.java.beans.customization;
2+
3+
import com.segmentfault.deep.in.java.beans.properties.Person;
4+
5+
import java.beans.BeanInfo;
6+
import java.beans.IntrospectionException;
7+
import java.beans.Introspector;
8+
import java.beans.PropertyEditor;
9+
import java.util.Date;
10+
import java.util.stream.Stream;
11+
12+
public class PersonBeanCustomization {
13+
14+
public static void main(String[] args) throws IntrospectionException {
15+
// 排除 java.lang.Object.class 类定义的干扰
16+
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class);
17+
18+
Person personBean = new Person();
19+
20+
Stream.of(beanInfo.getPropertyDescriptors())
21+
.filter(propertyDescriptor -> "id".equals(propertyDescriptor.getName())) // 过滤 "id" 属性
22+
.findFirst()
23+
.ifPresent(idPropertyDescriptor -> {
24+
// 为 "id" 属性描述注册属性修改器
25+
idPropertyDescriptor.setPropertyEditorClass(IdPropertyEditor.class);
26+
PropertyEditor propertyEditor = idPropertyDescriptor.createPropertyEditor(personBean);
27+
// 添加属性变化事件
28+
propertyEditor.addPropertyChangeListener(event -> {
29+
personBean.setId((Long) propertyEditor.getValue());
30+
});
31+
// 通过 setAsText(String) 方法模拟 <property name="id">1</property>
32+
propertyEditor.setAsText("1");
33+
}
34+
);
35+
36+
Stream.of(beanInfo.getPropertyDescriptors())
37+
.filter(propertyDescriptor -> "updateTime".equals(propertyDescriptor.getName())) // 过滤 "updateTime" 属性
38+
.findFirst()
39+
.ifPresent(propertyDescriptor -> {
40+
// 为 "updateTime" 属性描述注册属性修改器
41+
propertyDescriptor.setPropertyEditorClass(UpdateTimePropertyEditor.class);
42+
PropertyEditor propertyEditor = propertyDescriptor.createPropertyEditor(personBean);
43+
// 添加属性变化事件
44+
propertyEditor.addPropertyChangeListener(event -> {
45+
personBean.setUpdateTime((Date) propertyEditor.getValue());
46+
});
47+
// 通过 setAsText(String) 方法模拟 <property name="updateTime">2019-10-23 23:16:00</property>
48+
propertyEditor.setAsText("2019-10-23 23:16:00");
49+
}
50+
);
51+
52+
System.out.println("当前 person = " + personBean);
53+
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.segmentfault.deep.in.java.beans.customization;
2+
3+
public class PersonBeanDemo {
4+
5+
public static void main(String[] args) {
6+
7+
/**
8+
* <bean id="person" class="com.segmentfault.deep.in.java.beans.properties.Person" >
9+
* <property name="id">1</property> <!-- id 类型是 Long (String to Long) -->
10+
* <property name="name">小马哥</property> <!-- name 类型是 String -->
11+
* <property name="age">34</property> <!-- age 类型是 int (String to int) -->
12+
* </bean>
13+
*/
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.segmentfault.deep.in.java.beans.customization;
2+
3+
import java.beans.PropertyEditorSupport;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
7+
public class UpdateTimePropertyEditor extends PropertyEditorSupport {
8+
9+
public void setAsText(String text) {
10+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
11+
try {
12+
setValue(format.parse(text));
13+
} catch (ParseException e) {
14+
e.printStackTrace();
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.segmentfault.deep.in.java.beans.event;
2+
3+
import java.util.EventObject;
4+
5+
public class ApplicationEvent extends EventObject {
6+
7+
private final long timestamp;
8+
9+
/**
10+
* Constructs a prototypical Event.
11+
*
12+
* @param source the object on which the Event initially occurred
13+
* @throws IllegalArgumentException if source is null
14+
*/
15+
public ApplicationEvent(Object source) {
16+
super(source);
17+
this.timestamp = System.currentTimeMillis();
18+
}
19+
20+
public long getTimestamp() {
21+
return timestamp;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "ApplicationEvent{" +
27+
"timestamp=" + timestamp +
28+
", source=" + source +
29+
'}';
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.segmentfault.deep.in.java.beans.event;
2+
3+
import java.util.EventListener;
4+
5+
/**
6+
* 应用事件监听器
7+
*
8+
* @see EventListener
9+
* @param <E> ApplicationEvent 以及它的子类型
10+
*/
11+
public interface ApplicationEventListener<E extends ApplicationEvent> extends EventListener {
12+
13+
/**
14+
* 处理事件
15+
* @param event
16+
*/
17+
void onEvent(E event);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.segmentfault.deep.in.java.beans.event;
2+
3+
public interface ApplicationEventListenerRegistry {
4+
5+
void addApplicationEventListener(ApplicationEventListener<?> listener);
6+
7+
void removeApplicationEventListener(ApplicationEventListener<?> listener);
8+
9+
ApplicationEventListener[] getApplicationEventListeners();
10+
11+
ApplicationEventListener[] getApplicationEventListeners(Class<? extends ApplicationEvent> eventType);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.segmentfault.deep.in.java.beans.event;
2+
3+
/**
4+
* {@link ApplicationEvent} 广播器
5+
*/
6+
public class ApplicationEventMulticaster {
7+
8+
private final ApplicationEventListenerRegistry registry;
9+
10+
public ApplicationEventMulticaster() {
11+
this.registry = new SimpleApplicationEventListenerRegistry();
12+
}
13+
14+
public ApplicationEventMulticaster(ApplicationEventListenerRegistry registry) {
15+
this.registry = registry;
16+
}
17+
18+
public void addApplicationEventListener(ApplicationEventListener<?> listener) {
19+
registry.addApplicationEventListener(listener);
20+
}
21+
22+
public void removeApplicationEventListener(ApplicationEventListener<?> listener) {
23+
registry.removeApplicationEventListener(listener);
24+
}
25+
26+
public ApplicationEventListener[] getApplicationEventListeners() {
27+
return registry.getApplicationEventListeners();
28+
}
29+
30+
public ApplicationEventListener[] getApplicationEventListeners(Class<? extends ApplicationEvent> eventType) {
31+
return registry.getApplicationEventListeners(eventType);
32+
}
33+
34+
public void multicastEvent(ApplicationEvent event) {
35+
// 逐一传递
36+
Class<? extends ApplicationEvent> eventType = event.getClass();
37+
for (ApplicationEventListener listener : getApplicationEventListeners(eventType)) {
38+
listener.onEvent(event);
39+
}
40+
}
41+
42+
public static void main(String[] args) {
43+
44+
// displaySimpleEvent();
45+
46+
displayGenericEvent();
47+
48+
}
49+
50+
51+
private static void displayGenericEvent() {
52+
53+
ApplicationEventMulticaster multicaster =
54+
new ApplicationEventMulticaster(new GenericApplicationEventListenerRegistry());
55+
56+
multicaster.addApplicationEventListener(new MyEventListener());
57+
multicaster.addApplicationEventListener(new MyEventListener2());
58+
59+
// 传播的是 ApplicationEvent,MyEventListener 需要 MyEvent 类型,属于前者子类
60+
multicaster.multicastEvent(new MyEvent("2019"));
61+
}
62+
63+
64+
private static void displaySimpleEvent() {
65+
ApplicationEventMulticaster multicaster = new ApplicationEventMulticaster();
66+
// 注册一个事件监听器
67+
multicaster.addApplicationEventListener(event -> {
68+
System.out.println("处理事件-1 : " + event);
69+
});
70+
multicaster.addApplicationEventListener(event -> {
71+
System.out.println("处理事件-2 : " + event);
72+
});
73+
multicaster.addApplicationEventListener(event -> {
74+
System.out.println("处理事件-3 : " + event);
75+
});
76+
77+
// 广播事件
78+
multicaster.multicastEvent(new ApplicationEvent("Hello,World"));
79+
}
80+
81+
}

0 commit comments

Comments
 (0)