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.internal.StringUtils.*;
016
017import java.util.*;
018
019import org.apache.juneau.internal.*;
020import org.apache.juneau.utils.*;
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><code>GET</code>
029 *    <li><code>PUT</code>
030 *    <li><code>POST</code>
031 *    <li><code>DELETE</code>
032 *    <li><code>OPTIONS</code>
033 *    <li><code>HEAD</code>
034 *    <li><code>PATCH</code>
035 *    <li>Everything else.
036 * </ul>
037 */
038public class OperationMap extends TreeMap<String,Operation> {
039   private static final long serialVersionUID = 1L;
040
041   private static final Comparator<String> OP_SORTER = new Comparator<String>() {
042      private final Map<String,String> methods = new AMap<String,String>()
043         .append("get","0")
044         .append("put","1")
045         .append("post","2")
046         .append("delete","3")
047         .append("options","4")
048         .append("head","5")
049         .append("patch","6");
050
051      @Override
052      public int compare(String o1, String o2) {
053         String s1 = methods.get(o1);
054         String s2 = methods.get(o2);
055         if (s1 == null)
056            s1 = o1;
057         if (s2 == null)
058            s2 = o2;
059         return StringUtils.compare(s1, s2);
060      }
061   };
062
063   /**
064    * Constructor.
065    */
066   public OperationMap() {
067      super(OP_SORTER);
068   }
069
070   /**
071    * Fluent-style put method.
072    *
073    * @param httpMethodName The HTTP method name.
074    * @param operation The operation.
075    * @return This method (for method chaining).
076    */
077   public OperationMap append(String httpMethodName, Operation operation) {
078      put(emptyIfNull(httpMethodName).toLowerCase(), operation);
079      return this;
080   }
081}