net.e6tech.elements.common.resources.Bindings Maven / Gradle / Ivy
/*
* Copyright 2017 Futeh Kao
*
* 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 net.e6tech.elements.common.resources;
import net.e6tech.elements.common.util.function.ConsumerWithException;
import net.e6tech.elements.common.util.function.FunctionWithException;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("squid:S1700")
public class Bindings {
private Resources resources;
private Map bindings = new HashMap<>();
public Bindings(Resources resources) {
this.resources = resources;
}
@SuppressWarnings("unchecked")
public T get(Class cls) {
Binding binding = bindings.computeIfAbsent(cls, key -> resources.getBinding(cls));
return binding.get();
}
@SuppressWarnings("unchecked")
public Bindings rebind(Class cls, T newValue) {
Binding binding = bindings.computeIfAbsent(cls, key -> resources.getBinding(cls));
binding.rebind(newValue);
return this;
}
public void restore() {
bindings.values().forEach(Binding::restore);
}
public void rebind(ConsumerWithException consumer) throws E {
try {
consumer.accept(this);
} finally {
restore();
}
}
public T rebind(FunctionWithException function) throws E {
try {
return function.apply(this);
} finally {
restore();
}
}
}