samples/singletons/src/org/apidesign/singletons/api/DialogDisplayer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 27 Jan 2010 18:30:57 +0100
changeset 343 8e220093e4bf
child 345 f497cf8621b3
permissions -rw-r--r--
Adding singleton example
jtulach@343
     1
package org.apidesign.singletons.api;
jtulach@343
     2
jtulach@343
     3
import java.util.Iterator;
jtulach@343
     4
import java.util.ServiceLoader;
jtulach@343
     5
import org.openide.util.Lookup;
jtulach@343
     6
jtulach@343
     7
// BEGIN: singletons.injectable.api
jtulach@343
     8
public abstract class DialogDisplayer {
jtulach@343
     9
    protected DialogDisplayer() {
jtulach@343
    10
    }
jtulach@343
    11
jtulach@343
    12
    /** Ask user a question.
jtulach@343
    13
     *
jtulach@343
    14
     * @param query the text of the question
jtulach@343
    15
     * @return true if user confirmed or false if declined
jtulach@343
    16
     */
jtulach@343
    17
    public abstract boolean yesOrNo(String query);
jtulach@343
    18
jtulach@343
    19
    public static DialogDisplayer getDefault() {
jtulach@343
    20
        return Impl.DEFAULT;
jtulach@343
    21
    }
jtulach@343
    22
    // FINISH: singletons.injectable.api
jtulach@343
    23
jtulach@343
    24
jtulach@343
    25
    // BEGIN: singletons.injectable.dummyimpl
jtulach@343
    26
    private static final class Impl extends DialogDisplayer {
jtulach@343
    27
        private static final DialogDisplayer DEFAULT = initialize();
jtulach@343
    28
        
jtulach@343
    29
        @Override
jtulach@343
    30
        public boolean yesOrNo(String query) {
jtulach@343
    31
            System.err.printf("Saying no to '%s'\n", query);
jtulach@343
    32
            return false;
jtulach@343
    33
        }
jtulach@343
    34
        // FINISH: singletons.injectable.dummyimpl
jtulach@343
    35
jtulach@343
    36
        private static DialogDisplayer initialize() {
jtulach@343
    37
            if (Boolean.getBoolean("singleton.jdk6")) {
jtulach@343
    38
                return initializeServiceLoader();
jtulach@343
    39
            } else {
jtulach@343
    40
                return initializeLookup();
jtulach@343
    41
            }
jtulach@343
    42
        }
jtulach@343
    43
        
jtulach@343
    44
        // BEGIN: singletons.injectable.serviceloader
jtulach@343
    45
        private static DialogDisplayer initializeServiceLoader() {
jtulach@343
    46
            // see http://singletons.apidesign.org
jtulach@343
    47
            Iterator<DialogDisplayer> it = null;
jtulach@343
    48
            it = ServiceLoader.load(DialogDisplayer.class).iterator();
jtulach@343
    49
            return it != null && it.hasNext() ? it.next() : new Impl();
jtulach@343
    50
        }
jtulach@343
    51
        // END: singletons.injectable.serviceloader
jtulach@343
    52
jtulach@343
    53
        // BEGIN: singletons.injectable.lookup
jtulach@343
    54
        private static DialogDisplayer initializeLookup() {
jtulach@343
    55
            DialogDisplayer def = Lookup.getDefault().lookup(DialogDisplayer.class);
jtulach@343
    56
            return def != null ? def : new Impl();
jtulach@343
    57
        }
jtulach@343
    58
        // END: singletons.injectable.lookup
jtulach@343
    59
    }
jtulach@343
    60
}