Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
sw-samuraj / blog-ring-middleware-common.clj
Last active July 10, 2017 13:34
An example of the common Ring middleware (for a blog post).
(defn wrap-blank-middleware
"Blank middleware demonstrating common structure."
[handler]
(fn [request]
; Do some magic with the request
(let [response (handler request)]
; Do some magic with the response
; and return the response.
response)))
@sw-samuraj
sw-samuraj / ImmutableListInvariance.java
Created May 29, 2017 20:15
An example of the Guava ImmutableList invariance.
import com.google.common.collect.ImmutableList;
interface Powerful {}
class Jedi implements Powerful {}
class Sith implements Powerful {}
public class ImmutableListInvariance {
public static void main(String[] args) {
ImmutableList<Jedi> jedi = ImmutableList.of(new Jedi());
// ImmutableList<Powerful> powerfuls = jedi; // Error
@sw-samuraj
sw-samuraj / ListCovariance.scala
Created May 29, 2017 19:34
An example of the Scala List covariance.
trait Powerful
class Jedi extends Powerful
class Sith extends Powerful
object ListCovariance {
def main(args: Array[String]): Unit = {
val jedi: List[Jedi] = List(new Jedi)
val powerfuls: List[Powerful] = jedi
val morePowerfuls = new Sith :: powerfuls
}
@sw-samuraj
sw-samuraj / ListInvariance.java
Last active May 28, 2017 19:24
An example of the Java List invariance.
import java.util.LinkedList;
import java.util.List;
interface Powerful {}
class Jedi implements Powerful {}
class Sith implements Powerful {}
public class ListInvariance {
public static void main(String[] args) {
List<Jedi> jedi = new LinkedList<>();
@sw-samuraj
sw-samuraj / ArrayInvariance.scala
Last active May 28, 2017 19:24
An example of the Scala Array invariance.
trait Powerful
class Jedi extends Powerful
class Sith extends Powerful
object ArrayInvariance {
def main(args: Array[String]): Unit = {
val jedi: Array[Jedi] = Array(new Jedi)
val powerfuls: Array[Powerful] = jedi // Error
powerfuls(0) = new Sith
val hybrid: Jedi = jedi(0)
@sw-samuraj
sw-samuraj / ArrayCovariance.java
Last active May 24, 2017 14:39
An example of the Java Array covariance.
interface Powerful {}
class Jedi implements Powerful {}
class Sith implements Powerful {}
public class ArrayCovariance {
public static void main(String[] args) {
Jedi[] jedi = new Jedi[] { new Jedi() };
Powerful[] powerfuls = jedi;
powerfuls[0] = new Sith();
Jedi hybrid = jedi[0];
@sw-samuraj
sw-samuraj / blog-swagger-gradle-config.json
Created May 8, 2017 19:42
Additional Swagger configuration in JSON file for a blog post about REST contract-first approach.
{
"sourceFolder": "",
"modelPackage": "cz.swsamuraj.rest.contractfirst.ws.model",
"apiPackage": "cz.swsamuraj.rest.contractfirst.ws.api",
"interfaceOnly": true
}
@sw-samuraj
sw-samuraj / blog-swagger-gradle-build.gradle
Last active May 8, 2017 19:41
Partial Gradle build script for a blog post about REST contract-first approach.
plugins {
id 'org.hidetake.swagger.generator' version '2.4.2'
}
repositories {
mavenCentral()
}
dependencies {
compile 'io.swagger:swagger-annotations:1.5.10'
@sw-samuraj
sw-samuraj / ultimate-question-spec.yaml
Created May 8, 2017 17:33
A Swagger specification for a blog post about REST contract-first approach.
swagger: "2.0"
info:
version: "0.1.0"
title: "The Ultimate Question."
paths:
/question:
get:
summary: "Asks the ultimate question"
@sw-samuraj
sw-samuraj / blog-ring-app.clj
Last active May 1, 2017 19:33
An example of the simple Ring application (for a blog post).
(ns blog-ring.core
(:require [ring.adapter.jetty :as jetty]
[ring.util.response :refer [response]]
[ring.middleware.params :refer [wrap-params]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]))
(defn handler [request]
(response
(str "<h1>One Ring rules them all!</h1>"
"<ul><li>Query string: "