-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Liu Zengzeng
committed
Jul 21, 2017
1 parent
d038521
commit bc2ecb5
Showing
4 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...ents/2816977791/ood/ood-assignment/src/main/java/com/coderings/dp/builder/TagBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../2816977791/ood/ood-assignment/src/main/java/com/coderings/dp/builder/TagBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
students/2816977791/ood/ood-assignment/src/main/java/com/coderings/dp/builder/TagNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.coderings.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 + "\""; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...91/ood/ood-assignment/src/main/java/com/coderings/dp/singleton/jdk_singleton.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#Singleton in JDK | ||
|
||
## java.lang.Runtime | ||
|
||
private static Runtime currentRuntime = new Runtime(); | ||
|
||
public static Runtime getRuntime() { | ||
return currentRuntime; | ||
} | ||
|
||
## java.awt.Desktop | ||
|
||
private DesktopPeer peer; | ||
|
||
private Desktop() { | ||
peer = Toolkit.getDefaultToolkit().createDesktopPeer(this); | ||
} | ||
|
||
|
||
public static synchronized Desktop getDesktop(){ | ||
Desktop desktop = (Desktop)context.get(Desktop.class); | ||
if (desktop == null) { | ||
desktop = new Desktop(); | ||
context.put(Desktop.class, desktop); | ||
} | ||
return desktop; | ||
} | ||
|
||
## java.lang.System | ||
|
||
private static volatile SecurityManager security = null; | ||
|
||
public static SecurityManager getSecurityManager() { | ||
return security; | ||
} | ||
|