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.utils;
014
015import java.util.*;
016
017/**
018 * An extension of {@link LinkedHashMap} with a convenience {@link #append(Object,Object)} method.
019 *
020 * <p>
021 * Primarily used for testing purposes for quickly creating populated maps.
022 * <p class='bcode w800'>
023 *    <jc>// Example:</jc>
024 *    Map&lt;String,Integer&gt; m = <jk>new</jk> AMap&lt;String,Integer&gt;()
025 *       .append(<js>"foo"</js>,1).append(<js>"bar"</js>,2);
026 * </p>
027 *
028 * @param <K> The key type.
029 * @param <V> The value type.
030 */
031public final class AMap<K,V> extends LinkedHashMap<K,V> {
032
033   // TODO - Change to 1L in 8.0
034   private static final long serialVersionUID = 7344180601810042208L;
035
036   /**
037    * Creates an empty map.
038    *
039    * @return A new empty map.
040    */
041   public static <K,V> AMap<K,V> create() {
042      return new AMap<>();
043   }
044
045   /**
046    * Creates a map with one entry.
047    *
048    * @param key Entry key.
049    * @param value Entry value.
050    * @return A new map with one entry.
051    */
052   public static <K,V> AMap<K,V> create(K key, V value) {
053      return new AMap<K,V>().append(key, value);
054   }
055
056   /**
057    * Adds an entry to this map.
058    *
059    * @param k The key.
060    * @param v The value.
061    * @return This object (for method chaining).
062    */
063   public AMap<K,V> append(K k, V v) {
064      put(k, v);
065      return this;
066   }
067}