
core.main.RioMethodMap Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.rio.core.main;
import java.lang.reflect.Method;
import java.util.List;
import br.com.objectos.rio.annotations.RioMethod;
import br.com.objectos.way.core.util.WayIterable;
import br.com.objectos.way.core.util.WayIterables;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
/**
* @author [email protected] (Marcio Endo)
*/
public class RioMethodMap {
private final ListMultimap nameMap;
private RioMethodMap(ListMultimap nameMap) {
this.nameMap = nameMap;
}
public static RioMethodMap mapOf(Iterable> classes) {
WayIterable> iterable = WayIterables.> from(classes);
return mapOf(iterable);
}
public static RioMethodMap mapOf(Class>... classes) {
WayIterable> iterable = WayIterables.from(classes);
return mapOf(iterable);
}
private static RioMethodMap mapOf(WayIterable> classIterable) {
return classIterable
.transformAndConcat(new ClassToMethodList())
.filter(new MethodIsValid())
.transform(new MethodToMethodWrapper())
.asMap(new Function, RioMethodMap>() {
@Override
public RioMethodMap apply(List list) {
ListMultimap nameMap = Multimaps.index(list, RioMethodWrapperGetName.get());
return new RioMethodMap(nameMap);
}
});
}
public Optional get(String methodName, List argumentList) {
Optional res = Optional.absent();
List candidateList = nameMap.get(methodName);
for (RioMethodWrapper candidate : candidateList) {
if (candidate.matchesArgumentList(argumentList)) {
res = candidate.unwrap();
break;
}
}
return res;
}
private static class ClassToMethodList implements Function, List> {
@Override
public List apply(Class> input) {
Method[] methods = input.getMethods();
return ImmutableList.copyOf(methods);
}
}
private static class MethodIsValid implements Predicate {
@Override
public boolean apply(Method input) {
return input.isAnnotationPresent(RioMethod.class);
}
}
private static class MethodToMethodWrapper implements Function {
@Override
public RioMethodWrapper apply(Method input) {
return RioMethodWrapper.wrap(input);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy