br.com.objectos.way.code.AnnotationInfoMap Maven / Gradle / Ivy
/*
* Copyright 2014 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.way.code;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import br.com.objectos.way.core.testing.Testable;
import br.com.objectos.way.core.testing.Testables;
import br.com.objectos.way.core.util.WayIterables;
import br.com.objectos.way.core.util.WayMap;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
/**
* @author [email protected] (Marcio Endo)
*/
public class AnnotationInfoMap implements Testable, WayMap {
private static final AnnotationInfoMap EMPTY_MAP = mapOf(ImmutableList. of());
private final List list;
private final Map qualifiedNameMap;
private AnnotationInfoMap(List list) {
this.list = list;
qualifiedNameMap = Maps.uniqueIndex(list, AnnotationInfoToQualifiedName.get());
}
public static AnnotationInfoMap emptyMap() {
return EMPTY_MAP;
}
public static AnnotationInfoMap mapOf(List list) {
return new AnnotationInfoMap(list);
}
public Optional get(Class> type) {
String qname = type.getName();
AnnotationInfo maybe = qualifiedNameMap.get(qname);
return Optional.fromNullable(maybe);
}
public List getAnnotatedWith(Class extends Annotation> type) {
return WayIterables.from(list)
.filter(AnnotationInfoHasAnnotation.get(type))
.toImmutableList();
}
public Optional getFirstAnnotatedWith(Class extends Annotation> type) {
return WayIterables.from(list)
.filter(AnnotationInfoHasAnnotation.get(type))
.first();
}
@Override
public boolean isEqual(AnnotationInfoMap that) {
return Testables.isEqualHelper()
.equal(list, that.list)
.result();
}
@Override
public String toString() {
return list.toString();
}
@Override
public List list() {
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy