author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:58:11 +0200 | |
changeset 154 | 0fd5e9c500b9 |
parent 153 | b5cbb797ec0a |
child 201 | 784b78c2436f |
permissions | -rw-r--r-- |
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@101 | 7 |
Configuration[] getConfigurations(); |
jtulach@101 | 8 |
Configuration getActive(); |
jtulach@101 | 9 |
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@101 | 27 |
static { |
jtulach@101 | 28 |
// BEGIN: misuse.prjconfig.correct.access |
jtulach@154 | 29 |
ProjectConfigurationProvider<?> provider = null; // obtain elsewhere; |
jtulach@101 | 30 |
resetToZero(provider); |
jtulach@101 | 31 |
// END: misuse.prjconfig.correct.access |
jtulach@101 | 32 |
} |
jtulach@101 | 33 |
|
jtulach@101 | 34 |
// BEGIN: misuse.prjconfig.correct.openmethod |
jtulach@101 | 35 |
private static <C extends ProjectConfiguration> void resetToZero( |
jtulach@101 | 36 |
ProjectConfigurationProvider<C> provider |
jtulach@101 | 37 |
) { |
jtulach@101 | 38 |
provider.setActive(provider.getConfigurations()[0]); |
jtulach@101 | 39 |
} |
jtulach@101 | 40 |
// END: misuse.prjconfig.correct.openmethod |
jtulach@101 | 41 |
|
jtulach@101 | 42 |
// BEGIN: misuse.prjconfig.correct.openclass |
jtulach@154 | 43 |
static void workWithProjectConfigurationProvider( |
jtulach@154 | 44 |
ProjectConfigurationProvider<?> p |
jtulach@154 | 45 |
) { |
jtulach@101 | 46 |
ResetToZero<?> rtz = ResetToZero.create(p); |
jtulach@101 | 47 |
rtz.obtainFirst(); |
jtulach@101 | 48 |
// after a while |
jtulach@101 | 49 |
rtz.apply(); |
jtulach@101 | 50 |
} |
jtulach@101 | 51 |
|
jtulach@101 | 52 |
static class ResetToZero<C extends ProjectConfiguration> { |
jtulach@101 | 53 |
C active; |
jtulach@101 | 54 |
final ProjectConfigurationProvider<C> provider; |
jtulach@101 | 55 |
|
jtulach@101 | 56 |
ResetToZero(ProjectConfigurationProvider<C> provider) { |
jtulach@101 | 57 |
this.provider = provider; |
jtulach@101 | 58 |
} |
jtulach@101 | 59 |
|
jtulach@154 | 60 |
static <C extends ProjectConfiguration> ResetToZero<C> create( |
jtulach@154 | 61 |
ProjectConfigurationProvider<C> p |
jtulach@154 | 62 |
) { |
jtulach@101 | 63 |
return new ResetToZero<C>(p); |
jtulach@101 | 64 |
} |
jtulach@101 | 65 |
|
jtulach@101 | 66 |
public void obtainFirst() { |
jtulach@101 | 67 |
active = provider.getConfigurations()[0]; |
jtulach@101 | 68 |
} |
jtulach@101 | 69 |
|
jtulach@101 | 70 |
public void apply() { |
jtulach@101 | 71 |
provider.setActive(active); |
jtulach@101 | 72 |
} |
jtulach@101 | 73 |
} |
jtulach@101 | 74 |
// END: misuse.prjconfig.correct.openclass |
jtulach@101 | 75 |
|
jtulach@101 | 76 |
} |