Skip to content

Instantly share code, notes, and snippets.

View awwsmm's full-sized avatar

Andrew awwsmm

View GitHub Profile
@awwsmm
awwsmm / Signature.java
Last active August 18, 2018 16:43
Unshuffle a Java method signature that's mis-ordered
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Signature {
public static void main(String[] args) {
// slightly more complex example to show off capabilities of method
String oldSignature = "synchronized static final native public void barFooer(int foo, double bar)";
@awwsmm
awwsmm / firstNPrimes.py
Last active September 14, 2018 13:55
Get first N primes in a list
import math
import itertools
# number of primes to generate, beginning with 2, 3, 5, ...
N = 100
def firstNPrimes(N):
primes = [2]
prime = (x for x in itertools.count(2) if not any ([x%y==0 for y in range(2, int(math.ceil(math.sqrt(x)))+1)]))
for n in range(1, int(math.ceil(N))):
@awwsmm
awwsmm / ParseFileName.java
Created November 9, 2018 11:52
Parse any file name with Java and get path, name, and extension
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
public class ParseFileName {
public static void main (String[] args) {
System.out.println(Arrays.toString(parseFileName("test").get()));
System.out.println(Arrays.toString(parseFileName("test.a").get()));
@awwsmm
awwsmm / SplitXSV.java
Created November 10, 2018 12:00
Splits a line of sentinel-separated values into tokens
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class SplitXSV {
public static void main (String[] args) {
@awwsmm
awwsmm / ExpandPath.java
Last active May 6, 2020 05:24
Expand paths with globs '*', recursive double-globs '**', leading ~ or ~user home directories, or $ENV %VARS% (even on Windows!) to lists of files
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
@awwsmm
awwsmm / ParseCLArgs.java
Created November 11, 2018 22:08
Parse command-line arguments and categorise them as "arguments", "flags", "long flags", or "other". Return an easy-to-use List<Entry>.
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
public class ParseCLArgs {
// try with
// $ java ParseCLArgs arg -short anotherArg --long - -- ---toolong
@awwsmm
awwsmm / PNode.java
Created November 12, 2018 15:42
Ready-to-use node data structure for Java with examples in jshell
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@awwsmm
awwsmm / GetDerbyDBO.java
Created December 9, 2018 21:15
Determine / find the database owner ("dbo") and current user in an embedded Derby (JavaDB) database
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
/**
* Example class, showing how to find owner of an embedded Derby database.
*
@awwsmm
awwsmm / JavadocPaths.java
Created December 17, 2018 15:12
Filesystem paths (Windows and Linux) in Javadoc
/**
* Javadoc for filesystem path edge cases.
*
* <hr style="height:1px;border:0;border-top:1px solid #CCC">
*
* <h2><strong>Edge Case 0: Inline Windows Filesystem Paths</strong></h2>
*
* <p>Here is an inline Windows fs path with backslashes:
* <code>"C:&#92;Users&#92;user&#92;file.txt"</code>. You must replace the
* {@code '\'} characters with {@code "&#92;"} in the source code, otherwise
@awwsmm
awwsmm / FixedWidthFile.java
Created March 1, 2019 12:29
Infers the column widths of a fixed-width flat text file and parses its lines into tokens
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;