All Downloads are FREE. Search and download functionalities are using the official Maven repository.

restx.description.OperationDescription Maven / Gradle / Ivy

There is a newer version: 1.2.0-rc2
Show newest version
package restx.description;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import restx.common.MoreAnnotations;
import restx.http.HttpStatus;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
 * User: xavierhanin
 * Date: 2/7/13
 * Time: 10:54 AM
 */
public class OperationDescription {
    public String httpMethod;
    public String nickname;
    public String responseClass;
    public String sourceLocation = "";
    public String inEntitySchemaKey = "";
    @JsonIgnore
    public Type inEntityType;
    public String outEntitySchemaKey = "";
    @JsonIgnore
    public Type outEntityType;
    public String summary = "";
    public String notes = "";
    public HttpStatus.Descriptor successStatus;
    public List parameters = Lists.newArrayList();
    public List errorResponses = Lists.newArrayList();
    public List relatedOperations = Lists.newArrayList();
    @JsonIgnore
    public ImmutableList annotations;

    public static class Matcher implements Predicate {
        public Predicate httpMethodMatcher = Predicates.alwaysTrue();
        public Predicate inEntityTypeMatcher = Predicates.alwaysTrue();
        public Predicate outEntityTypeMatcher = Predicates.alwaysTrue();
        public Predicate successStatusMatcher = Predicates.alwaysTrue();
        public Predicate> errorResponsesMatcher = Predicates.alwaysTrue();
        public Predicate> annotationsMatcher = Predicates.alwaysTrue();

        public Matcher withHttpMethodMatcher(Predicate httpMethodMatcher) {
            this.httpMethodMatcher = httpMethodMatcher;
            return this;
        }

        public Matcher withInEntityTypeMatcher(Predicate inEntityTypeMatcher) {
            this.inEntityTypeMatcher = inEntityTypeMatcher;
            return this;
        }

        public Matcher withOutEntityTypeMatcher(Predicate outEntityTypeMatcher) {
            this.outEntityTypeMatcher = outEntityTypeMatcher;
            return this;
        }

        public Matcher withSuccessStatusMatcher(Predicate successStatusMatcher) {
            this.successStatusMatcher = successStatusMatcher;
            return this;
        }

        public Matcher withErrorResponsesMatcher(Predicate> errorResponsesMatcher) {
            this.errorResponsesMatcher = errorResponsesMatcher;
            return this;
        }

        public Matcher withAnnotationsMatcher(Predicate> annotationsMatcher) {
            this.annotationsMatcher = annotationsMatcher;
            return this;
        }

        public Matcher havingAnyAnnotations(final Class... annotationTypes) {
            return this.withAnnotationsMatcher(new Predicate>() {
                @Override
                public boolean apply(ImmutableList annotations) {
                    if(annotations == null) {
                        return false;
                    }
                    return !FluentIterable.from(annotations)
                            .transform(MoreAnnotations.EXTRACT_ANNOTATION_TYPE)
                            .filter(Predicates.in(Arrays.asList(annotationTypes)))
                            .isEmpty();
                }
            });
        }

        public Matcher havingAllAnnotations(final Class... annotationTypes) {
            return this.withAnnotationsMatcher(new Predicate>() {
                @Override
                public boolean apply(ImmutableList annotations) {
                    if(annotations == null) {
                        return false;
                    }
                    return FluentIterable.from(annotations)
                            .transform(MoreAnnotations.EXTRACT_ANNOTATION_TYPE)
                            .filter(Predicates.in(Arrays.asList(annotationTypes)))
                            .size() == annotationTypes.length;
                }
            });
        }

        @Override
        public boolean apply(OperationDescription description) {
            return httpMethodMatcher.apply(description.httpMethod)
                && inEntityTypeMatcher.apply(description.inEntityType)
                && outEntityTypeMatcher.apply(description.outEntityType)
                && successStatusMatcher.apply(description.successStatus)
                && errorResponsesMatcher.apply(ImmutableList.copyOf(description.errorResponses))
                && annotationsMatcher.apply(description.annotations);
        }
    }

    public Optional findBodyParameter() {
        for (OperationParameterDescription parameter : parameters) {
            if (parameter.paramType == OperationParameterDescription.ParamType.body) {
                return Optional.of(parameter);
            }
        }

        return Optional.absent();
    }

    public  Optional findAnnotation(final Class clazz) {
        for(Annotation annotation: annotations) {
            if(clazz.isAssignableFrom(annotation.getClass())) {
                return Optional.fromNullable((T) annotation);
            }
        }
        return Optional.absent();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy