To catch up on a backlog of work for an upcoming deadline, I spent most of this past weekend coding (writing new stuff and fixing up old projects). Check out some of these Gists I wrote and gimme those sweet, sweet stars:
There is no shortage of custom-madetree structures written in Java, probably because the best implementation only exists in the javax.swing package, which is now de facto deprecated, replaced by JavaFX. So I wrote a tree (or rather a node) class where each node has a label and a value (both or either of which can be null). The nodes are kept track of via their indices in Lists, which means when a node is "deleted", it's replaced by a null to maintain List indexing.
Often, when parsing text files, you want to determine the type of data represented by the Strings you're reading in. Typifier allows interpretation of String data as any of Java's primitive wrapper types, boxing data in the narrowest type possible. The code also allows for the user to define timestamp formats so that Strings can be parsed as LocalDateTime objects as well! To speed up processing of large data sets, interpretation can be restricted to "common" types only (Double, Boolean, LocalDateTime, String).
String.trim() is used to remove leading and trailing whitespace, so the following work fine:
typify (" 23 ") => "23" [Byte]
typify (" 3.4 ") => "3.4" [Float]
But if the user enters only whitespace, that's fine, too:
typify (" ") => " " [String]
typify (" ") => " " [String]
The user can choose to interpret 1/0 as true/false:
typify ("1") => "true" [Boolean]
typify ("true") => "true" [Boolean]
typify ("0") => "false" [Boolean]
Ranges of Byte, Short, and Float are used to box the value in the narrowest type available:
typify (" 2 ") => "2" [Byte]
typify (" 200 ") => "200" [Short]
typify ("2e9 ") => "2.0E9" [Float]
typify (" 2e99") => "2.0E99" [Double]
If String has length 1 and is not Boolean or Byte, it will be assigned the Character type:
typify ("4") => "4" [Byte]
typify ("-") => "-" [Character]
typify ("a") => "a" [Character]
Dates can also be parsed, and formats can be defined by the user:
typify ("2014-12-22 14:35:22") => "2014-12-22 14:35:22" [LocalDateTime]
typify ("3/5/99 6:30") => "3/5/99 6:30" [LocalDateTime]
Passing lists of files via the command line to a Java program doesn't always work as expected. The shell can auto-expand $ENVIRONMENT_VARIABLES or gl*bs, and lots of features available in bash aren't available in the Windows cmd.exe prompt. This script allows for paths to be interpreted the same way on Windows and Unix and adds recursive "double-globbing" (**) to pre-bash4 shells (as well as cmd), among other cool features.
This script splits a line of sentinel-separated text and returns the resulting tokens. Any non-alphanumeric character is preceded with a '\' to escape it, so even tabs or line breaks can be used as the delimiters.
given sentinel: '-'
given string:
{1-800-944-2345}
result:
{1}, {800}, {944}, {2345}
----------
given sentinel: ' '
given string:
{hi how are you}
result:
{hi}, {how}, {are}, {you}
It can be frustrating to try to figure out how to read "resource" files in the code of a Maven project. There are differing and contradictory answers online. For posterity, I've set up a MWE Maven Project that shows how to read files located at src/main/resources and use those files in your Java code.
It will make a new directory called "my-new-package" inside the current directory. Next, make a directory called src/main/resources inside the Maven project directory (inside my-new-package) and add the following text to a file called example.txt in that directory:
this is an example resource
here is a second line
and a third one
Remove the files src/test/java/com/companyname/packagename/AppTest.java and src/main/java/com/companyname/packagename/App.java. Replace the text of your pom.xml file with the following text:
This short script is useful for quickly getting the containing directory of a file or its extension. The user can them selectively process files based on those attributes.
The lines:
System.out.println(Arrays.toString(parseFileName("test").get()));
System.out.println(Arrays.toString(parseFileName("test.a").get()));
System.out.println(Arrays.toString(parseFileName("test.a.b").get()));
...yield the output:
[/home/andrew, test, ]
[/home/andrew, test, a]
[/home/andrew, test, a.b]
This is another short script that parses command-line arguments as basic ARGUMENTs, FLAGs (like -this), LONGFLAGs (like --this), or OTHER (only - or --). The arguments are returned in-order in a List so the user can determine whether or not the provided flags and arguments are valid.