View Javadoc
1   /*
2    * Copyright 2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.apiclient.webflux.contract.spring;
18  
19  import java.util.Comparator;
20  import org.bremersee.apiclient.webflux.InvocationParameter;
21  import org.springframework.web.bind.annotation.RequestBody;
22  import org.springframework.web.bind.annotation.RequestPart;
23  
24  /**
25   * The request body comparator.
26   *
27   * @author Christian Bremer
28   */
29  class RequestBodyComparator implements Comparator<InvocationParameter> {
30  
31    @Override
32    public int compare(InvocationParameter o1, InvocationParameter o2) {
33      boolean a1 = o1.hasParameterAnnotation(RequestBody.class);
34      boolean a2 = o2.hasParameterAnnotation(RequestBody.class);
35      if (a1 && a2) {
36        return 0;
37      }
38      if (a1) {
39        return -1;
40      }
41      if (a2) {
42        return 1;
43      }
44      a1 = o1.hasParameterAnnotation(RequestPart.class);
45      a2 = o2.hasParameterAnnotation(RequestPart.class);
46      if (a1 && a2) {
47        return 0;
48      }
49      if (a1) {
50        return -1;
51      }
52      if (a2) {
53        return 1;
54      }
55      return Integer.compare(o1.getIndex(), o2.getIndex());
56    }
57  }