java - How to print contents of a class object by field? -
i have pojo class, location used map json file class using jackson. current implementation can print out every location object in class calling,location's tostring() i'm wondering how can print example, location id= "2", name="desert"
at moment, use tostring method print contents of location:
public string tostring() { return "location [location=" + arrays.tostring(location) + ", id=" + id + ", description=" + description + ", weight=" + weight + ", name=" + name + ", exit=" + arrays.tostring(exit) +"]"; } does know how can print specific locations within location object based on field id?
this example of stored in location class when call tostring() on it:
http://hastebin.com/eruxateduz.vhdl
an example of 1 of locations within location object:
[location [location=null, id=1, description=you in city of tiberius. see long street high buildings , castle.you see exit south., weight=100, name=tiberius, exit=[exit [title=desert, direction=south]]] this pojo location class use map json fields class:
public class location { private location[] location; private int id; private string description; private string weight; private string name; private exit[] exit; private boolean visited = false; private boolean goallocation; private int approximatedistancefromgoal = 0; private location parent; public location[] getlocation() { return location; } public void setlocation(location[] location) { this.location = location; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getdescription () { return description; } public void setdescription (string description) { this.description = description; } public string getweight() { return weight; } public void setweight(string weight) { this.weight = weight; } public string getname () { return name; } public void setname (string name) { this.name = name; } public exit[] getexit() { return exit; } public void setexit(exit[] exit) { this.exit = exit; } public boolean isvisited() { return visited; } public void setvisited(boolean visited) { this.visited = visited; } public boolean isgoallocation() { return goallocation; } public void setgoallocation(boolean goallocation) { this.goallocation = goallocation; } public int getapproximatedistancefromgoal() { return approximatedistancefromgoal; } public void setapproximatedistancefromgoal(int approximatedistancefromgoal) { this.approximatedistancefromgoal = approximatedistancefromgoal; } public location getparent() { return parent; } public void setparent(location parent) { this.parent = parent; } @override public string tostring() { return "location [location=" + arrays.tostring(location) + ", id=" + id + ", description=" + description + ", weight=" + weight + ", name=" + name + ", exit=" + arrays.tostring(exit) +"]"; } }
<dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-collections4</artifactid> <version>4.0</version> </dependency> you need above dependency define predicate unless want on own.
public class location { private int id; // more stuff here private predicate<integer> filter; public location() { this.filter = truepredicate.instance; } public location(int idfilter) { this.filter = new equalprediate(idfilter); } public string tostring() { stringbuilder buffer = new stringbuilder(); if(filter.apply(this.id)) { buffer.append("location [location=" + arrays.tostring(location) + ", id=" + id + ", description=" + description + ", weight=" + weight + ", name=" + name + ", exit=" + arrays.tostring(exit) +"]"); } return buffer.tostring(); } } this code simplified visitor pattern
'visitor' -> predicate
'this' -> 'this.id'
this works because tostring() invoking tostring() of nested location objects have predicates filtering set.
if aren't in control of construction can propogate filter can take approach:
public string tostring() { stringbuilder buffer = new stringbuilder(); int = 0; for(location l = this; < locations.length; l = locations[i++]) if(filter.apply(l.id) { buffer.append("location [location=" + arrays.tostring(location) + ", id=" + id + ", description=" + description + ", weight=" + weight + ", name=" + name + ", exit=" + arrays.tostring(exit) +"]"); } return buffer.tostring(); }
Comments
Post a Comment