Last active
August 29, 2015 14:00
-
-
Save jstefek/11177195 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class AttributeSetup<T> { // better name ? | |
| private final T defaultValue; | |
| private T actualValue; | |
| public AttributeSetup(T defaultValue) { | |
| if (defaultValue == null) { | |
| throw new IllegalArgumentException("Default value cannot be null!"); | |
| } | |
| this.defaultValue = defaultValue; | |
| this.actualValue = defaultValue; | |
| } | |
| public T getValue() { | |
| return actualValue; | |
| } | |
| public void resetValue() { | |
| actualValue = defaultValue; | |
| } | |
| public void setValue(T value) { | |
| if (value == null) { | |
| throw new IllegalArgumentException("Cannot set value to null!"); | |
| } | |
| actualValue = value; | |
| } | |
| public static <T> AttributeSetup<T> createAttributeSetup(T defaultValue) { | |
| return new AttributeSetup<T>(defaultValue); | |
| } | |
| } | |
| // with previous value ? | |
| public class AttributeSetupWithPreviousValue<T> extends Storage<T> { | |
| public void setToPreviousValue(); | |
| } | |
| ======================== | |
| Use | |
| ======================== | |
| Transform this: | |
| private static final Event DEFAULT_EVENT = Event.CLICK; | |
| private Event toggleEvent = DEFAULT_EVENT; | |
| public Event getToggleEvent() { | |
| return toggleEvent; | |
| } | |
| public void setupToggleEvent() { | |
| toggleEvent = DEFAULT_EVENT; | |
| } | |
| public void setupToggleEvent(Event e) { | |
| toggleEvent = e; | |
| } | |
| fragment.advanced().setupToggleEvent(event); | |
| fragment.advanced().setupToggleEvent(); | |
| fragment.advanced().getToggleEvent(); | |
| To this: | |
| private final AttributeSetup<Event> toggleEvent = AttributeSetup.createAttributeSetup(Event.CLICK); | |
| public AttributeSetup<Event> toggleEvent() {// better name? | |
| return toggleEvent; | |
| } | |
| fragment.advanced().toggleEvent().setValue(event); | |
| fragment.advanced().toggleEvent().resetValue(); | |
| fragment.advanced().toggleEvent().getValue(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment