# HG changeset patch # User Jaroslav Tulach # Date 1264613457 -3600 # Node ID 8e220093e4bfab65ee13ff1a6073ce06e61f1095 # Parent 04f97f1bc2da335b64821766ab7ed6427c4ebb85 Adding singleton example diff -r 04f97f1bc2da -r 8e220093e4bf .hgignore --- a/.hgignore Wed Jan 27 18:30:40 2010 +0100 +++ b/.hgignore Wed Jan 27 18:30:57 2010 +0100 @@ -1,5 +1,6 @@ .*/build/.* .*/dist/.* .*/nbproject/private/.* -.*orig$ +.*.orig$ +.*~$ diff -r 04f97f1bc2da -r 8e220093e4bf samples/componentinjection/anagram-plain/build.xml --- a/samples/componentinjection/anagram-plain/build.xml Wed Jan 27 18:30:40 2010 +0100 +++ b/samples/componentinjection/anagram-plain/build.xml Wed Jan 27 18:30:57 2010 +0100 @@ -2,6 +2,11 @@ + + + + + Builds, tests, and runs the project anagram-plain. diff -r 04f97f1bc2da -r 8e220093e4bf samples/componentinjection/anagram-plain/nbproject/build-impl.xml --- a/samples/componentinjection/anagram-plain/nbproject/build-impl.xml Wed Jan 27 18:30:40 2010 +0100 +++ b/samples/componentinjection/anagram-plain/nbproject/build-impl.xml Wed Jan 27 18:30:57 2010 +0100 @@ -19,7 +19,14 @@ - cleanup --> - + + + + + + + + @@ -152,14 +190,23 @@ - + + - + + + + + + + + - + + @@ -198,7 +245,7 @@ - + @@ -213,18 +260,19 @@ + - + - + @@ -235,7 +283,9 @@ - + + + @@ -253,6 +303,12 @@ + + + + + + @@ -261,8 +317,11 @@ + - + + + @@ -279,12 +338,16 @@ + + + + - + @@ -308,7 +371,29 @@ COMPILATION SECTION =================== --> - + + + + + + + + + + + + + + + + + + + + + + + @@ -317,10 +402,15 @@ - + + + + + + - + @@ -329,7 +419,7 @@ - + @@ -337,13 +427,13 @@ Must select some files in the IDE or set javac.includes - + - + - + - + @@ -403,11 +493,53 @@ java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + diff -r 04f97f1bc2da -r 8e220093e4bf samples/componentinjection/anagram-plain/nbproject/genfiles.properties --- a/samples/componentinjection/anagram-plain/nbproject/genfiles.properties Wed Jan 27 18:30:40 2010 +0100 +++ b/samples/componentinjection/anagram-plain/nbproject/genfiles.properties Wed Jan 27 18:30:57 2010 +0100 @@ -1,8 +1,8 @@ build.xml.data.CRC32=a145f2df -build.xml.script.CRC32=d6f783b7 -build.xml.stylesheet.CRC32=be360661 +build.xml.script.CRC32=5535cf40 +build.xml.stylesheet.CRC32=958a1d3e@1.34.0.45 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. nbproject/build-impl.xml.data.CRC32=a145f2df -nbproject/build-impl.xml.script.CRC32=751fa40b -nbproject/build-impl.xml.stylesheet.CRC32=f1d9da08 +nbproject/build-impl.xml.script.CRC32=e5485127 +nbproject/build-impl.xml.stylesheet.CRC32=576378a2@1.34.0.45 diff -r 04f97f1bc2da -r 8e220093e4bf samples/componentinjection/anagram-plain/nbproject/project.properties --- a/samples/componentinjection/anagram-plain/nbproject/project.properties Wed Jan 27 18:30:40 2010 +0100 +++ b/samples/componentinjection/anagram-plain/nbproject/project.properties Wed Jan 27 18:30:57 2010 +0100 @@ -3,6 +3,7 @@ # This directory is removed when the project is cleaned: build.dir=build build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources # Only compile against the classpath explicitly listed here: build.sysclasspath=ignore build.test.classes.dir=${build.dir}/test/classes diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/build.xml Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project singletons. + + + diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/nbproject/build-impl.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/nbproject/build-impl.xml Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,805 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/nbproject/genfiles.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/nbproject/genfiles.properties Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,8 @@ +build.xml.data.CRC32=5a9b3034 +build.xml.script.CRC32=28fad8e6 +build.xml.stylesheet.CRC32=958a1d3e@1.34.0.45 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=5a9b3034 +nbproject/build-impl.xml.script.CRC32=a0976efa +nbproject/build-impl.xml.stylesheet.CRC32=576378a2@1.34.0.45 diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/nbproject/project.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/nbproject/project.properties Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,66 @@ +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/singletons.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +file.reference.org-netbeans-insane.jar=../libs/dist/org-netbeans-insane.jar +file.reference.org-netbeans-modules-nbjunit.jar=../libs/dist/org-netbeans-modules-nbjunit.jar +file.reference.org-openide-util-lookup.jar=../libs/dist/org-openide-util-lookup.jar +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.org-openide-util-lookup.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit_4.classpath}:\ + ${file.reference.org-netbeans-insane.jar}:\ + ${file.reference.org-netbeans-modules-nbjunit.jar} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=org.apidesign.singletons.usage.Main +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/nbproject/project.xml Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + singletons + + + + + + + + + diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/src/org/apidesign/singletons/api/DialogDisplayer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/src/org/apidesign/singletons/api/DialogDisplayer.java Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,60 @@ +package org.apidesign.singletons.api; + +import java.util.Iterator; +import java.util.ServiceLoader; +import org.openide.util.Lookup; + +// BEGIN: singletons.injectable.api +public abstract class DialogDisplayer { + protected DialogDisplayer() { + } + + /** Ask user a question. + * + * @param query the text of the question + * @return true if user confirmed or false if declined + */ + public abstract boolean yesOrNo(String query); + + public static DialogDisplayer getDefault() { + return Impl.DEFAULT; + } + // FINISH: singletons.injectable.api + + + // BEGIN: singletons.injectable.dummyimpl + private static final class Impl extends DialogDisplayer { + private static final DialogDisplayer DEFAULT = initialize(); + + @Override + public boolean yesOrNo(String query) { + System.err.printf("Saying no to '%s'\n", query); + return false; + } + // FINISH: singletons.injectable.dummyimpl + + private static DialogDisplayer initialize() { + if (Boolean.getBoolean("singleton.jdk6")) { + return initializeServiceLoader(); + } else { + return initializeLookup(); + } + } + + // BEGIN: singletons.injectable.serviceloader + private static DialogDisplayer initializeServiceLoader() { + // see http://singletons.apidesign.org + Iterator it = null; + it = ServiceLoader.load(DialogDisplayer.class).iterator(); + return it != null && it.hasNext() ? it.next() : new Impl(); + } + // END: singletons.injectable.serviceloader + + // BEGIN: singletons.injectable.lookup + private static DialogDisplayer initializeLookup() { + DialogDisplayer def = Lookup.getDefault().lookup(DialogDisplayer.class); + return def != null ? def : new Impl(); + } + // END: singletons.injectable.lookup + } +} diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/src/org/apidesign/singletons/impl/DefaultDialogDisplayer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/src/org/apidesign/singletons/impl/DefaultDialogDisplayer.java Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,14 @@ +package org.apidesign.singletons.impl; + +import javax.swing.JOptionPane; +import org.apidesign.singletons.api.DialogDisplayer; +import org.openide.util.lookup.ServiceProvider; + +@ServiceProvider(service=DialogDisplayer.class) +public final class DefaultDialogDisplayer extends DialogDisplayer { + @Override + public boolean yesOrNo(String query) { + final int res = JOptionPane.showConfirmDialog(null, query); + return res == JOptionPane.OK_OPTION; + } +} diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/src/org/apidesign/singletons/usage/Main.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/src/org/apidesign/singletons/usage/Main.java Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,15 @@ +package org.apidesign.singletons.usage; + +import org.apidesign.singletons.api.DialogDisplayer; + +public class Main { + public static void main(String[] args) { + if (DialogDisplayer.getDefault().yesOrNo("Do you like singletons?")) { + System.err.println("OK, thank you!"); + } else { + System.err.println( + "Visit http://singletons.apidesign.org to change your mind!" + ); + } + } +} diff -r 04f97f1bc2da -r 8e220093e4bf samples/singletons/test/org/apidesign/singletons/usage/MainTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/singletons/test/org/apidesign/singletons/usage/MainTest.java Wed Jan 27 18:30:57 2010 +0100 @@ -0,0 +1,37 @@ +package org.apidesign.singletons.usage; + +import org.apidesign.singletons.api.DialogDisplayer; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.netbeans.junit.MockServices; +import static org.junit.Assert.*; + +public class MainTest { + + public MainTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + MockServices.setServices(MockDialogDisplayer.class); + } + + @Test + public void testMainAsksAQuestion() { + assertNull("No question asked yet", MockDialogDisplayer.askedQuery); + Main.main(new String[0]); + assertNotNull("main code asked our Mock displayer", MockDialogDisplayer.askedQuery); + } + + public static final class MockDialogDisplayer extends DialogDisplayer { + static String askedQuery; + + @Override + public boolean yesOrNo(String query) { + askedQuery = query; + return false; + } + + } +} \ No newline at end of file