Skip to content

Latest commit

 

History

History
160 lines (107 loc) · 4.14 KB

File metadata and controls

160 lines (107 loc) · 4.14 KB

JavaUtil


JavaUtil

General utility classes:   org.jutil.*

📖   Documentation / Examples         Javadoc Overview

Class
org.jutil.util.Util Javadoc
org.jutil.util.Strings Javadoc
org.jutil.util.CommandExecutor Javadoc


JavaUtil is still in a formation process - but you can watch things grow.


⚡️ Quick overview

Using org.jutil.Strings

import org.jutil.util.*;

	... 
	// create a simple headline (e.g. for logging)
	System.out.println(Strings.repeat(30, "*"));
	System.out.println(Strings.center("***", "headline", "***", 30));
	System.out.println(Strings.repeat(30, "*"));	
	
Output:
******************************
***        headline        ***
******************************

	...
	// create line numbers
	int i = 42;
	System.out.println(
		Strings.fillLeft("" + i + ": ", 7, " ") + "some text");
	i += 1000;
	System.out.println(
		Strings.fillLeft("" + i + ": ", 7, " ") + "some other text");
	
Output:
   42: some text
 1042: some other text
   
	...
	Strings.stripIfEndsWith("myFile.json", ".json");             // returns "myFile"

	...
	Strings.listToString(List.of(1, 2, 3), null, ","", false);   // returns "1,2,3"

	...
	Strings.listToString(List.of(1, 2, 3), "   ", ",\n", false);	
	
Output (indented by "   "):
   1,
   2,
   3

	...
	System.out.println(
		Strings.listToString(List.of("This", "is", "a", "text."), "   <a>, "</a>\n", true)); 

Output:
   <a>This</a>
   <a>is</a>
   <a>a</a>
   <a>text.</a>
   
	...
	indent("  ", 3, "text line");          // returns "      text line" (indented by 6 spaces)
	
More detailed examples can be found in the package README or the Javadoc API.

Back to top

Using org.jutil.Util (selected examples)

import org.jutil.Util;

	... 
	Util.notImplementedException();		
	// throws a subclass of RuntimeException and text to identify unimplemented
	// code parts 
	
	...
	Util.sleep(100, false); 	
	// convenience method: sleeps 100ms or until interruption, see 
	// implementation or Javadoc for details
	
	...
	String prompt = Util.prompt("go further: ");		// console IO
	System.out.println("Prompted input: '" + prompt + "'");
	
More detailed examples can be found in the package README or the Javadoc API.

Back to top

Using org.jutil.CommandExecutor

import org.jutil.Util;

	... 
	// Linux, Unix, MacOS, others:
	
	CommandExecutor executor = new CommandExecutor("bash", "-c", "ls");
	System.out.println("Exit code: " + executor.getExitCode());
	System.out.println("\n" + executor.getOutput());
	
Output (of the JavaUtil folder):
Exit code: 0

bin
build.xml
CHANGELOG.md
examples
LICENSE
...

	// MS-Windows:
	
	CommandExecutor executor = new CommandExecutor("cmd.exe", "/c", "ping -n 3 localhost");
	
More detailed examples can be found in the package README or the Javadoc API.

Overview                 Back to top