samples/misuse/src/org/apidesign/misuse/projectconfig/ProjectConfigurationCorrect.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:08 +0200
changeset 153 b5cbb797ec0a
parent 132 3bc4c54f4bcc
child 154 0fd5e9c500b9
permissions -rw-r--r--
up to line 2000
jtulach@101
     1
package org.apidesign.misuse.projectconfig;
jtulach@101
     2
jtulach@101
     3
public class ProjectConfigurationCorrect {
jtulach@101
     4
    // BEGIN: misuse.prjconfig.correct
jtulach@153
     5
    interface ProjectConfigurationProvider<Configuration extends ProjectConfiguration> {
jtulach@101
     6
        Configuration[] getConfigurations();
jtulach@101
     7
        Configuration getActive();
jtulach@101
     8
        void setActive(Configuration c);
jtulach@101
     9
    }
jtulach@101
    10
    interface ProjectConfiguration {
jtulach@101
    11
        public String getDisplayName();
jtulach@101
    12
    }
jtulach@101
    13
    // END: misuse.prjconfig.correct
jtulach@101
    14
    
jtulach@101
    15
    
jtulach@101
    16
jtulach@101
    17
    /* Following does not work:
jtulach@101
    18
    static {
jtulach@101
    19
        // BEGIN: misuse.prjconfig.correct.trivial.access
jtulach@153
    20
        ProjectConfigurationProvider<?> provider = null; // obtain from somewhere;
jtulach@101
    21
        provider.setActive(provider.getConfigurations()[0]);
jtulach@101
    22
        // END: misuse.prjconfig.correct.trivial.access
jtulach@101
    23
    }
jtulach@101
    24
    */
jtulach@101
    25
jtulach@101
    26
    static { 
jtulach@101
    27
        // BEGIN: misuse.prjconfig.correct.access
jtulach@153
    28
        ProjectConfigurationProvider<?> provider = null; // obtain from somewhere;
jtulach@101
    29
        resetToZero(provider);
jtulach@101
    30
        // END: misuse.prjconfig.correct.access
jtulach@101
    31
    }
jtulach@101
    32
jtulach@101
    33
    // BEGIN: misuse.prjconfig.correct.openmethod
jtulach@101
    34
    private static <C extends ProjectConfiguration> void resetToZero(
jtulach@101
    35
        ProjectConfigurationProvider<C> provider
jtulach@101
    36
    ) {
jtulach@101
    37
        provider.setActive(provider.getConfigurations()[0]);
jtulach@101
    38
    }
jtulach@101
    39
    // END: misuse.prjconfig.correct.openmethod
jtulach@101
    40
jtulach@101
    41
    // BEGIN: misuse.prjconfig.correct.openclass
jtulach@153
    42
    static void workWithProjectConfigurationProvider(ProjectConfigurationProvider<?> p) {
jtulach@101
    43
        ResetToZero<?> rtz = ResetToZero.create(p);
jtulach@101
    44
        rtz.obtainFirst();
jtulach@101
    45
        // after a while
jtulach@101
    46
        rtz.apply();
jtulach@101
    47
    }
jtulach@101
    48
jtulach@101
    49
    static class ResetToZero<C extends ProjectConfiguration> {
jtulach@101
    50
        C active;
jtulach@101
    51
        final ProjectConfigurationProvider<C> provider;
jtulach@101
    52
jtulach@101
    53
        ResetToZero(ProjectConfigurationProvider<C> provider) {
jtulach@101
    54
            this.provider = provider;
jtulach@101
    55
        }
jtulach@101
    56
jtulach@153
    57
        static <C extends ProjectConfiguration> ResetToZero<C> create(ProjectConfigurationProvider<C> p) {
jtulach@101
    58
            return new ResetToZero<C>(p);
jtulach@101
    59
        }
jtulach@101
    60
jtulach@101
    61
        public void obtainFirst() {
jtulach@101
    62
            active = provider.getConfigurations()[0];
jtulach@101
    63
        }
jtulach@101
    64
jtulach@101
    65
        public void apply() {
jtulach@101
    66
            provider.setActive(active);
jtulach@101
    67
        }
jtulach@101
    68
    }
jtulach@101
    69
    // END: misuse.prjconfig.correct.openclass
jtulach@101
    70
jtulach@101
    71
}