<commit_before>/*
 *
 *  Copyright 2015 the original author or authors.
 *
 *  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 springfox.documentation.spi.service.contexts;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Optional;
import com.google.common.collect.Ordering;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UriComponentsBuilder;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.ClassSupport;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.ApiListingReference;
import springfox.documentation.service.Operation;
import springfox.documentation.service.ResponseMessage;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Lists.*;
import static com.google.common.collect.Maps.*;
import static com.google.common.collect.Sets.*;
import static java.util.Arrays.asList;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import static springfox.documentation.schema.AlternateTypeRules.*;

public class Defaults {

  private HashSet<Class> ignored;
  private LinkedHashMap<RequestMethod, List<ResponseMessage>> responses;
  private List<Class<? extends Annotation>> annotations;
  private Ordering<Operation> operationOrdering;
  private Ordering<ApiDescription> apiDescriptionOrdering;
  private Ordering<ApiListingReference> apiListingReferenceOrdering;

  public Defaults() {
    init();
  }

  public Set<Class> defaultIgnorableParameterTypes() {
    return ignored;
  }

  /**
   * Default response messages set on all api operations
   */
  public Map<RequestMethod, List<ResponseMessage>> defaultResponseMessages() {
    return responses;
  }

  public List<Class<? extends Annotation>> defaultExcludeAnnotations() {
    return annotations;
  }

  public Ordering<Operation> operationOrdering() {
    return operationOrdering;
  }


  public Ordering<ApiDescription> apiDescriptionOrdering() {
    return apiDescriptionOrdering;
  }

  public Ordering<ApiListingReference> apiListingReferenceOrdering() {
    return apiListingReferenceOrdering;
  }

  public List<AlternateTypeRule> defaultRules(TypeResolver typeResolver) {
    List<AlternateTypeRule> rules = newArrayList();
    rules.add(newRule(typeResolver.resolve(Map.class), typeResolver.resolve(Object.class)));
    rules.add(newRule(typeResolver.resolve(Map.class, String.class, Object.class),
            typeResolver.resolve(Object.class)));
    rules.add(newRule(typeResolver.resolve(Map.class, Object.class, Object.class),
            typeResolver.resolve(Object.class)));


    rules.add(newRule(typeResolver.resolve(ResponseEntity.class, WildcardType.class),
            typeResolver.resolve(WildcardType.class)));

    rules.add(newRule(typeResolver.resolve(HttpEntity.class, WildcardType.class),
            typeResolver.resolve(WildcardType.class)));
    rules.add(newRule(typeResolver.resolve(Optional.class, WildcardType.class), WildcardType.class));
    maybeAddRuleForClassName(typeResolver, rules, "java.util.Optional", WildcardType.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.LocalDate", java.sql.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.LocalDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.Instant", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.OffsetDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.ZonedDateTime", java.util.Date.class);
    return rules;
  }

  void maybeAddRuleForClassName(TypeResolver typeResolver, List<AlternateTypeRule> rules, String className, Class clazz ) {
    Optional<? extends Class> fromClazz = ClassSupport.classByName(className);
    if (fromClazz.isPresent()) {
      rules.add(newRule(
          typeResolver.resolve(fromClazz.get()),
          typeResolver.resolve(clazz)));
    }
  }

  private void init() {
    initIgnorableTypes();
    initResponseMessages();
    initExcludeAnnotations();
    initOrderings();
  }

  private void initOrderings() {
    operationOrdering = Ordering.from(Orderings.positionComparator()).compound(Orderings.nickNameComparator());
    apiDescriptionOrdering = Ordering.from(Orderings.apiPathCompatator());
    apiListingReferenceOrdering = Ordering.from(Orderings.listingPositionComparator()).compound(Orderings.listingReferencePathComparator());
  }<commit_msg>add native support for joda and threetenbp date types<commit_after>/*
 *
 *  Copyright 2015 the original author or authors.
 *
 *  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 springfox.documentation.spi.service.contexts;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Optional;
import com.google.common.collect.Ordering;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UriComponentsBuilder;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.ClassSupport;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.ApiListingReference;
import springfox.documentation.service.Operation;
import springfox.documentation.service.ResponseMessage;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Lists.*;
import static com.google.common.collect.Maps.*;
import static com.google.common.collect.Sets.*;
import static java.util.Arrays.asList;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import static springfox.documentation.schema.AlternateTypeRules.*;

public class Defaults {

  private HashSet<Class> ignored;
  private LinkedHashMap<RequestMethod, List<ResponseMessage>> responses;
  private List<Class<? extends Annotation>> annotations;
  private Ordering<Operation> operationOrdering;
  private Ordering<ApiDescription> apiDescriptionOrdering;
  private Ordering<ApiListingReference> apiListingReferenceOrdering;

  public Defaults() {
    init();
  }

  public Set<Class> defaultIgnorableParameterTypes() {
    return ignored;
  }

  /**
   * Default response messages set on all api operations
   */
  public Map<RequestMethod, List<ResponseMessage>> defaultResponseMessages() {
    return responses;
  }

  public List<Class<? extends Annotation>> defaultExcludeAnnotations() {
    return annotations;
  }

  public Ordering<Operation> operationOrdering() {
    return operationOrdering;
  }


  public Ordering<ApiDescription> apiDescriptionOrdering() {
    return apiDescriptionOrdering;
  }

  public Ordering<ApiListingReference> apiListingReferenceOrdering() {
    return apiListingReferenceOrdering;
  }

  public List<AlternateTypeRule> defaultRules(TypeResolver typeResolver) {
    List<AlternateTypeRule> rules = newArrayList();
    rules.add(newRule(typeResolver.resolve(Map.class), typeResolver.resolve(Object.class)));
    rules.add(newRule(typeResolver.resolve(Map.class, String.class, Object.class),
            typeResolver.resolve(Object.class)));
    rules.add(newRule(typeResolver.resolve(Map.class, Object.class, Object.class),
            typeResolver.resolve(Object.class)));


    rules.add(newRule(typeResolver.resolve(ResponseEntity.class, WildcardType.class),
            typeResolver.resolve(WildcardType.class)));

    rules.add(newRule(typeResolver.resolve(HttpEntity.class, WildcardType.class),
            typeResolver.resolve(WildcardType.class)));
    rules.add(newRule(typeResolver.resolve(Optional.class, WildcardType.class), WildcardType.class));

    maybeAddRuleForClassName(typeResolver, rules, "java.util.Optional", WildcardType.class);

    maybeAddRuleForClassName(typeResolver, rules, "java.time.LocalDate", java.sql.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.LocalDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.Instant", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.OffsetDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "java.time.ZonedDateTime", java.util.Date.class);

    maybeAddRuleForClassName(typeResolver, rules, "org.threeten.bp.LocalDate", java.sql.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.threeten.bp.LocalDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.threeten.bp.Instant", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.threeten.bp.OffsetDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.threeten.bp.ZonedDateTime", java.util.Date.class);

    maybeAddRuleForClassName(typeResolver, rules, "org.joda.time.LocalDate", java.sql.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.joda.time.LocalDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.joda.time.Instant", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.joda.time.DateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.joda.time.ReadableDateTime", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.joda.time.ReadableInstant", java.util.Date.class);
    maybeAddRuleForClassName(typeResolver, rules, "org.joda.time.DateMidnight", java.util.Date.class);

    return rules;
  }

  void maybeAddRuleForClassName(TypeResolver typeResolver, List<AlternateTypeRule> rules, String className, Class clazz ) {
    Optional<? extends Class> fromClazz = ClassSupport.classByName(className);
    if (fromClazz.isPresent()) {
      rules.add(newRule(
          typeResolver.resolve(fromClazz.get()),
          typeResolver.resolve(clazz)));
    }
  }

  private void init() {
    initIgnorableTypes();
    initResponseMessages();
    initExcludeAnnotations();
    initOrderings();
  }

  private void initOrderings() {
    operationOrdering = Ordering.from(Orderings.positionComparator()).compound(Orderings.nickNameComparator());
    apiDescriptionOrdering = Ordering.from(Orderings.apiPathCompatator());
    apiListingReferenceOrdering = Ordering.from(Orderings.listingPositionComparator()).compound(Orderings.listingReferencePathComparator());
  }
{'fraction_non_alphanumeric': 0.1034190013120321, 'fraction_numerical': 0.0012348537470093387, 'mean_word_length': 5.472527472527473, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3960868', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3504, 'n_words': 704}