Skip to content

Instantly share code, notes, and snippets.

@bluerogue
bluerogue / JsonNoNulls.java
Last active August 29, 2015 14:09
Json or toString without nulls - using reflection to omit null object fields from toString or toJson
/*
* Copyright (C) 2014 Matthew Roberts
* MIT License, http://opensource.org/licenses/MIT
*/
package org.matthewroberts;
import java.io.Serializable;
import java.lang.reflect.Field;
/**
@bluerogue
bluerogue / StringToIntegerValidator.java
Last active August 29, 2015 14:09
String to Integer without needing exception handling
/*
* Copyright (C) 2014 Matthew Roberts
* MIT License, http://opensource.org/licenses/MIT
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Checks to see if a String can be used as a valid Integer without resorting to
@bluerogue
bluerogue / single-line-httpserver-and-webpage.sh
Last active August 29, 2015 14:09
Single Line HTTP Server and Webpage
# This is a single line command to executed directly in the CLI. This will NOT run as a shell script
# as-is. This assumes you have Python installed and should work on any Mac OS X system and most
# linux systems.
#
# This changes to the user's home directory, creates and changes into a public_html directory,
# creates a file called index.html with the contents "Hello, {username}", starts a Python HTTP server,
# and finally attempts to open your default browser to the webpage. If this last step doesn't happen,
# simply open a browser and go to http://127.0.0.1:8080.
cd && mkdir public_html && cd public_html && echo 'Hello,' $USER >> index.html && python -m SimpleHTTPServer 8080 & open http://127.0.0.1:8080
@bluerogue
bluerogue / GetNonNullMap.java
Last active August 29, 2015 14:17
Reflection method for getting field values in a child class from a parent class
/**
* Assembles a hashmap, omitting null or empty values at runtime. This
* can be used in an abstract class and properly accessed in a child class.
*
* @return returns a hashmap of the current class fields that are not null
*/
public Map<String, String> getNonNullMap() {
Class<?> clazz = null;
Map<String, String> classMap = new HashMap<String, String>();
@bluerogue
bluerogue / TimerTask.java
Last active August 29, 2015 14:18
Sets up a single instance of a Java TimerTask
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Sets up a singleton to run a scheduled task. This ensures that only one
* instance of the task will run at once and allows monitoring of that task.
@bluerogue
bluerogue / GridGenerator.java
Created April 1, 2015 20:05
Generates a 2D grid with randomized integers (ensuring no 3 consecutive values) and performs several functions against the grid
/**
* Handles the generation of the randomized grid and associated functions.
*
* @author Matthew Roberts
* matthew.bowen.roberts@gmail.com
* matthewroberts.org
*/
public class GridGenerator {
private static final int MAX_RANDOM = 5;
@bluerogue
bluerogue / LargestSum.java
Created April 5, 2015 18:00
Largest sum from adjacent path
Map<Integer, Integer[]> values = populate(); // should typically be a 2D array of type 'int' instead
for (int i = values.size() - 2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
values.get(i)[j] +=
Math.max(values.get(i + 1)[j], values.get(i + 1)[j + 1]);
}
}
int largestPathSum = values.get(0)[0];
@bluerogue
bluerogue / RestConcurrencyTest.java
Created July 7, 2015 17:58
HTTP calls testing for concurrency issues within an API
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
/**
* Recursive quicksort method. Pivot is calculated at 1/2 the difference
* between supplied bounds.
*
* @param lowerBound
* the lower bound of indexed inputs - e.g., usually 0
* @param upperBound
* the upper bound of indexed inputs - e.g., usually array.length
* @param array
* the array to sort
@bluerogue
bluerogue / TicTacToe.java
Last active July 5, 2016 20:23
Basic console TicTacToe
import java.util.Scanner;
/**
* There is absolutely no error checking in this game. It's used here as
* a basic example only.
*
* @author matthewroberts
*
*/
public class TicTacToe {