EldiraSesto

GenericTreeNode

May 20th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. package tree.node;
  2.  
  3. import container.Container;
  4. import util.searchable.ISearchFilter;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8.  
  9. public class GenericTreeNode<NODETYPE> implements ITreeNode<NODETYPE> { //Eldira 11815163
  10.     private String label;
  11.     private NODETYPE value;
  12.     private Collection<ITreeNode<NODETYPE>> children;
  13.  
  14.     public GenericTreeNode(String label, NODETYPE value) {
  15.         this.label = label;
  16.         this.value = value;
  17.  
  18.         this.children = new Container<>();
  19.     }
  20.  
  21.     GenericTreeNode(String label, NODETYPE value, Collection<ITreeNode<NODETYPE>> children) {
  22.         this.label = label;
  23.         this.value = value;
  24.         this.children = children;
  25.     }
  26.  
  27.     @Override
  28.     public boolean checkNodeByValue(NODETYPE value) {
  29.         return this.value.equals(value);
  30.     }
  31.  
  32.     @Override
  33.     public ITreeNode<NODETYPE> deepCopy() {
  34.         Collection<ITreeNode<NODETYPE>> newChildren = new Container<>();
  35.         for (ITreeNode<NODETYPE> child : this.children) {
  36.             newChildren.add(child.deepCopy());
  37.         }
  38.  
  39.         return new GenericTreeNode<NODETYPE>(this.label, this.value, newChildren);
  40.     }
  41.  
  42.     @Override
  43.     public ITreeNode<NODETYPE> findNodeByNode(ITreeNode<NODETYPE> searchNode) {
  44.         if (this.equals(searchNode)) {
  45.             return this;
  46.         }
  47.  
  48.         for (ITreeNode<NODETYPE> child : this.children) {
  49.             ITreeNode<NODETYPE> found = child.findNodeByNode(searchNode);
  50.             if (found != null) {
  51.                 return found;
  52.             }
  53.         }
  54.  
  55.         return null;
  56.     }
  57.  
  58.     @Override
  59.     public ITreeNode<NODETYPE> findNodeByValue(NODETYPE searchValue) {
  60.         if (this.checkNodeByValue(searchValue)) {
  61.             return this;
  62.         }
  63.  
  64.         if (this.children.isEmpty()) {
  65.             return null;
  66.         }
  67.  
  68.         for (ITreeNode<NODETYPE> child : this.children) {
  69.             ITreeNode<NODETYPE> found = child.findNodeByValue(searchValue);
  70.  
  71.             if (found != null) {
  72.                 return found;
  73.             }
  74.         }
  75.  
  76.         return null;
  77.     }
  78.  
  79.     @Override
  80.     public String generateConsoleView(String spacer, String preamble) {
  81.         StringBuilder ret = new StringBuilder();
  82.  
  83.         ret.append(preamble);
  84.         ret.append(this);
  85.  
  86.         if (!this.children.isEmpty()) {
  87.             for (ITreeNode<NODETYPE> child : this.children) {
  88.                 ret.append("\n");
  89.                 ret.append(child.generateConsoleView(spacer, preamble + spacer));
  90.             }
  91.         }
  92.  
  93.         return ret.toString();
  94.     }
  95.  
  96.     @Override
  97.     public Collection<ITreeNode<NODETYPE>> getChildren() {
  98.         return this.children;
  99.     }
  100.  
  101.     @Override
  102.     public String getLabel() {
  103.         return this.label;
  104.     }
  105.  
  106.     @Override
  107.     public boolean isLeaf() {
  108.         return this.children.isEmpty();
  109.     }
  110.  
  111.     @Override
  112.     public NODETYPE nodeValue() {
  113.         return this.value;
  114.     }
  115.  
  116.     /*void setChildren(Collection<ITreeNode<NODETYPE>> children) {
  117.         this.children = children;
  118.     }
  119.  
  120.     void setLabel(String label) {
  121.         this.label = label;
  122.     }
  123.  
  124.     void setValue(NODETYPE value) {
  125.         this.value = value;
  126.     }*/
  127.  
  128.     @Override
  129.     public Collection<ITreeNode<NODETYPE>> searchByFilter(ISearchFilter filter, Object compareObject) {
  130.         Collection<ITreeNode<NODETYPE>> ret = new Container<>();
  131.  
  132.         if (filter.searchFilterFunction(this, compareObject) ||
  133.                 filter.searchFilterFunction(this.value, compareObject)) {
  134.             ret.add(this);
  135.         }
  136.  
  137.         for (ITreeNode<NODETYPE> child : this.children) {
  138.             ret.addAll(child.searchByFilter(filter, compareObject));
  139.         }
  140.  
  141.         return ret;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment