com.github.robozonky.common.state.StateModifierImpl Maven / Gradle / Ivy
/*
* Copyright 2018 The RoboZonky Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robozonky.common.state;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import com.github.robozonky.internal.test.DateUtil;
final class StateModifierImpl implements StateModifier,
Callable {
private final Collection> actions = new ArrayList<>(0);
private final InstanceStateImpl instanceState;
public StateModifierImpl(final InstanceStateImpl instanceState, final boolean fresh) {
this.instanceState = instanceState;
if (fresh) { // first action is to reset the class-specific state
actions.add(StateStorage::unsetValues);
}
}
@Override
public StateModifier put(final String key, final String value) {
actions.add((state, section) -> state.setValue(section, key, value));
return this;
}
@Override
public StateModifier remove(final String key) {
final BiConsumer action = (state, section) -> state.unsetValue(section, key);
actions.add(action);
return this;
}
@Override
public Boolean call() {
final String sectionName = instanceState.getSectionName();
final StateStorage backend = instanceState.getStorage();
actions.forEach(a -> a.accept(backend, sectionName));
backend.setValue(sectionName, Constants.LAST_UPDATED_KEY.getValue(), DateUtil.offsetNow().toString());
return backend.store();
}
}