Skip to content

Commit 3e28735

Browse files
authored
Merge pull request onlyliuxin#13 from onlyliuxin/master
合并老师代码
2 parents ab8980d + dddbc14 commit 3e28735

File tree

234 files changed

+7625
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+7625
-52
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.coderising</groupId>
6+
<artifactId>design-patterns-enan</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>design-patterns-enan</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.projectlombok</groupId>
27+
<artifactId>lombok</artifactId>
28+
<version>1.16.16</version>
29+
<optional>true</optional>
30+
</dependency>
31+
32+
</dependencies>
33+
<repositories>
34+
<repository>
35+
<id>aliyunmaven</id>
36+
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
37+
</repository>
38+
</repositories>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.3</version>
46+
<configuration>
47+
<source>1.8</source>
48+
<target>1.8</target>
49+
<encoding>${project.build.sourceEncoding}</encoding>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package first;
2+
3+
public class TagBuilder {
4+
5+
private TagNode tagNode;
6+
private TagNode currentTagNode;
7+
private TagNode superTagNode;
8+
9+
public TagBuilder(String tagName) {
10+
this.tagNode = new TagNode(tagName);
11+
this.currentTagNode = this.tagNode;
12+
this.superTagNode = null;
13+
}
14+
15+
public TagBuilder addChild(String childTagName) {
16+
this.superTagNode = this.currentTagNode;
17+
TagNode tagNode = new TagNode(childTagName);
18+
this.currentTagNode.add(tagNode);
19+
this.currentTagNode = tagNode;
20+
return this;
21+
}
22+
23+
public TagBuilder setAttribute(String key, String value) {
24+
this.currentTagNode.setAttribute(key, value);
25+
return this;
26+
}
27+
28+
public TagBuilder addSibling(String siblingTagName) {
29+
TagNode tagNode = new TagNode(siblingTagName);
30+
this.superTagNode.add(tagNode);
31+
this.currentTagNode = tagNode;
32+
return this;
33+
}
34+
35+
public TagNode build() {
36+
return tagNode;
37+
}
38+
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package first;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class TagBuilderTest {
10+
11+
@Before
12+
public void setUp() throws Exception {
13+
}
14+
15+
@After
16+
public void tearDown() throws Exception {
17+
}
18+
19+
@Test
20+
public void testToXML() {
21+
22+
TagBuilder builder = new TagBuilder("order");
23+
24+
String xml = builder.addChild("line-items")
25+
.addChild("line-item").setAttribute("pid", "P3677").setAttribute("qty", "3")
26+
.addSibling("line-item").setAttribute("pid", "P9877").setAttribute("qty", "10")
27+
.build()
28+
.toXML();
29+
30+
String expected = "<order>"
31+
+ "<line-items>"
32+
+ "<line-item pid=\"P3677\" qty=\"3\"/>"
33+
+ "<line-item pid=\"P9877\" qty=\"10\"/>"
34+
+ "</line-items>"
35+
+ "</order>";
36+
37+
System.out.println(xml);
38+
assertEquals(expected, xml);
39+
}
40+
41+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package first;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class TagNode {
7+
private String tagName;
8+
private String tagValue;
9+
private List<TagNode> children = new ArrayList<>();
10+
private List<Attribute> attributes = new ArrayList<>();
11+
12+
public TagNode(String name){
13+
this.tagName = name;
14+
}
15+
public void add(TagNode child){
16+
this.children.add(child);
17+
}
18+
public void setAttribute(String name, String value) {
19+
Attribute attr = findAttribute(name);
20+
if(attr != null){
21+
attr.value = value;
22+
return;
23+
}
24+
25+
attributes.add(new Attribute(name,value));
26+
}
27+
private Attribute findAttribute(String name){
28+
for(Attribute attr : attributes){
29+
if(attr.name.equals(name)){
30+
return attr;
31+
}
32+
}
33+
return null;
34+
}
35+
public void setValue(String value) {
36+
this.tagValue = value;
37+
38+
}
39+
public String getTagName() {
40+
return tagName;
41+
}
42+
public List<TagNode> getChildren() {
43+
return children;
44+
}
45+
46+
private static class Attribute{
47+
public Attribute(String name, String value) {
48+
this.name = name;
49+
this.value = value;
50+
}
51+
String name;
52+
String value;
53+
54+
}
55+
public String toXML(){
56+
return toXML(this);
57+
}
58+
private String toXML(TagNode node){
59+
StringBuilder buffer = new StringBuilder();
60+
buffer.append("<").append(node.tagName);
61+
if(node.attributes.size()> 0){
62+
for(int i=0;i<node.attributes.size();i++){
63+
Attribute attr = node.attributes.get(i);
64+
buffer.append(" ").append(toXML(attr));
65+
}
66+
}
67+
if(node.children.size()== 0){
68+
buffer.append("/>");
69+
return buffer.toString();
70+
}
71+
buffer.append(">");
72+
for(TagNode childNode : node.children){
73+
buffer.append(toXML(childNode));
74+
}
75+
buffer.append("</").append(node.tagName).append(">");
76+
77+
78+
return buffer.toString();
79+
}
80+
private String toXML(Attribute attr){
81+
return attr.name+"=\""+attr.value + "\"";
82+
}
83+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.coderising</groupId>
6+
<artifactId>ds-answer</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>ds-answer</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
</dependency>
24+
25+
</dependencies>
26+
<repositories>
27+
<repository>
28+
<id>aliyunmaven</id>
29+
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
30+
</repository>
31+
</repositories>
32+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.coderising.download;
2+
3+
import com.coderising.download.api.Connection;
4+
5+
public class DownloadThread extends Thread{
6+
7+
Connection conn;
8+
int startPos;
9+
int endPos;
10+
11+
public DownloadThread( Connection conn, int startPos, int endPos){
12+
13+
this.conn = conn;
14+
this.startPos = startPos;
15+
this.endPos = endPos;
16+
}
17+
public void run(){
18+
19+
}
20+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.coderising.download;
2+
3+
import com.coderising.download.api.Connection;
4+
import com.coderising.download.api.ConnectionException;
5+
import com.coderising.download.api.ConnectionManager;
6+
import com.coderising.download.api.DownloadListener;
7+
8+
9+
public class FileDownloader {
10+
11+
String url;
12+
13+
DownloadListener listener;
14+
15+
ConnectionManager cm;
16+
17+
18+
public FileDownloader(String _url) {
19+
this.url = _url;
20+
21+
}
22+
23+
public void execute(){
24+
// 在这里实现你的代码, 注意: 需要用多线程实现下载
25+
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
26+
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定)
27+
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
28+
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
29+
// 具体的实现思路:
30+
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度
31+
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
32+
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
33+
// 3. 把byte数组写入到文件中
34+
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法
35+
36+
// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
37+
Connection conn = null;
38+
try {
39+
40+
conn = cm.open(this.url);
41+
42+
int length = conn.getContentLength();
43+
44+
new DownloadThread(conn,0,length-1).start();
45+
46+
} catch (ConnectionException e) {
47+
e.printStackTrace();
48+
}finally{
49+
if(conn != null){
50+
conn.close();
51+
}
52+
}
53+
54+
55+
56+
57+
}
58+
59+
public void setListener(DownloadListener listener) {
60+
this.listener = listener;
61+
}
62+
63+
64+
65+
public void setConnectionManager(ConnectionManager ucm){
66+
this.cm = ucm;
67+
}
68+
69+
public DownloadListener getListener(){
70+
return this.listener;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)