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 LinkedList} with a convenience {@link #append(Object)} method.
019 *
020 * <p>
021 * Primarily used for testing purposes for quickly creating populated lists.
022 * <p class='bcode w800'>
023 *    <jc>// Example:</jc>
024 *    List&lt;String&gt; l = <jk>new</jk> AList&lt;String&gt;().append(<js>"foo"</js>).append(<js>"bar"</js>);
025 * </p>
026 *
027 * @param <T> The entry type.
028 */
029@SuppressWarnings({"unchecked"})
030public final class AList<T> extends LinkedList<T> {
031
032   // TODO - Change to 1L in 8.0
033   private static final long serialVersionUID = 2165760898071759212L;
034
035   /**
036    * Convenience method for creating a list of objects.
037    *
038    * @param t The initial values.
039    * @return A new list.
040    */
041   @SafeVarargs
042   public static <T> AList<T> create(T...t) {
043      return new AList<T>().appendAll(t);
044   }
045
046   /**
047    * Adds an entry to this list.
048    *
049    * @param t The entry to add to this list.
050    * @return This object (for method chaining).
051    */
052   public AList<T> append(T t) {
053      add(t);
054      return this;
055   }
056
057   /**
058    * Adds multiple entries to this list.
059    *
060    * @param t The entries to add to this list.
061    * @return This object (for method chaining).
062    */
063   public AList<T> appendAll(T...t) {
064      addAll(Arrays.asList(t));
065      return this;
066   }
067
068   /**
069    * Adds an entry to this list if the boolean flag is <jk>true</jk>.
070    *
071    * @param b The boolean flag.
072    * @param val The value to add.
073    * @return This object (for method chaining).
074    */
075   public AList<T> appendIf(boolean b, T val) {
076      if (b)
077         append(val);
078      return this;
079   }
080}