Skip to content

Instantly share code, notes, and snippets.

Jonathan Jackson jonjack

Block or report user

Report or block jonjack

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View cool-thanks-message-in-code
if (tweeted || facebooked) {
gratitude += 1;
}
if (githubStarred) {
karmaBalance = true;
} else {
$('we').say('ok');
}
View JodaTimer.java
import org.joda.time.LocalTime;
public class Timer {
public static void main(String[] args) {
LocalTime start = LocalTime.now();
// some event to be timed
LocalTime end = LocalTime.now();
int delta = end.getMillisOfDay() - start.getMillisOfDay();
System.out.println("Time took: " + Double.valueOf(delta)/1000 + " seconds");
}
View JavaTimer.java
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
public class Timer {
public static void main(String[] args) {
LocalTime start = LocalTime.now();
// some event to be timed
LocalTime end = LocalTime.now();
long delta = TimeUnit.NANOSECONDS.toMillis(end.toNanoOfDay() - start.toNanoOfDay());
System.out.println("Time took: " + Double.valueOf(delta)/1000 + " seconds");
View mockito-mocking-iterators.md

We have a method to test which iterates over some List. The example is a validator service which iterates over a List of validators applying each one and returning when one fails ie. it returns an Optional containing an error message (this is just a concrete example to demonstrate Mockito and is not important to understand).

public class SomeValidationService implements ValidationService {

    /* List of validators which all encapsulate some different business validation logic */
    /* This gets injected say via Spring */
    private List<Validator> validators;
 
    public Optional<ErrorMessage> validateAction(final ObjectToValidate obj) {
View java-overloading-interfaces.md

Scenario: We have an interface that defines an operation.

public interface Validator {
  Optional validate(String subject, String subject2);
}

Some Validators only have 1 String to validate so we can extend the interface with a simpler one.

@jonjack
jonjack / scala-snippets.md
Last active Nov 7, 2018
Random Scala snippets
View scala-snippets.md

Return the max value of a List[Int] if unique, otherwise return -1.

def maxIfUnique(li: List[Int]) = li.groupBy(_.intValue).maxBy(_._1)._2 match {
  case x if x.size == 1 => x.head
  case _ => -1
}

scala> val list = List(1, 2, 1)   // 2 is max and is unique
View my-mac-setup.md

General Mac Setup

Fonts

Check out Adobe Fonts.

brew tap caskroom/fonts && brew cask install font-source-code-pro
@jonjack
jonjack / java-chaining-optional-checks.md
Last active Oct 29, 2018
Example of how to check multiple Optional values in a chain and return a default value if none contain a value.
View java-chaining-optional-checks.md

The following scenario has multiple Optionals, a real example could be where you are calling multiple validation checks where each check returns an Optional<T>. You then want to check if each Optional contains a value or is empty - returning the first one that has a value. If none contain a value (ie. are instances of Optional.empty) then you return a default value.

In this example we are using Optional<Single> where Single is an RxJava Observer that returns a single value.

import com.google.common.collect.ImmutableList;
@jonjack
jonjack / .bash_profile
Created Oct 12, 2018 — forked from natelandau/.bash_profile
Mac OSX Bash Profile
View .bash_profile
# ---------------------------------------------------------------------------
#
# Description: This file holds all my BASH configurations and aliases
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
@jonjack
jonjack / git-setup-osx.md
Last active Oct 10, 2018
Git configuration on Mac.
View git-setup-osx.md

Set your global user details.

git config --global user.name "Your Name"
git config --global user.email your.email@elasticpath.com

Set your line feed settings.

You can’t perform that action at this time.