samples/misuse/src/org/apidesign/misuse/projectconfig/ProjectConfigurationCorrect.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
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@154
     5
    interface ProjectConfigurationProvider
jtulach@154
     6
        <Configuration extends ProjectConfiguration> {
jtulach@210
     7
        public Configuration[] getConfigurations();
jtulach@210
     8
        public Configuration getActive();
jtulach@210
     9
        public void setActive(Configuration c);
jtulach@101
    10
    }
jtulach@101
    11
    interface ProjectConfiguration {
jtulach@101
    12
        public String getDisplayName();
jtulach@101
    13
    }
jtulach@101
    14
    // END: misuse.prjconfig.correct
jtulach@101
    15
    
jtulach@101
    16
    
jtulach@101
    17
jtulach@101
    18
    /* Following does not work:
jtulach@101
    19
    static {
jtulach@101
    20
        // BEGIN: misuse.prjconfig.correct.trivial.access
jtulach@154
    21
        ProjectConfigurationProvider<?> provider = null; // obtain elsewhere;
jtulach@101
    22
        provider.setActive(provider.getConfigurations()[0]);
jtulach@101
    23
        // END: misuse.prjconfig.correct.trivial.access
jtulach@101
    24
    }
jtulach@101
    25
    */
jtulach@101
    26
jtulach@210
    27
    static 
jtulach@210
    28
    // BEGIN: misuse.prjconfig.correct.access
jtulach@210
    29
    { 
jtulach@154
    30
        ProjectConfigurationProvider<?> provider = null; // obtain elsewhere;
jtulach@101
    31
        resetToZero(provider);
jtulach@101
    32
    }
jtulach@210
    33
    // END: misuse.prjconfig.correct.access
jtulach@101
    34
jtulach@101
    35
    // BEGIN: misuse.prjconfig.correct.openmethod
jtulach@101
    36
    private static <C extends ProjectConfiguration> void resetToZero(
jtulach@101
    37
        ProjectConfigurationProvider<C> provider
jtulach@101
    38
    ) {
jtulach@101
    39
        provider.setActive(provider.getConfigurations()[0]);
jtulach@101
    40
    }
jtulach@101
    41
    // END: misuse.prjconfig.correct.openmethod
jtulach@101
    42
jtulach@101
    43
    // BEGIN: misuse.prjconfig.correct.openclass
jtulach@154
    44
    static void workWithProjectConfigurationProvider(
jtulach@154
    45
        ProjectConfigurationProvider<?> p
jtulach@154
    46
    ) {
jtulach@101
    47
        ResetToZero<?> rtz = ResetToZero.create(p);
jtulach@101
    48
        rtz.obtainFirst();
jtulach@101
    49
        // after a while
jtulach@101
    50
        rtz.apply();
jtulach@101
    51
    }
jtulach@101
    52
jtulach@101
    53
    static class ResetToZero<C extends ProjectConfiguration> {
jtulach@101
    54
        C active;
jtulach@101
    55
        final ProjectConfigurationProvider<C> provider;
jtulach@101
    56
jtulach@101
    57
        ResetToZero(ProjectConfigurationProvider<C> provider) {
jtulach@101
    58
            this.provider = provider;
jtulach@101
    59
        }
jtulach@101
    60
jtulach@154
    61
        static <C extends ProjectConfiguration> ResetToZero<C> create(
jtulach@154
    62
            ProjectConfigurationProvider<C> p
jtulach@154
    63
        ) {
jtulach@101
    64
            return new ResetToZero<C>(p);
jtulach@101
    65
        }
jtulach@101
    66
jtulach@101
    67
        public void obtainFirst() {
jtulach@101
    68
            active = provider.getConfigurations()[0];
jtulach@101
    69
        }
jtulach@101
    70
jtulach@101
    71
        public void apply() {
jtulach@101
    72
            provider.setActive(active);
jtulach@101
    73
        }
jtulach@101
    74
    }
jtulach@101
    75
    // END: misuse.prjconfig.correct.openclass
jtulach@101
    76
jtulach@101
    77
}