Epickie życie epickiej klasy

Z powodu, bo tak:

Listing 1. Epicka klasa

/**
*   Copyright 2013 Bartek 'koziołek' Kuczyński
*
*   I hereby release this under Beerware 2.0, 
*   which means that if you use it and it helped you somehow, 
*   then if we meet in person, you are free to buy me a beer (or slice of pizza).
*   
*/
package pl.koziolekweb.epic;

public abstract class Epic<T> {

    private T t;

    private Epic(T t) {
        this.t = t;
    }

    private static class Win<T> extends Epic<T> {

        public Win(T t) {
            super(t);
        }

        public boolean isWin() {
            return true;
        }

        public boolean isFail() {
            return false;
        }

        public String message() {
            return "";
        }
    }

    private static class Fail<T> extends Epic<T> {
        private String message;

        public Fail(T t, String message) {
            super(t);
            this.message = message;
        }

        public boolean isWin() {
            return false;
        }

        public boolean isFail() {
            return true;
        }

        public String message() {
            return message;
        }
    }

    public static <T> Epic<T> win(T t) {
        return new Win(t);
    }

    public static <T> Epic<T> fail(T t, String message) {
        return new Fail(t, message);
    }

    public T result() {
        return t;
    }

    public abstract boolean isWin();

    public abstract boolean isFail();

    public abstract String message();
}

2 myśli na temat “Epickie życie epickiej klasy

  1. Wygląda jak coś pomiędzy Option[T] a Either[T, T]. Albo nawet Either[T, (T, String)]. Albo (T, Option[String]). Tylko po co? 🙂

Napisz odpowiedź

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax