001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.commons.svl;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023import org.apache.juneau.commons.svl.vars.*;
024
025/**
026 * Simple list of variables that can consist of either variable classes or instances.
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/MarshallSimpleVariableLanguage">Marshall Simple Variable Language</a>
030 * </ul>
031 *
032 * @serial exclude
033 */
034public class VarList extends ArrayList<Object> {
035
036   private static final long serialVersionUID = 1L;
037
038   /**
039    * Returns an empty list of variables.
040    *
041    * @return A new empty list of variables.
042    */
043   public static VarList create() {
044      return new VarList();
045   }
046
047   /**
048    * Creates a new list of variables.
049    *
050    * @param vars The variables to create.
051    * @return A new list of variables.
052    */
053   @SafeVarargs
054   public static final VarList of(Class<? extends Var>...vars) {
055      return create().append(vars);
056   }
057
058   /**
059    * Creates a new list of variables.
060    *
061    * @param vars The variables to create.
062    * @return A new list of variables.
063    */
064   public static VarList of(Var...vars) {
065      return create().append(vars);
066   }
067
068   /**
069    * Constructor.
070    */
071   protected VarList() {}
072
073   /**
074    * Copy constructor.
075    *
076    * @param copyFrom The list to copy.  Must not be <jk>null</jk>.
077    */
078   protected VarList(VarList copyFrom) {
079      super(copyFrom);
080   }
081
082   /**
083    * Adds the default variables to this list.
084    *
085    * <p>
086    * The default variables are:
087    * <ul>
088    *    <li>{@link PropertyVar}
089    *    <li>{@link SystemPropertiesVar}
090    *    <li>{@link EnvVariablesVar}
091    *    <li>{@link EnvFileVar}
092    *    <li>{@link DotenvVar}
093    *    <li>{@link ArgsVar}
094    *    <li>{@link ManifestFileVar}
095    * </ul>
096    *
097    * <p>
098    * The 11 transformation/conditional {@code Var}s that previously lived here
099    * ({@code SwitchVar}, {@code IfVar}, {@code CoalesceVar}, {@code PatternMatchVar},
100    * {@code PatternReplaceVar}, {@code PatternExtractVar}, {@code UpperCaseVar},
101    * {@code LowerCaseVar}, {@code NotEmptyVar}, {@code LenVar}, {@code SubstringVar}) were
102    * deleted in 10.0.0 and replaced by the {@code #{...}} script syntax. The
103    * built-in {@code VarFunction} catalog is registered separately via
104    * {@link VarResolver.Builder#defaultFunctions()}.
105    *
106    * @return This object.
107    */
108   public VarList addDefault() {
109      // @formatter:off
110      return append(
111         PropertyVar.class,
112         SystemPropertiesVar.class,
113         EnvVariablesVar.class,
114         EnvFileVar.class,
115         DotenvVar.class,
116         ManifestFileVar.class,
117         ArgsVar.class
118      );
119      // @formatter:on
120   }
121
122   /**
123    * Adds a list of variables to this list.
124    *
125    * @param vars The variables to append to this list.
126    * @return This object.
127    */
128   @SafeVarargs
129   public final VarList append(Class<? extends Var>...vars) {
130      addAll(l(vars));
131      return this;
132   }
133
134   /**
135    * Adds a list of variables to this list.
136    *
137    * @param vars The variables to append to this list.
138    * @return This object.
139    */
140   public VarList append(Var...vars) {
141      addAll(l(vars));
142      return this;
143   }
144
145   /**
146    * Adds a list of variables to this list.
147    *
148    * @param vars The variables to append to this list.  Must not be <jk>null</jk>.
149    * @return This object.
150    */
151   public VarList append(VarList vars) {
152      addAll(vars);
153      return this;
154   }
155
156   /**
157    * Makes a copy of this list.
158    *
159    * @return A new copy of this list.
160    */
161   public VarList copy() {
162      return new VarList(this);
163   }
164}