Skip to content

Instantly share code, notes, and snippets.

@johnlcox
johnlcox / Example.cs
Last active February 9, 2017 14:17
Event Driven State Machine
public class Example
{
public enum States { One, Two }
public interface IExampleMessage
{
}
public class OneMessage : IExampleMessage
{
@johnlcox
johnlcox / GoogleStyle.xml
Created August 2, 2016 23:38
Intellij formatter settings for Google's Java style guide
<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">
<value>
<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
johnlcox / regex
Created March 7, 2015 03:14
Toml Regexes
integer:
-?\+?(\d+(\d*_\d*)*\d+|\d+)
float:
-?\+?((\d+(\d*_\d*)*\d+|\d+)(\.(\d+(\d*_\d*)*\d+|\d+))?(([eE]-?\+?(\d+(\d*_\d*)*\d+|\d+))?))
@johnlcox
johnlcox / Examples.java
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;
import com.google.common.base.Optional;
public class Examples {
public String matchInteger(int x) {
class MyInjectable() {
val myString = "my String"
}
trait MyTrait {
@Inject
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
johnlcox / gist:7493793
Created November 15, 2013 23:50
Scala Futures
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
johnlcox / gist:7362881
Last active December 27, 2015 17:29
jackson-scala-module List of case class
it("should deserialize a List of a case class") {
case class InListCaseClass(id: Long)
val paramTypes = Array(objectMapper.getTypeFactory().constructSimpleType(classOf[InListCaseClass],
Array.empty[JavaType]))
val listType = objectMapper.getTypeFactory.constructSimpleType(classOf[List[InListCaseClass]], paramTypes)
val value = List(InListCaseClass(2), InListCaseClass(5))
val json = """"[{"id":2},{"id":5"}]""""
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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
johnlcox / Preconditions.cs
Created February 25, 2013 19:34
C# Preconditions
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)