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.collections.*; 020import org.apache.juneau.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 */ 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 = AMap.of("get","0","put","1","post","2","delete","3","options","4","head","5","patch","6"); 043 044 @Override 045 public int compare(String o1, String o2) { 046 String s1 = methods.get(o1); 047 String s2 = methods.get(o2); 048 if (s1 == null) 049 s1 = o1; 050 if (s2 == null) 051 s2 = o2; 052 return StringUtils.compare(s1, s2); 053 } 054 }; 055 056 /** 057 * Constructor. 058 */ 059 public OperationMap() { 060 super(OP_SORTER); 061 } 062 063 /** 064 * Fluent-style put method. 065 * 066 * @param httpMethodName The HTTP method name. 067 * @param operation The operation. 068 * @return This method (for method chaining). 069 */ 070 public OperationMap append(String httpMethodName, Operation operation) { 071 put(emptyIfNull(httpMethodName).toLowerCase(), operation); 072 return this; 073 } 074}