More code snippets for the mutex example
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 25 May 2009 18:27:02 +0200
changeset 330be82436a7a8b
parent 329 918e6f7f8136
child 331 b5228ed41a43
More code snippets for the mutex example
samples/privilegedcreator/build.xml
samples/privilegedcreator/src/api/Executors.java
samples/privilegedcreator/src/api/Mutex.java
samples/privilegedcreator/src/org/apidesign/privileged/api/Executors.java
samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java
samples/privilegedcreator/test/api/MutexTest.java
     1.1 --- a/samples/privilegedcreator/build.xml	Wed Apr 29 11:56:47 2009 +0200
     1.2 +++ b/samples/privilegedcreator/build.xml	Mon May 25 18:27:02 2009 +0200
     1.3 @@ -2,6 +2,11 @@
     1.4  <!-- You may freely edit this file. See commented blocks below for -->
     1.5  <!-- some examples of how to customize the build. -->
     1.6  <!-- (If you delete it and reopen the project it will be recreated.) -->
     1.7 +<!-- By default, only the Clean and Build commands use this build script. -->
     1.8 +<!-- Commands such as Run, Debug, and Test only use this build script if -->
     1.9 +<!-- the Compile on Save feature is turned off for the project. -->
    1.10 +<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
    1.11 +<!-- in the project's Project Properties dialog box.-->
    1.12  <project name="privilegedcreator" default="default" basedir=".">
    1.13      <description>Builds, tests, and runs the project privilegedcreator.</description>
    1.14      <import file="nbproject/build-impl.xml"/>
     2.1 --- a/samples/privilegedcreator/src/api/Executors.java	Wed Apr 29 11:56:47 2009 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,60 +0,0 @@
     2.4 -
     2.5 -package api;
     2.6 -
     2.7 -import java.util.concurrent.Executor;
     2.8 -
     2.9 -/**
    2.10 - *
    2.11 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.12 - */
    2.13 -public final class Executors {
    2.14 -    /** let's prefer factory methods */
    2.15 -    private Executors() {
    2.16 -    }
    2.17 -    
    2.18 -    
    2.19 -    public static Executor create() {
    2.20 -        return new Simple();
    2.21 -    }
    2.22 -    
    2.23 -    public static Executor create(boolean fair) {
    2.24 -        Configuration conf = new Configuration();
    2.25 -        conf.setFair(fair);
    2.26 -        return new Fair(conf);
    2.27 -    }
    2.28 -
    2.29 -    // BEGIN: design.less.privileged
    2.30 -    public static Executor create(Configuration config) {
    2.31 -        return new Fair(config);
    2.32 -    }
    2.33 -    
    2.34 -    public static final class Configuration {
    2.35 -        boolean fair;
    2.36 -        int maxWaiters = -1;
    2.37 -        
    2.38 -        public void setFair(boolean fair) {
    2.39 -            this.fair = fair;
    2.40 -        }
    2.41 -        public void setMaxWaiters(int max) {
    2.42 -            this.maxWaiters = max;
    2.43 -        }
    2.44 -    }
    2.45 -    // END: design.less.privileged
    2.46 -    
    2.47 -    private static final class Simple implements Executor {
    2.48 -        public synchronized void execute(Runnable command) {
    2.49 -            command.run();
    2.50 -        }
    2.51 -    }
    2.52 -    private static final class Fair implements Executor {
    2.53 -        private final Configuration conf;
    2.54 -        
    2.55 -        public Fair(Configuration conf) {
    2.56 -            this.conf = conf;
    2.57 -        }
    2.58 -        
    2.59 -        public void execute(Runnable command) {
    2.60 -            // TBD
    2.61 -        }
    2.62 -    }
    2.63 -}
     3.1 --- a/samples/privilegedcreator/src/api/Mutex.java	Wed Apr 29 11:56:47 2009 +0200
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,51 +0,0 @@
     3.4 -/*
     3.5 - * To change this template, choose Tools | Templates
     3.6 - * and open the template in the editor.
     3.7 - */
     3.8 -
     3.9 -package api;
    3.10 -
    3.11 -import java.util.concurrent.locks.Lock;
    3.12 -import java.util.concurrent.locks.ReentrantLock;
    3.13 -
    3.14 -/**
    3.15 - *
    3.16 - * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
    3.17 - */
    3.18 -public class Mutex {
    3.19 -    Lock lock = new ReentrantLock();
    3.20 -    
    3.21 -    public Mutex() {
    3.22 -    }
    3.23 -
    3.24 -    
    3.25 -    public void readAccess(Runnable r) {
    3.26 -        try {
    3.27 -            lock.lock();
    3.28 -            r.run();
    3.29 -        } finally {
    3.30 -            lock.unlock();
    3.31 -        }
    3.32 -    }
    3.33 -    
    3.34 -
    3.35 -    
    3.36 -    public Mutex(Privileged privileged) {
    3.37 -        if (privileged.mutex != null) {
    3.38 -            throw new IllegalStateException();
    3.39 -        }
    3.40 -        privileged.mutex = this;
    3.41 -    }
    3.42 -    
    3.43 -    public static final class Privileged {
    3.44 -        private Mutex mutex;
    3.45 -        
    3.46 -        public void enterReadAccess() {
    3.47 -            mutex.lock.lock();
    3.48 -        }
    3.49 -        
    3.50 -        public void exitReadAccess() {
    3.51 -            mutex.lock.unlock();
    3.52 -        }
    3.53 -    }
    3.54 -}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/privilegedcreator/src/org/apidesign/privileged/api/Executors.java	Mon May 25 18:27:02 2009 +0200
     4.3 @@ -0,0 +1,60 @@
     4.4 +
     4.5 +package org.apidesign.privileged.api;
     4.6 +
     4.7 +import java.util.concurrent.Executor;
     4.8 +
     4.9 +/**
    4.10 + *
    4.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.12 + */
    4.13 +public final class Executors {
    4.14 +    /** let's prefer factory methods */
    4.15 +    private Executors() {
    4.16 +    }
    4.17 +    
    4.18 +    
    4.19 +    public static Executor create() {
    4.20 +        return new Simple();
    4.21 +    }
    4.22 +    
    4.23 +    public static Executor create(boolean fair) {
    4.24 +        Configuration conf = new Configuration();
    4.25 +        conf.setFair(fair);
    4.26 +        return new Fair(conf);
    4.27 +    }
    4.28 +
    4.29 +    // BEGIN: design.less.privileged
    4.30 +    public static Executor create(Configuration config) {
    4.31 +        return new Fair(config);
    4.32 +    }
    4.33 +    
    4.34 +    public static final class Configuration {
    4.35 +        boolean fair;
    4.36 +        int maxWaiters = -1;
    4.37 +        
    4.38 +        public void setFair(boolean fair) {
    4.39 +            this.fair = fair;
    4.40 +        }
    4.41 +        public void setMaxWaiters(int max) {
    4.42 +            this.maxWaiters = max;
    4.43 +        }
    4.44 +    }
    4.45 +    // END: design.less.privileged
    4.46 +    
    4.47 +    private static final class Simple implements Executor {
    4.48 +        public synchronized void execute(Runnable command) {
    4.49 +            command.run();
    4.50 +        }
    4.51 +    }
    4.52 +    private static final class Fair implements Executor {
    4.53 +        private final Configuration conf;
    4.54 +        
    4.55 +        public Fair(Configuration conf) {
    4.56 +            this.conf = conf;
    4.57 +        }
    4.58 +        
    4.59 +        public void execute(Runnable command) {
    4.60 +            // TBD
    4.61 +        }
    4.62 +    }
    4.63 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java	Mon May 25 18:27:02 2009 +0200
     5.3 @@ -0,0 +1,52 @@
     5.4 +/*
     5.5 + * To change this template, choose Tools | Templates
     5.6 + * and open the template in the editor.
     5.7 + */
     5.8 +
     5.9 +package org.apidesign.privileged.api;
    5.10 +
    5.11 +import java.util.concurrent.locks.Lock;
    5.12 +import java.util.concurrent.locks.ReentrantLock;
    5.13 +
    5.14 +/**
    5.15 + *
    5.16 + * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
    5.17 + */
    5.18 +// BEGIN: mutex.api
    5.19 +public class Mutex {
    5.20 +    Lock lock = new ReentrantLock();
    5.21 +    
    5.22 +    public Mutex() {
    5.23 +    }
    5.24 +    
    5.25 +    public void readAccess(Runnable r) {
    5.26 +        try {
    5.27 +            lock.lock();
    5.28 +            r.run();
    5.29 +        } finally {
    5.30 +            lock.unlock();
    5.31 +        }
    5.32 +    }
    5.33 +// FINISH: mutex.api
    5.34 +
    5.35 +// BEGIN: mutex.privileged.api
    5.36 +    public Mutex(Privileged privileged) {
    5.37 +        if (privileged.mutex != null) {
    5.38 +            throw new IllegalStateException();
    5.39 +        }
    5.40 +        privileged.mutex = this;
    5.41 +    }
    5.42 +    
    5.43 +    public static final class Privileged {
    5.44 +        private Mutex mutex;
    5.45 +        
    5.46 +        public void enterReadAccess() {
    5.47 +            mutex.lock.lock();
    5.48 +        }
    5.49 +        
    5.50 +        public void exitReadAccess() {
    5.51 +            mutex.lock.unlock();
    5.52 +        }
    5.53 +    }
    5.54 +// END: mutex.privileged.api
    5.55 +}
     6.1 --- a/samples/privilegedcreator/test/api/MutexTest.java	Wed Apr 29 11:56:47 2009 +0200
     6.2 +++ b/samples/privilegedcreator/test/api/MutexTest.java	Mon May 25 18:27:02 2009 +0200
     6.3 @@ -1,5 +1,6 @@
     6.4  package api;
     6.5  
     6.6 +import org.apidesign.privileged.api.Mutex;
     6.7  import org.junit.After;
     6.8  import org.junit.AfterClass;
     6.9  import org.junit.Before;