Git Product home page Git Product logo

treeprinter's Introduction

TreePrinter

To add this library to your project, click on JitPack badge. You can also manually add Jar file, you can find jar file in release section.

Demo: I have created two classes for printing FilesStructure.

1.FileTreePrinter

public class Main {

    public static void main(String[] args) {

        File file=new File("./src");

        FileTreePrinter treePrinter=new FileTreePrinter(file);
        treePrinter.visitAndPrint();
        //  or
        //  StringBuilder sb=treePrinter.visitAndReturn();
        //  sout(sb);
    }
}

This code will produce below output.

src
├─main
│  ├─java
│  │  └─com
│  │     ├─demo
│  │     │  └─Main.java
│  │     └─devik
│  │        ├─Color.java
│  │        ├─file
│  │        │  ├─FileTreePrinter.java
│  │        │  └─FileTreePrinterColored.java
│  │        └─TreePrinter.java
│  └─resources
└─test
   └─java

2.FileTreePrinterColored

public class Main {

    public static void main(String[] args) {

        File file=new File("./src");

        FileTreePrinterColored treePrinter=new FileTreePrinterColored(file);
        treePrinter.visitAndPrint();
        //  or
        //  StringBuilder sb=treePrinter.visitAndReturn();
        //  System.out.println(sb);
    }
}

This code will produce below output.

image

Also in case of FileTreePrinterColored you can set custom colors.

public class Main {

    public static void main(String[] args) {

        File file=new File("./src");

        FileTreePrinterColored treePrinter=new FileTreePrinterColored(file);
        
        treePrinter.setFolderColor(Color.RED);
        treePrinter.setHiddenFolderColor(Color);
        treePrinter.setExecutableFileColor(Color);
        treePrinter.setNonExecutableFileColor(Color);
        //or
        treePrinter.setNonExecutableFileColor(Color.valueOf("color asci"));

        
        treePrinter.visitAndPrint();
        //  or
        //  StringBuilder sb=treePrinter.visitAndReturn();
        //  System.out.println(sb);
    }
}

If you want to print other than file tree than you can use TreePrinter abstract class. In that case you need to implement three methods.

 Object obj;
        TreePrinter treePrinter=new TreePrinter(obj) {
            @Override
            public Object[] getChild(Object obj) {
                return new Object[0];
            }

            @Override
            public String getValue(Object obj) {
                return null;
            }

            @Override
            public boolean isLeaf(Object obj) {
                return false;
            }
        };
        // Here obj is the root of TreeStructure.

for ie. if you want to print BinaryTree than you can do something like below...

Object obj; //here obj is root of Binary tree
       TreePrinter treePrinter=new TreePrinter(obj) {
           @Override
           public Object[] getChild(Object obj) {
               Node node=(Node)obj;
               return new Object[]{node.left,node.right};
           }

           @Override
           public String getValue(Object obj) {
               return ((Node)obj).getValue();
           }

           @Override
           public boolean isLeaf(Object obj) {
              Node node=(Node)obj;
               return node.left==null && node.right==null;
           }
       };
       // Here obj is the root of TreeStructure.

treeprinter's People

Contributors

deviknitkkr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.