org.rapidoid.var.Vars Maven / Gradle / Ivy
package org.rapidoid.var;
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.lambda.Predicate;
import org.rapidoid.var.impl.MandatoryVar;
import org.rapidoid.var.impl.SimpleVar;
import org.rapidoid.var.impl.ValidatingVar;
/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* 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.
* #L%
*/
@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
public class Vars extends RapidoidThing {
public static Var var(String name, T value) {
return new SimpleVar(name, value);
}
@SuppressWarnings("unchecked")
public static Var[] vars(String name, T... values) {
Var[] vars = new Var[values.length];
for (int i = 0; i < vars.length; i++) {
vars[i] = var(name + "[" + i + "]", values[i]);
}
return vars;
}
public static Var mandatory(Var var) {
return new MandatoryVar(var);
}
@SuppressWarnings("unchecked")
public static T unwrap(T value) {
return (value instanceof Var) ? (T) ((Var>) value).get() : value;
}
@SuppressWarnings("unchecked")
public static Var cast(Object value) {
return (Var) value;
}
public static Var validate(Var var, Predicate isValid, String message) {
return new ValidatingVar(var, isValid, message);
}
}