Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -2,35 +2,27 @@ package com.iluwatar.nullobject;
|
||||
|
||||
/**
|
||||
*
|
||||
* Null Object pattern replaces null values with neutral objects.
|
||||
* Many times this simplifies algorithms since no extra null checks
|
||||
* are needed.
|
||||
* Null Object pattern replaces null values with neutral objects. Many times this simplifies
|
||||
* algorithms since no extra null checks are needed.
|
||||
* <p>
|
||||
* In this example we build a binary tree where the nodes are either
|
||||
* normal or Null Objects. No null values are used in the tree making
|
||||
* the traversal easy.
|
||||
* In this example we build a binary tree where the nodes are either normal or Null Objects. No null
|
||||
* values are used in the tree making the traversal easy.
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main( String[] args ) {
|
||||
|
||||
Node root = new NodeImpl("1",
|
||||
new NodeImpl("11",
|
||||
new NodeImpl("111",
|
||||
NullNode.getInstance(),
|
||||
NullNode.getInstance()),
|
||||
NullNode.getInstance()),
|
||||
new NodeImpl("12",
|
||||
NullNode.getInstance(),
|
||||
new NodeImpl("122",
|
||||
NullNode.getInstance(),
|
||||
NullNode.getInstance())));
|
||||
public class App {
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
root.walk();
|
||||
}
|
||||
Node root =
|
||||
new NodeImpl("1", new NodeImpl("11", new NodeImpl("111", NullNode.getInstance(),
|
||||
NullNode.getInstance()), NullNode.getInstance()), new NodeImpl("12",
|
||||
NullNode.getInstance(), new NodeImpl("122", NullNode.getInstance(),
|
||||
NullNode.getInstance())));
|
||||
|
||||
root.walk();
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,13 @@ package com.iluwatar.nullobject;
|
||||
*/
|
||||
public interface Node {
|
||||
|
||||
String getName();
|
||||
int getTreeSize();
|
||||
Node getLeft();
|
||||
Node getRight();
|
||||
void walk();
|
||||
String getName();
|
||||
|
||||
int getTreeSize();
|
||||
|
||||
Node getLeft();
|
||||
|
||||
Node getRight();
|
||||
|
||||
void walk();
|
||||
}
|
||||
|
@ -7,44 +7,44 @@ package com.iluwatar.nullobject;
|
||||
*/
|
||||
public class NodeImpl implements Node {
|
||||
|
||||
private final String name;
|
||||
private final Node left;
|
||||
private final Node right;
|
||||
|
||||
public NodeImpl(String name, Node left, Node right) {
|
||||
this.name = name;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTreeSize() {
|
||||
return 1 + left.getTreeSize() + right.getTreeSize();
|
||||
}
|
||||
private final String name;
|
||||
private final Node left;
|
||||
private final Node right;
|
||||
|
||||
@Override
|
||||
public Node getLeft() {
|
||||
return left;
|
||||
}
|
||||
public NodeImpl(String name, Node left, Node right) {
|
||||
this.name = name;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getRight() {
|
||||
return right;
|
||||
}
|
||||
@Override
|
||||
public int getTreeSize() {
|
||||
return 1 + left.getTreeSize() + right.getTreeSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@Override
|
||||
public Node getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void walk() {
|
||||
System.out.println(name);
|
||||
if (left.getTreeSize() > 0) {
|
||||
left.walk();
|
||||
}
|
||||
if (right.getTreeSize() > 0) {
|
||||
right.walk();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Node getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void walk() {
|
||||
System.out.println(name);
|
||||
if (left.getTreeSize() > 0) {
|
||||
left.walk();
|
||||
}
|
||||
if (right.getTreeSize() > 0) {
|
||||
right.walk();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,36 +9,34 @@ package com.iluwatar.nullobject;
|
||||
*/
|
||||
public class NullNode implements Node {
|
||||
|
||||
private static NullNode instance = new NullNode();
|
||||
|
||||
private NullNode() {
|
||||
}
|
||||
|
||||
public static NullNode getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTreeSize() {
|
||||
return 0;
|
||||
}
|
||||
private static NullNode instance = new NullNode();
|
||||
|
||||
@Override
|
||||
public Node getLeft() {
|
||||
return null;
|
||||
}
|
||||
private NullNode() {}
|
||||
|
||||
@Override
|
||||
public Node getRight() {
|
||||
return null;
|
||||
}
|
||||
public static NullNode getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public int getTreeSize() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void walk() {
|
||||
}
|
||||
@Override
|
||||
public Node getLeft() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getRight() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void walk() {}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.nullobject.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user