Skip to content

Instantly share code, notes, and snippets.

View johnlcox's full-sized avatar

John Leacox johnlcox

View GitHub Profile
johnlcox / Example.cs
Last active February 9, 2017 14:17
Event Driven State Machine
View Example.cs
public class Example
public enum States { One, Two }
public interface IExampleMessage
public class OneMessage : IExampleMessage
johnlcox / GoogleStyle.xml
Created August 2, 2016 23:38
Intellij formatter settings for Google's Java style guide
View GoogleStyle.xml
<code_scheme name="GoogleStyle">
<option name="USE_SAME_INDENTS" value="true" />
<option name="IGNORE_SAME_INDENTS_FOR_LANGUAGES" value="true" />
<option name="OTHER_INDENT_OPTIONS">
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="2" />
<option name="USE_TAB_CHARACTER" value="false" />
<option name="SMART_TABS" value="false" />
johnlcox / regex
Created March 7, 2015 03:14
Toml Regexes
View regex
johnlcox /
Last active August 29, 2015 13:58
Java 8 Pattern Matching
import static GuavaOptionalPattern.caseNone;
import static GuavaOptionalPattern.caseSome;
import static IntegerPattern.caseOf;
import static OrRelsePattern.orElse;
import static PatternMatching.match;
public class Examples {
public String matchInteger(int x) {
View Injectable Mixins
class MyInjectable() {
val myString = "my String"
trait MyTrait {
val myInjectable: MyInjectable = null // Initialize to null so that it isn't abstract and will be injected by guice. Is there a better way to do this though?
class MyObject extends Controller with MyTrait {}
johnlcox / gist:7493793
Created November 15, 2013 23:50
Scala Futures
View gist:7493793
def getNearby(avatarId: UUID, radius: Double) = Action.async {
Avatar.getById(avatarId).flatMap {
case Some(avatar) => avatar.location match {
case Some(location) => AvatarService.findAvatarsNearLocation(location, radius).map {nearby => Ok(Json.toJson(nearby))}
case None => Future.successful(NotFound)
case None => Future.successful(NotFound)
johnlcox / gist:7362881
Last active December 27, 2015 17:29
jackson-scala-module List of case class
View gist:7362881
it("should deserialize a List of a case class") {
case class InListCaseClass(id: Long)
val paramTypes = Array(objectMapper.getTypeFactory().constructSimpleType(classOf[InListCaseClass],
val listType = objectMapper.getTypeFactory.constructSimpleType(classOf[List[InListCaseClass]], paramTypes)
val value = List(InListCaseClass(2), InListCaseClass(5))
val json = """"[{"id":2},{"id":5"}]""""
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ApacheLogParser {
johnlcox / Preconditions.cs
Created February 25, 2013 19:34
C# Preconditions
View Preconditions.cs
public class Preconditions
/// <summary>
/// Ensures that an object reference passed as a parameter to the calling method is not null.
/// </summary>
/// <param name="reference">an object reference</param>
/// <param name="errorMessage">the exception message to use if the check fails</param>
/// <returns>the non-null reference that was validated</returns>
/// <exception cref="NullReferenceException">if reference is null</exception>
public static T CheckNotNull<T>(T reference, string errorMessage)