I have an object CategoryBean where are defined a list of other objects CategoryBean (its children) and another object CategoryBean (its parent).
Cloning its children is not a problem, but cloning its parent isn't working. Indeed, when I have my object A and its parent P, when I go through P and get the list of its children (where I should fine A), A has not the same reference than the child of P. The objects equal ("attributly" speaking) but are not the same.
Here is my class and its attributes :
public class CategoryBean implements Externalizable, Cloneable {
public static final boolean ACTIVE = true;
public static final boolean INACTIVE = false;
public static final int VALIDATED = 1;
public static final int NON_OBSERVED = 2;
public static final int IN_PROGRESS = 3;
public static final int PARTIEL = 4;
private String perimetre;
private CategoryBean parentCategory;
private List<CategoryBean> categoryList = new LinkedList<CategoryBean>();
protected String title;
/**
* Category validée ou non
*/
protected int state = NON_OBSERVED;
/**
* Category active ou inactive
*/
protected boolean activated = ACTIVE;
/**
* #return the parentCategory
*/
public CategoryBean getParentCategory() {
return parentCategory;
}
/**
* #param parentCategory
* the parentCategory to set
*/
public void setParentCategory(CategoryBean parentCategory) {
this.parentCategory = parentCategory;
}
/**
* #return the perimetre
*/
public String getPerimetre() {
return perimetre != null ? perimetre.trim() : null;
}
/**
* #param perimetre
* the perimetre to set
*/
public void setPerimetre(String perimetre) {
this.perimetre = perimetre != null ? perimetre.trim() : null;
}
/**
* #return the category
*/
public List<CategoryBean> getCategoryList() {
return categoryList;
}
/**
* #param category
* the category to set
*/
public void setCategoryList(List<CategoryBean> categoryList) {
this.categoryList = categoryList;
}
/**
* #return the title
*/
public String getTitle() {
return title != null ? title.trim() : null;
}
/**
* #param title
* the title to set
*/
public void setTitle(String title) {
this.title = title != null ? title.trim() : null;
}
/**
* #return the state
*/
public int getState() {
return state;
}
/**
* #param state
* the state to set
*/
public void setState(int state) {
this.state = state;
}
/**
* #return the activated
*/
public boolean isActivated() {
return activated;
}
/**
* #param activated
* the activated to set
*/
public void setActivated(boolean activated) {
this.activated = activated;
}
#Override
public int hashCode() {
if (parentCategory != null && categoryList != null && title != null) {
return parentCategory.hashCode() + categoryList.hashCode() + title.hashCode();
} else if (categoryList != null && title != null) {
return parentCategory.hashCode() + categoryList.hashCode() + title.hashCode();
} else {
return super.hashCode();
}
}
#Override
public boolean equals(Object o) {
if (o != null && o instanceof CategoryBean) {
CategoryBean o2 = (CategoryBean) o;
if (getPerimetre() != null && getTitle() != null && getParentCategory() != null) {
return getPerimetre().equals(o2.getPerimetre()) && getTitle().equals(o2.getTitle())
&& getParentCategory().equals(o2.getParentCategory());
} else if (getPerimetre() != null && getTitle() != null && getPerimetre().equals(getTitle())) {
return getPerimetre().equals(o2.getPerimetre()) && getTitle().equals(o2.getTitle());
}
}
return super.equals(o);
}
#SuppressWarnings("unchecked")
#Override
public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
setPerimetre((String) input.readObject());
setParentCategory((CategoryBean) input.readObject());
setCategoryList((List<CategoryBean>) input.readObject());
setTitle((String) input.readObject());
setState((Integer) input.readObject());
setActivated((Boolean) input.readObject());
}
#Override
public void writeExternal(ObjectOutput output) throws IOException {
output.writeObject(getPerimetre());
output.writeObject(getParentCategory());
output.writeObject(getCategoryList());
output.writeObject(getTitle());
output.writeObject(getState());
output.writeObject(isActivated());
}
#Override
public CategoryBean clone() throws CloneNotSupportedException {
try {
CategoryBean clone = (CategoryBean) super.clone();
clone.setPerimetre(getPerimetre());
clone.setParentCategory(getParentCategory());
List<CategoryBean> categoryListClone = null;
if (getCategoryList() != null) {
categoryListClone = new LinkedList<CategoryBean>();
for (int i = 0; i < getCategoryList().size(); i++) {
categoryListClone.add(getCategoryList().get(i).clone());
}
}
clone.setCategoryList(categoryListClone);
clone.setTitle(getTitle());
clone.setState(getState());
clone.setActivated(isActivated());
return clone;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
Do you know what I do wrong in the clone method ?
Thanks for you help :)
(Edit) Solution :
public CategoryBean clone() throws CloneNotSupportedException {
try {
CategoryBean clone = (CategoryBean) super.clone();
clone.setPerimetre(getPerimetre());
List<CategoryBean> categoryListClone = null;
if (getCategoryList() != null) {
categoryListClone = new LinkedList<CategoryBean>();
for (int i = 0; i < getCategoryList().size(); i++) {
CategoryBean childClone = getCategoryList().get(i).clone();
childClone.setParentCategory(clone);
categoryListClone.add(childClone);
}
}
clone.setCategoryList(categoryListClone);
clone.setTitle(getTitle());
clone.setState(getState());
clone.setActivated(isActivated());
return clone;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
I think you need to cache the parent beans already cloned during the clone operation, so that when clone.setParentCategory is called in the child, this is the cloned parent that is injected in the property...
Not sure it's crystal clear :)... so... something like this (careful, no tested !) :
#Override
public CategoryBean clone() throws CloneNotSupportedException {
return cloneRecursive(new HashMap<CategoryBean, CategoryBean>());
}
private CategoryBean cloneRecursive(Map<CategoryBean, CategoryBean> categoryBeans) throws CloneNotSupportedException {
try {
CategoryBean clone = (CategoryBean) super.clone();
categoryBeans.put(this, clone);
clone.setPerimetre(getPerimetre());
clone.setParentCategory(categoryBeans.get(getParentCategory()));
List<CategoryBean> categoryListClone = null;
if (getCategoryList() != null) {
categoryListClone = new LinkedList<CategoryBean>();
for (int i = 0; i < getCategoryList().size(); i++) {
categoryListClone.add(getCategoryList().get(i).cloneRecursive(categoryBeans));
}
}
clone.setCategoryList(categoryListClone);
clone.setTitle(getTitle());
clone.setState(getState());
clone.setActivated(isActivated());
return clone;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
Related
I would like to create a void method that could change the value of the given parameter. I Tried to modify it in the method but it didn't work because it's modifying a copy of the variable.
I Know I can do it in c++ as following :
void Foo::myMethod(MyObject& obj);
I know I can use the return statement in my methods but I would Like to know if there's a way to do as above in java and here is what I tried :
public class LocalCallbackTest {
public static class MyClass {
private MyClass oldClass = null;
private MyClass newClass = null;
private int value = 0;
public MyClass() {
}
public MyClass(MyClass old) {
this();
oldClass = old;
}
/**
* #return the newClass
*/
public MyClass getNewClass() {
return newClass;
}
/**
* #return the oldClass
*/
public MyClass getOldClass() {
return oldClass;
}
/**
* #return the value
*/
public int getValue() {
return value;
}
/**
* #param newClass the newClass to set
*/
public void setNewClass(MyClass newClass) {
this.newClass = newClass;
}
/**
* #param oldClass the oldClass to set
*/
public void setOldClass(MyClass oldClass) {
this.oldClass = oldClass;
}
/**
* #param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (oldClass != null)
sb.append("old class : \n\t").append(oldClass.getValue()).append("\n");
if (newClass != null)
sb.append("new class : \n\t").append(newClass.getValue()).append("\n");
sb.append("actual class : \n\t").append(value).append("\n");
return sb.toString();
}
private MyClass rollBack() {
if (this.oldClass != null) {
this.oldClass.setNewClass(this);
return oldClass;
}
return this;
}
private MyClass nextMember() {
if (this.newClass != null) {
this.newClass.setOldClass(this);
return newClass;
}
return this;
}
public static boolean rollBack(MyClass param) {
if (param != (param = param.rollBack())) {
return true;
}
return false;
}
public static boolean nextMember(MyClass param) {
if (param != (param = param.nextMember())) {
return true;
}
return false;
}
public static void test(int i) {
i++;
}
}
public static void main(String[] args) {
MyClass test = new MyClass();
test.setValue(0);
for (int i = 1; i <= 5; i++) {
MyClass test2 = new MyClass();
test2.setValue(i);
test2.setOldClass(test);
test.setNewClass(test2);
test = test.getNewClass();
System.out.println(test.getValue());
}
int val=2;
MyClass.test(val);
System.out.println(val);
}
}
thank you for your Help.
In C++ you can pass parameters either by copy or by reference (with the &), but in Java, you can only pass an object as a reference, so any object you pass as a parameter will be modified exactly how you want it without having to return it. (like with an & in C++) if it does not work in your example, it's probably because of some logic.
I have found few similar threads about infinity loop while trying save object as Json, but most of them are about java + jpa, and these solutions don't work for me. I would like to create tree structure of some data. There is class ProjectNode which got
private ArrayList<ProjectNode> children;
field. I'm pretty sure this one course the problem but its the main field in this structure, I can't ignore it. I have tested and I can convert object which has list with node which doesn't have children, but cant if any child got another children in the below code:
public class ProjectNode implements Comparable<ProjectNode>{
private String parent;
private String nodeName;
private String nodeHref;
private boolean hasChild;
private int td;
private ArrayList<ProjectNode> children;
public ProjectNode() {
super();
// TODO Auto-generated constructor stub
}
+ getters and setters
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(parser.getProjectFactory().get(12));
parser.getProjectFactory() returns list of ParentNodes (parent=null), I would like to convert 12 elements because there is a case where node has child and this child has child. Cause this error:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"]->java.util.ArrayList[31]->ProjectNode["children"])
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:734)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
EDIT:
Parser - parse project/hosts list from cacti:
public List<ProjectNode> getProjectFactory() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
FileWriter fstream = new FileWriter("graphMap.txt");
BufferedWriter out = new BufferedWriter(fstream);
String [] hostInfo = null;
HtmlTableCell cel= (HtmlTableCell) page.getHtmlPage().getByXPath("/html/body/table/tbody/tr[5]/td[1]").get(0);
System.out.println(cel.getChildElementCount());
List<HtmlDivision> divs = cel.getByXPath(".//div");
System.out.println(divs.size());
//checks if list of host has been added to the project
ProjectTree projectTree = new ProjectTree();
Map<Integer,String> suppMap = new HashMap<Integer,String>();
for(HtmlDivision div : divs) {
List<DomNode> td = div.getByXPath(".//table/tbody/tr/td");
if(td.size()>2) {
ProjectNode pn = new ProjectNode(getNameSrc(td).split("#")[0],getNameSrc(td).split("#")[1],td.size());
hostInfo = getNameSrc(td).split("#");
if(pn.getTd()>3) {
if(!suppMap.get(pn.getTd()-1).isEmpty()) {
pn.setParent(suppMap.get(pn.getTd()-1));
}
if(suppMap.get(pn.getTd())!=null){
suppMap.remove(pn.getTd());
}
suppMap.put(pn.getTd(), pn.getNodeName());
projectTree.addNodeToTree(pn);
} else {
projectTree.addNodeToTree(pn);
suppMap.put(pn.getTd(), pn.getNodeName());
//out.write(pn.toString()+"\n");
}
}
}
ArrayList<ProjectNode> pns = projectTree.getNodeList();
Collections.sort(pns, Comparator.comparing(ProjectNode::getTd));
Collections.reverse(pns);
projectTree.setNodeList(pns);
List<ProjectNode> finalList = new ArrayList<ProjectNode>();
for(ProjectNode pn :pns ) {
if(pn.getTd()==3) {
finalList.add(pn);
}else {
projectTree.addToParent(pn);
}
}
Collections.reverse(finalList);
for(ProjectNode pn : finalList) {
Collections.sort(pn.getChildren());
}
out.write(finalList.get(12).getChildren().get(4).getChildren().toString());
out.close();
System.out.println(finalList.size());
return finalList;
}
ProjectTree class
public class ProjectTree {
private ProjectNode rootNode;
private ArrayList<ProjectNode> NodeList;
public ProjectTree() {
super();
this.NodeList = new ArrayList<ProjectNode>();
}
public ProjectTree(ProjectNode rootNode, ArrayList<ProjectNode> nodeList) {
super();
this.rootNode = rootNode;
NodeList = nodeList;
}
public ArrayList<ProjectNode> groupHosts(){
return this.NodeList;
}
public void addToParent(ProjectNode pn) {
for(ProjectNode pnA :this.NodeList) {
if(pnA.getNodeName().equals(pn.getParent())) {
if(pnA.getChildren()!=null && pnA.getChildren().size()!=0) {
pnA.sortChildren();
}
pnA.addChlild(pn);
}
}
}
public ProjectNode getRootNode() {
return rootNode;
}
public void setRootNode(ProjectNode rootNode) {
this.rootNode = rootNode;
}
public ArrayList<ProjectNode> getNodeList() {
return NodeList;
}
public void setNodeList(ArrayList<ProjectNode> nodeList) {
NodeList = nodeList;
}
}
ProjectNode.class
public class ProjectNode implements Comparable<ProjectNode>{
private String parent;
private String nodeName;
private String nodeHref;
private boolean hasChild;
private int td;
private ArrayList<ProjectNode> children;
public ProjectNode() {
super();
// TODO Auto-generated constructor stub
}
public ProjectNode( String nodeName, String nodeHref, int td) {
super();
this.nodeName = nodeName;
this.nodeHref = nodeHref;
this.hasChild =false;
this.td=td;
}
public void addChlild(ProjectNode pn) {
if(children!=null) {
this.children.add(pn);
}else {
this.children = new ArrayList<ProjectNode>();
this.children.add(pn);
}
}
public List<ProjectNode> getChildren() {
return children;
}
public void sortChildren() {
Collections.sort(this.getChildren());;
//Collections.sort(this.getChildren(), Comparator.comparing(ProjectNode::getNodeName));
//System.out.println(this.children.toString());
}
public void setChildren(ArrayList<ProjectNode> children) {
this.children = children;
}
public int getTd() {
return td;
}
public void setTd(int td) {
this.td = td;
}
public boolean isHasChild() {
return hasChild;
}
public void setHasChild(boolean hasChild) {
this.hasChild = hasChild;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public String getNodeHref() {
return nodeHref;
}
public void setNodeHref(String nodeHref) {
this.nodeHref = nodeHref;
}
#Override
public String toString() {
return "ProjectNode [parent=" + parent + ", nodeName=" + nodeName + ", nodeHref=" + nodeHref + ", hasChild="
+ hasChild + ", td=" + td + ", children=" +"]";
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((children == null) ? 0 : children.hashCode());
result = prime * result + (hasChild ? 1231 : 1237);
result = prime * result + ((nodeHref == null) ? 0 : nodeHref.hashCode());
result = prime * result + ((nodeName == null) ? 0 : nodeName.hashCode());
result = prime * result + ((parent == null) ? 0 : parent.hashCode());
result = prime * result + td;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProjectNode other = (ProjectNode) obj;
if (children == null) {
if (other.children != null)
return false;
} else if (!children.equals(other.children))
return false;
if (hasChild != other.hasChild)
return false;
if (nodeHref == null) {
if (other.nodeHref != null)
return false;
} else if (!nodeHref.equals(other.nodeHref))
return false;
if (nodeName == null) {
if (other.nodeName != null)
return false;
} else if (!nodeName.equals(other.nodeName))
return false;
if (parent == null) {
if (other.parent != null)
return false;
} else if (!parent.equals(other.parent))
return false;
if (td != other.td)
return false;
return true;
}
#Override
public int compareTo(ProjectNode o) {
// TODO Auto-generated method stub
return nodeName.compareTo(o.getNodeName());
}
}
I would use recursive to groupd all nodes in corect place but i had same error (with stackoverflow) so i had to use work around which is addToParent() function.
I have managed it by breaking loop when parent of my node have been found. I made it just for performance because before even if parent has been found loop was checking all elements anyway so i added break, and it works.. Not sure why..
Code below:
public void group() {
int td = this.highestTd;
while (td > 2) {
List<ProjectNode> toRemove = new ArrayList<ProjectNode>();
for (ProjectNode pnA : this.NodeList) {
if(pnA.getTd()==3) {
pnA.setParent("root");
}
if (pnA.getTd() == td) {
for (ProjectNode pnB : this.NodeList) {
System.out.println(pnB.toString());
if (pnA.getParent()!=null && pnA.getParent().equals(pnB.getNodeName())) {
System.out.println("Dodaje "+pnA.getNodeName() + " Do "+pnB.getNodeName());
pnB.addChlild(pnA);
toRemove.add(pnA);
break;
}
}
}
}
td--;
this.NodeList.removeAll(toRemove);
}
}
I defined classes Graph containing GraphNode as follows, my intention for declaring N is to compare 2 GraphNode objects using generics.
The question is how shall I instantiate the Graph which is recursively bound.
error while declaring as below.
Graph<Integer,Comparable<GraphNode>> graph = new Graph<>();
Bound mismatch: The type Comparable<GraphNode> is not a valid substitute for the bounded parameter <N extends Comparable<GraphNode<T,N>>> of the type Graph<T,N>
public class Graph<T, N extends Comparable<GraphNode<T, N>>> {
private N root;
private Class<N> clazz;
Graph(Class<N> clazz) {
this.clazz = clazz;
}
public N getInstance() {
try {
return clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
#SuppressWarnings("unchecked")
public void insert(T d, N n) {
if (root == null && n == null)
root = getInstance();
if (root == null)
root = n;
N node = root;
while (node != null) {
if (node.equals(n)) {
N newNode = getInstance();
((GraphNode<T, N>) newNode).setAdjNode(newNode);
}
}
}
}
public class GraphNode<T, N extends Comparable<GraphNode<T, N>>> implements Comparable<N> {
private T data;
private LinkedHashSet<N> adjNodes = new LinkedHashSet<>();
GraphNode() {
data = null;
}
GraphNode(T d) {
setData(d);
}
public void setAdjNode(N n) {
adjNodes.add(n);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
#Override
public int hashCode() {
return data.hashCode();
}
#Override
public boolean equals(Object obj) {
if (obj instanceof GraphNode<?, ?>) {
return ((GraphNode<?, ?>) obj).getData() == this.getData();
}
return false;
}
#Override
public String toString() {
return data + "";
}
#Override
public int compareTo(N o) {
return this.compareTo(o);
}
}
This solved my above problem
public class Graph<T extends Comparable<T>> {
private GraphNode<T> root;
public void insert(T d, GraphNode<T> n) {
if (root == null && n == null)
root = new GraphNode<T>(d);
if (root == null)
root = n;
GraphNode<T> node = root;
Queue<GraphNode<T>> queue = new ConcurrentLinkedQueue<>();
queue.add(root);
while (!queue.isEmpty()) {
node = queue.poll();
node.setNodeColor(color.BLACK);
if (node.equals(n)) {
GraphNode<T> newNode = new GraphNode<T>(d);
((GraphNode<T>) newNode).setAdjNode(newNode);
} else {
queue.addAll(node.getUnexploredAdjNodes());
}
}
}
}
public class GraphNode<T extends Comparable<T>> implements Comparable<GraphNode<T>> {
enum color {
WHITE, GREY, BLACK
};
private T data;
private color nodeColor = color.WHITE;
private LinkedHashSet<GraphNode<T>> adjNodes = new LinkedHashSet<>();
GraphNode() {
data = null;
}
GraphNode(T d) {
setData(d);
}
public void setAdjNode(GraphNode<T> n) {
adjNodes.add(n);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public LinkedHashSet<GraphNode<T>> getAdjNodes() {
return adjNodes;
}
public LinkedHashSet<GraphNode<T>> getUnexploredAdjNodes() {
LinkedHashSet<GraphNode<T>> n = new LinkedHashSet<>();
for (GraphNode<T> node : adjNodes) {
if (node.getNodeColor() == color.WHITE)
n.add(node);
}
return n;
}
public color getNodeColor() {
return nodeColor;
}
public void setNodeColor(color nodeColor) {
this.nodeColor = nodeColor;
}
#Override
public int hashCode() {
return data.hashCode();
}
#Override
public boolean equals(Object obj) {
if (obj instanceof GraphNode<?>) {
return ((GraphNode<?>) obj).getData() == this.getData();
}
return false;
}
#Override
public int compareTo(GraphNode<T> o) {
return data.compareTo(o.data);
}
#Override
public String toString() {
return data + "";
}
}
Graph<Integer> graph = new Graph<>();
I have created a Custom JavaFX Control, Call it ComboBoxTablePopup, it's a Generic Control witch takes a list of items of type S from the user.
Internally, i have used some sort of filtering in the TableView(a child of ComboboxTablePopup Contrl).So i have used :
FiltredList.setPridcate(S -> {
if (pr == null || pr.isEmpty() || pr.length() == 0)
return true;
if (((Engine) S).getDesignation().toLowerCase().contains(pr.toLowerCase())) {
return true;
} else
return false;).
Because i'm using generic class and the list can be of any type i have to cast filtredlist items to well known object in ordre to get filtring work.
So, how can i make the predicate function generic, so i can work with any object and filtre it ?
Here is the code of my CustomControl:
public class ComboBoxTablePopup<S> extends ComboBoxBase {
private
ObservableList<TableColumn> columns = FXCollections.emptyObservableList();
public ObservableList<TableColumn> getColumns() {
return columns;
}
public void setColumns(ObservableList<TableColumn> columns) {
this.columns = columns;
}
/***************************************************************************
* *
* Static properties and methods *
* *
**************************************************************************/
private static <T> StringConverter<T> defaultStringConverter() {
return new StringConverter<T>() {
#Override
public String toString(T t) {
return t == null ? null : t.toString();
}
#Override
public T fromString(String string) {
return (T) string;
}
};
}
/***************************************************************************
* *
* Constructors *
* *
**************************************************************************/
/**
* Creates a default ComboboxTablePopup instance with an empty
* {#link #itemsProperty() items} list and default
* {#link #selectionModelProperty() selection model}.
*/
public ComboBoxTablePopup() {
this(FXCollections.<S>emptyObservableList());
}
/**
* Creates a default ComboboxTablePopup instance with the provided items list and
* a default {#link #selectionModelProperty() selection model}.
*/
public ComboBoxTablePopup(ObservableList<S> items) {
setItems(items);
getStyleClass().add(DEFAULT_STYLE_CLASS);
setEditable(true);
setPromptText("Plz Search for a pirticular term");
}
public ComboBoxTablePopup(ObservableList<S> items, ObservableList<TableColumn> columns) {
this(items);
this.columns = columns;
}
private static final String DEFAULT_STYLE_CLASS = "combobox-table-popup";
private ReadOnlyObjectWrapper<TextField> editor;
private ObjectProperty<ObservableList<S>> items = new SimpleObjectProperty<ObservableList<S>>(this, "items");
public final void setItems(ObservableList<S> value) {
itemsProperty().set(value);
}
public final ObservableList<S> getItems() {
return items.get();
}
public ObjectProperty<ObservableList<S>> itemsProperty() {
return items;
}
// Converter
public ObjectProperty<StringConverter<S>> converterProperty() {
return converter;
}
private ObjectProperty<StringConverter<S>> converter =
new SimpleObjectProperty<StringConverter<S>>(this, "converter", ComboBoxTablePopup.<S>defaultStringConverter());
public final void setConverter(StringConverter<S> value) {
converterProperty().set(value);
}
public final StringConverter<S> getConverter() {
return converterProperty().get();
}// Create a symmetric (format/parse) converter with the default locale.
// Editor
public TextField getEditor() {
return editorProperty().get();
}
public ReadOnlyObjectProperty<TextField> editorProperty() {
if (editor == null) {
editor = new ReadOnlyObjectWrapper<TextField>(this, "editor");
editor.set(new ComboBoxListViewSkin.FakeFocusTextField());
}
return editor.getReadOnlyProperty();
}
#Override
protected Skin<?> createDefaultSkin() {
return new ComboBoxTablePopupSkin<>(this);
}
}
public class ComboBoxTablePopupSkin<S> extends ComboBoxPopupControl {
private ComboBoxTablePopup comboBoxTablePopup;
private ObservableList<S> comboboxTablePopupItems;
private TextField displayNode;
private TableView<S> tableViewPopupContent;
FilteredList<S> filtredList;
private SortedList<S> sortedList;
private Predicate<S> predicate;
private final InvalidationListener itemsObserver;
private final ListChangeListener<S> tableViewItemsListener = new ListChangeListener<S>() {
#Override
public void onChanged(ListChangeListener.Change<? extends S> c) {
getSkinnable().requestLayout();
}
};
private final WeakListChangeListener<S> weakListViewItemsListener =
new WeakListChangeListener<S>(tableViewItemsListener);
public ComboBoxTablePopupSkin(ComboBoxTablePopup comboBoxTablePopup) {
super(comboBoxTablePopup, new ComboBoxTablePopupBehavior(comboBoxTablePopup));
this.comboBoxTablePopup = comboBoxTablePopup;
setupTablePredicate();
updateComboBoxTablePopupItems();
itemsObserver = observable -> {
updateComboBoxTablePopupItems();
updateTableViewItems();
};
this.comboBoxTablePopup.itemsProperty().addListener(new WeakInvalidationListener(itemsObserver));
tableViewPopupContent = createTableView();
tableViewPopupContent.setManaged(false);
getChildren().add(tableViewPopupContent);
updateTableViewItems();
getEditor().textProperty().addListener((obv, oldValue, newValue) -> {
if (!newValue.isEmpty())
comboBoxTablePopup.show();
filtreData(newValue);
});
registerChangeListener(comboBoxTablePopup.itemsProperty(), "ITEMS");
registerChangeListener(comboBoxTablePopup.valueProperty(), "VALUE");
registerChangeListener(comboBoxTablePopup.editorProperty(), "EDITABLE");
}
private void setupTablePredicate() {
}
private void updateTableViewItems() {
filtredList = new FilteredList<S>(comboboxTablePopupItems, p -> true);
sortedList = new SortedList<S>(filtredList);
sortedList.comparatorProperty().bind(tableViewPopupContent.comparatorProperty());
tableViewPopupContent.setItems(sortedList);
}
private void filtreData(String pr) {
String data = pr;
filtredList.setPredicate(engine -> {
if (pr == null || pr.isEmpty() || pr.length() == 0)
return true;
if (((Engine) engine).getDesignation().toLowerCase().contains(pr.toLowerCase())) {
return true;
} else
return false;
});
}
public void updateComboBoxTablePopupItems() {
comboboxTablePopupItems = comboBoxTablePopup.getItems();
comboboxTablePopupItems = comboboxTablePopupItems == null ? FXCollections.<S>emptyObservableList() : comboboxTablePopupItems;
}
private TableView<S> createTableView() {
final TableView<S> tableView = new TableView<>();
tableView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
tableView.setFocusTraversable(false);
tableView.getSelectionModel().selectedItemProperty().addListener(o -> {
int index = tableView.getSelectionModel().getSelectedIndex();
System.out.println("selected item changed");
});
for (TableColumn tblColumn : tableColumns()) {
tableView.getColumns().add(tblColumn);
}
tableView.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER ||
e.getCode() == KeyCode.ESCAPE ||
e.getCode() == KeyCode.SPACE) {
S selectedItem = tableView.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
System.out.println(((Engine) selectedItem).getDesignation());
getEditor().setText(((Engine) selectedItem).getDesignation());
comboBoxTablePopup.setValue(selectedItem);
comboBoxTablePopup.hide();
}
}
});
return tableView;
}
private ObservableList<TableColumn> tableColumns() {
return ((ComboBoxTablePopup) getSkinnable()).getColumns();
}
#Override
protected Node getPopupContent() {
return this.tableViewPopupContent;
}
#Override
protected TextField getEditor() {
return ((ComboBoxTablePopup) getSkinnable()).getEditor();
}
#Override
protected StringConverter getConverter() {
return ((ComboBoxTablePopup) getSkinnable()).getConverter();
}
#Override
public Node getDisplayNode() {
if (displayNode == null) {
displayNode = getEditableInputNode();
displayNode.getStylesheets().add("ComboBoxTable-display-node");
updateDisplayNode();
}
displayNode.setEditable(comboBoxTablePopup.isEditable());
return displayNode;
}
#Override
protected void handleControlPropertyChanged(String p) {
super.handleControlPropertyChanged(p);
if ("TEXT".equals(p)) {
if (!getEditor().textProperty().get().isEmpty()) {
comboBoxTablePopup.show();
}
filtreData(getEditor().textProperty().get());
} else if ("ITEMS".equals(p)) {
updateComboBoxTablePopupItems();
} else if ("EDITABLE".equals(p)) {
getEditableInputNode();
}
}
}
Use the StringConverter to convert the item to a String or use a similar property.
public class ComboBoxTablePopupSkin<S> extends ComboBoxPopupControl<S> {
...
private static final StringConverter DEFAULT_CONVERTER = new StringConverter() {
#Override
public String toString(Object o) {
return o == null ? null : o.toString();
}
#Override
public Object fromString(String s) {
throw new UnsupportedOperationException();
}
};
public final StringConverter<S> getConverter() {
StringConverter<S> converter = converterProperty().get();
// fix in case the property is set to a null
return converter == null ? DEFAULT_CONVERTER : converter;
}
private void filtreData(String pr) {
if (pr == null || pr.isEmpty()) {
filtredList.setPredicate(null);
} else {
final StringConverter<S> converter = ((ComboBoxTablePopup<S>) getSkinnable()).getConverter();
final String data = pr.toLowerCase();
filtredList.setPredicate(object -> {
String s = converter.toString(object);
return s != null && s.toLowerCase().contains(data);
});
}
}
I have a problem in getting checkbox's value from JSP.
I used SpringMVC, and my related class as follows:
Form:
#Getter #Setter
public class Sample3Form extends AbstractForm {<
private List<Sample1Bean> sampleList; //all data
private ValidPagedListHolder<Sample1Bean> samplePagedList;//1 page data
}
the class to control changing page:
ValidPagedListHolder:
#SuppressWarnings("serial")
public class ValidPagedListHolder<E> extends PagedListHolder<E> implements
Serializable {
...
...
/**
* Return a sub-list representing the current page.
*/
#Override
#Valid
public List<E> getPageList() {
return super.getPageList();
}
/**
* コンストラクタ生成時の初期化処理を行うメソッド
*/
protected void init() {
setPageSize(DEFAULT_PAGE_SIZE);
}
}
Sample1Bean:
#Getter #Setter
public class Sample1Bean extends AbstractOnlineDataBean {
private String keiyakuno;
private String keiyakunm;
private String torisakino;
}
Parent Bean Class:
#Getter
#Setter
public abstract class AbstractOnlineDataBean extends AbstractDataBean {
private Integer selectedIndex;
private String maskPattern;
}
Controller:
#Controller
#RequestMapping(value = "app/sample3")
#SessionAttributes(value = "sample3Form")
#NotCheckToken
public class SampleController3 extends AbstractController {
private static Logger logger = LoggerFactory.getLogger(UserUtil.class);
#ModelAttribute(value = "sample3Form")
public Sample3Form getForm() {
return new Sample3Form();
}
......
......
#RequestMapping(value = "delete")
public String delete(Model model, #Validated Sample3Form form, BindingResult result, HttpServletRequest request) throws GdcSystemException {
List<Sample1Bean> list = new ArrayList<Sample1Bean>();
int size = form.getSampleList().size();
ValidPagedListHolder<Sample1Bean> plist = form.getSamplePagedList();
list = plist.getPageList();
for (int i = 0 ; i < size; i ++) {
if (form.getSampleList().get(i).getSelectedIndex() == null ) {
} else {
//I cannot arrive here even though I checked in JSP
list.add(form.getSampleList().get(i));
}
}
...
...
}
JSP:
<ab:form id="sample3" action="submit" modelAttribute="sample3Form">
<table width="100%" class="tableborder" cellpadding="0" cellspacing="0">
...
...
<ab:tr index="${row.index}">
<td width="2%" class="data_list_area">
<ab:checkbox path="${sample3Form.samplePagedList.pageList[row.index].selectedIndex}" fwformat='NONE'
value="${sample3Form.samplePagedList.page * sample3Form.samplePagedList.pageSize + row.index }" name="checkedids"/>
</td>
<td width="5%" class="data_list_area">
<!--ab:out value="${sample3Form.samplePagedList.pageList[row.index].keiyakuno}" /-->
<ab:label path="" fwformat='NONE'>${sample3Form.samplePagedList.pageList[row.index].keiyakuno}</ab:label>
</td>
...
...
In above JSP, I set checkboxes and bind the checkboxes to selectedIndex property for for every row data.but i cannot get selected row in controller as follows:
form.getSampleList().get(i).getSelectedIndex()
The return value is always null.
why? Could who please tell me how i can get the selectedIndex in controller?
By the way, I can get checked row in controller like below:
String[] arr = request.getParameterValues("checkedids");
Remarks: in JSP, These added checkboxes is defined by name "checkedids".
and <ab:checkbox> is a tag class extends AbstractSingleCheckedElementTag.
the src for CheckboxTagImpl is here:
public class CheckboxTagImpl extends AbstractSingleCheckedElementTag {
private String maskPatterns = null;
private boolean escapeMaskFlg = false;
/**
*
* #return
*/
public String getMaskPatterns() {
return maskPatterns;
}
/**
*
* #param maskPatterns
*/
public void setMaskPatterns(String maskPatterns) {
this.maskPatterns = maskPatterns;
}
/**
* #return
*/
public Boolean isEscapeMaskFlg() {
return escapeMaskFlg;
}
/**
* #param escapeMaskFlg
*/
public void setEscapeMaskFlg (Boolean escapeMaskFlg) {
this.escapeMaskFlg = escapeMaskFlg;
}
#Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
if (maskPatterns != null) {
FormTagImpl formTag = TagUtil.getFormTag(this);
String formClassName = formTag.getModelAttribute();
String maskPattern = TagUtil.getMaskPattern(formClassName, pageContext);
if (maskPattern != null) {
String[] maskPatternArray = maskPatterns.split(",");
for (int i = 0; i < maskPatternArray.length; i++) {
if (maskPattern.equals(maskPatternArray[i])) {
setDisabled(true);
break;
}
}
}
}
FormTagImpl formTag = TagUtil.getFormTag(this);
String formClassName = formTag.getModelAttribute();
Boolean maskFlg = TagUtil.getMaskFlg(formClassName, pageContext);
if (maskFlg == true && escapeMaskFlg == false) {
setDisabled(true);
}
if (!isDisabled()) {
// Write out the 'field was present' marker.
tagWriter.startTag("input");
tagWriter.writeAttribute("type", "hidden");
String name = WebDataBinder.DEFAULT_FIELD_MARKER_PREFIX + getName();
tagWriter.writeAttribute("id", getId());
tagWriter.writeAttribute("name", name);
tagWriter.writeAttribute("value", processFieldValue(name, SystemConstant.FLAG_OFF, getInputType()));
tagWriter.endTag();
}
super.writeTagContent(tagWriter);
return SKIP_BODY;
}
#Override
protected void writeTagDetails(TagWriter tagWriter) throws JspException {
tagWriter.writeAttribute("type", getInputType());
Object boundValue = getBoundValue();
Class<?> valueType = getBindStatus().getValueType();
if (Boolean.class.equals(valueType) || boolean.class.equals(valueType)) {
// the concrete type may not be a Boolean - can be String
if (boundValue instanceof String) {
boundValue = Boolean.valueOf((String) boundValue);
}
Boolean booleanValue = (boundValue != null ? (Boolean) boundValue : Boolean.FALSE);
renderFromBoolean(booleanValue, tagWriter);
} else if (String.class.equals(valueType) && getValue() == null) {
renderFromValue(SystemConstant.FLAG_ON, tagWriter);
}
else {
Object value = getValue();
if (value == null) {
throw new IllegalArgumentException("Attribute 'value' is required when binding to non-boolean values");
}
Object resolvedValue = (value instanceof String ? evaluate("value", value) : value);
renderFromValue(resolvedValue, tagWriter);
}
}
#Override
public void doFinally() {
if (PropertiesUtil.getProperty("cleanAtDoFinally").equals("true")) {
super.doFinally();
this.maskPatterns = null;
this.escapeMaskFlg = false;
setDisabled(false);
}
}
#Override
protected String getInputType() {
return "checkbox";
}
#Override
protected String getName() throws JspException {
if (getPath()==null) {
return super.getName();
} else {
return getPath();
}
}
#Override
protected BindStatus getBindStatus() throws JspException {
BindStatus bindStatus = null;
if (bindStatus == null) {
// HTML escaping in tags is performed by the ValueFormatter class.
String nestedPath = getNestedPath();
String pathToUse = (nestedPath != null ? nestedPath + getPath() : getPath());
if (pathToUse.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)) {
pathToUse = pathToUse.substring(0, pathToUse.length() - 1);
}
bindStatus = new BindStatus(getRequestContext(), pathToUse, false);
}
return bindStatus;
}
}
Thanks.