
de.eleon.console.builder.functional.Transformers Maven / Gradle / Ivy
/*
* Copyright 2014 Dominik Foerderreuther
*
* 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 de.eleon.console.builder.functional;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
public class Transformers {
public static > Transformer toEnum(final Class enumClass) {
return new Transformer() {
@Override
public T apply(final String userIn) {
Optional ret = FluentIterable
.from(newArrayList(enumClass.getEnumConstants()))
.firstMatch(new Predicate() {
@Override
public boolean apply(T enumValue) {
return enumValue.toString()
.equalsIgnoreCase(userIn);
}
});
if (!ret.isPresent()) throw new IllegalArgumentException(format("could not find value %s in enum %s", userIn, enumClass.getName()));
return ret.get();
}
};
}
public static Transformer toInteger() {
return new Transformer() {
@Override
public Integer apply(final String userIn) {
return Integer.parseInt(userIn.trim());
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy