Skip to content

Commit

Permalink
Merge pull request #7 from onlyliuxin/master
Browse files Browse the repository at this point in the history
20170729合并
  • Loading branch information
lorcx authored Jul 29, 2017
2 parents be6e24a + 01331f6 commit 61bbb64
Show file tree
Hide file tree
Showing 111 changed files with 4,031 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coderising.dp.bridge;

public class GraphicLibrary1 {
public void draw_a_line(int x1,int y1,int x2,int y2){

}
public void draw_a_circle(int x,int y, int r){

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coderising.dp.bridge;

public class GraphicLibrary2 {
public void drawLine(int x1,int x2,int y1,int y2){

}
public void drawCircle(int x,int y, int r){

}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package com.coderising.dp.builder;

public class TagBuilder {

private TagNode rootNode;
private TagNode currentNode;
private TagNode parentNode;
public TagBuilder(String rootTagName){

rootNode = new TagNode(rootTagName);
currentNode = rootNode;
parentNode = null;
}

public TagBuilder addChild(String childTagName){

return null;
parentNode = this.currentNode;
this.currentNode = new TagNode(childTagName);
parentNode.add(currentNode);
return this;
}
public TagBuilder addSibling(String siblingTagName){


return null;
this.currentNode = new TagNode(siblingTagName);
parentNode.add(this.currentNode);
return this;

}
public TagBuilder setAttribute(String name, String value){

return null;
this.currentNode.setAttribute(name, value);
return this;
}
public TagBuilder setText(String value){

return null;
this.currentNode.setValue(value);
return this;
}
public String toXML(){
return null;
return this.rootNode.toXML();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coderising.dp.composite;

public class Line implements Shape {

@Override
public void draw() {


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coderising.dp.composite;

public class Rectangle implements Shape {

@Override
public void draw() {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.dp.composite;

public interface Shape {
public void draw();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coderising.dp.composite;

public class Square implements Shape {

@Override
public void draw() {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coderising.dp.composite;

public class Text implements Shape {

@Override
public void draw() {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.coderising.dp.builder;

import java.util.List;

public class TagBuilder {
private TagNode root;
private TagNode now;
private TagNode prev;
public TagBuilder(String rootTagName) {
root=new TagNode(rootTagName);
now=root;
}

public TagBuilder addChild(String childTagName) {
prev=now;
now=new TagNode(childTagName);
prev.add(now);

return this;
}

public TagBuilder addSibling(String siblingTagName) {
List<TagNode> children=prev.getChildren();
now=new TagNode(siblingTagName);
children.add(now);
return this;

}

public TagBuilder setAttribute(String name, String value) {
now.setAttribute(name, value);
return this;
}

public TagBuilder setText(String value) {

return this;
}

public String toXML() {
return root.toXML();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.coderising.dp.builder;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TagBuilderTest {
@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testToXML() {

TagBuilder builder = new TagBuilder("order");

String xml = builder.addChild("line-items")
.addChild("line-item").setAttribute("pid", "P3677").setAttribute("qty", "3")
.addSibling("line-item").setAttribute("pid", "P9877").setAttribute("qty", "10")
.toXML();

String expected = "<order>"
+ "<line-items>"
+ "<line-item pid=\"P3677\" qty=\"3\"/>"
+ "<line-item pid=\"P9877\" qty=\"10\"/>"
+ "</line-items>"
+ "</order>";

System.out.println(xml);
assertEquals(expected, xml);

// com.oracle.nio.BufferSecrets
//
// com.sun.corba.se.spi.extension.ServantCachingPolicy
//
// java.net.ProxySelector
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.coderising.dp.builder;

import java.util.ArrayList;
import java.util.List;

public class TagNode {
private String tagName;
private String tagValue;
private List<TagNode> children = new ArrayList<>();
private List<Attribute> attributes = new ArrayList<>();

public TagNode(String name){
this.tagName = name;
}
public void add(TagNode child){
this.children.add(child);
}
public void setAttribute(String name, String value) {
Attribute attr = findAttribute(name);
if(attr != null){
attr.value = value;
return;
}

attributes.add(new Attribute(name,value));
}
private Attribute findAttribute(String name){
for(Attribute attr : attributes){
if(attr.name.equals(name)){
return attr;
}
}
return null;
}
public void setValue(String value) {
this.tagValue = value;

}
public String getTagName() {
return tagName;
}
public List<TagNode> getChildren() {
return children;
}

private static class Attribute{
public Attribute(String name, String value) {
this.name = name;
this.value = value;
}
String name;
String value;

}
public String toXML(){
return toXML(this);
}
private String toXML(TagNode node){
StringBuilder buffer = new StringBuilder();
buffer.append("<").append(node.tagName);
if(node.attributes.size()> 0){
for(int i=0;i<node.attributes.size();i++){
Attribute attr = node.attributes.get(i);
buffer.append(" ").append(toXML(attr));
}
}
if(node.children.size()== 0){
buffer.append("/>");
return buffer.toString();
}
buffer.append(">");
for(TagNode childNode : node.children){
buffer.append(toXML(childNode));
}
buffer.append("</").append(node.tagName).append(">");


return buffer.toString();
}
private String toXML(Attribute attr){
return attr.name+"=\""+attr.value + "\"";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.coderings.dp.builder;

public class TagBuilder {

final TagNode rootNode;

TagNode iteratorNode;

TagNode prevIteratorNode;

public TagBuilder(String rootTagName) {
rootNode = new TagNode(rootTagName);
iteratorNode = rootNode;
prevIteratorNode = rootNode;
}

public TagBuilder addChild(String childTagName) {
TagNode tagNode = new TagNode(childTagName);
iteratorNode.add(tagNode);

prevIteratorNode = iteratorNode;
iteratorNode = tagNode;

return this;
}

public TagBuilder addSibling(String siblingTagName) {
TagNode tagNode = new TagNode(siblingTagName);
prevIteratorNode.add(tagNode);

iteratorNode = tagNode;

return this;
}

public TagBuilder setAttribute(String name, String value) {
iteratorNode.setAttribute(name, value);
return this;
}

public TagBuilder setText(String value) {
iteratorNode.setValue(value);
return this;
}

public String toXML() {
return rootNode.toXML();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.coderings.dp.builder;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TagBuilderTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testToXML() {

TagBuilder builder = new TagBuilder("order");

String xml = builder.addChild("line-items")
.addChild("line-item").setAttribute("pid", "P3677").setAttribute("qty", "3")
.addSibling("line-item").setAttribute("pid", "P9877").setAttribute("qty", "10")
.toXML();

String expected = "<order>"
+ "<line-items>"
+ "<line-item pid=\"P3677\" qty=\"3\"/>"
+ "<line-item pid=\"P9877\" qty=\"10\"/>"
+ "</line-items>"
+ "</order>";

System.out.println(xml);
assertEquals(expected, xml);
}

}
Loading

0 comments on commit 61bbb64

Please sign in to comment.