001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.dto.swagger;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017
018import java.util.*;
019
020import org.apache.juneau.common.internal.*;
021
022/**
023 * Map meant for method-name/operation mappings.
024 *
025 * <p>
026 * Forces entries to be sorted in the following order:
027 * <ul>
028 *    <li><c>GET</c>
029 *    <li><c>PUT</c>
030 *    <li><c>POST</c>
031 *    <li><c>DELETE</c>
032 *    <li><c>OPTIONS</c>
033 *    <li><c>HEAD</c>
034 *    <li><c>PATCH</c>
035 *    <li>Everything else.
036 * </ul>
037 *
038 * <h5 class='section'>See Also:</h5><ul>
039 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Swagger">Overview &gt; juneau-rest-server &gt; Swagger</a>
040 * </ul>
041 *
042 * @serial exclude
043 */
044public class OperationMap extends TreeMap<String,Operation> {
045   private static final long serialVersionUID = 1L;
046
047   private static final Comparator<String> OP_SORTER = new Comparator<>() {
048      private final Map<String,String> methods = mapBuilder(String.class,String.class)
049         .add("get","0").add("put","1").add("post","2").add("delete","3").add("options","4").add("head","5").add("patch","6")
050         .build();
051
052      @Override
053      public int compare(String o1, String o2) {
054         String s1 = methods.get(o1);
055         String s2 = methods.get(o2);
056         if (s1 == null)
057            s1 = o1;
058         if (s2 == null)
059            s2 = o2;
060         return StringUtils.compare(s1, s2);
061      }
062   };
063
064   /**
065    * Constructor.
066    */
067   public OperationMap() {
068      super(OP_SORTER);
069   }
070
071   /**
072    * Fluent-style put method.
073    *
074    * @param httpMethodName The HTTP method name.
075    * @param operation The operation.
076    * @return This object.
077    */
078   public OperationMap append(String httpMethodName, Operation operation) {
079      put(emptyIfNull(httpMethodName).toLowerCase(), operation);
080      return this;
081   }
082}