Created
December 27, 2012 13:34
-
-
Save neomatrix369/4388418 to your computer and use it in GitHub Desktop.
New example class created - src\test\benchmarks\org\mutabilitydetector\benchmarks\ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mutabilitydetector.benchmarks; | |
public class ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern { | |
private final String field; | |
// usual method of making a class immutable | |
// - make its constructor private: ref EffectiveJava | |
private ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern (String field) { | |
this.field = field; | |
} | |
public String getField() { | |
return field; | |
} | |
// inner Builder class | |
public static class Builder { | |
public ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern build() { | |
// this new OnlyPrivateConstructors() is fooling mutability detector | |
// it thinks OnlyPrivateConstructors() is no longer immutable due to the | |
// ability to call new to create an instance of OnlyPrivateConstructors. | |
return new ImmutableByHavingOnlyAPrivateConstructorUsingTheBuilderPattern("hi"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment