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.rest.server.arg;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021
022import java.util.*;
023
024import org.apache.juneau.commons.inject.*;
025
026/**
027 * A list of {@link RestOpArg} classes.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestOpAnnotatedMethods">@RestOp-Annotated Method</a>
031 * </ul>
032 */
033public class RestOpArgList {
034
035   /**
036    * Builder class.
037    */
038   public static class Builder {
039
040      private final BeanStore beanStore;
041      List<Class<? extends RestOpArg>> entries;
042
043      /**
044       * Constructor.
045       *
046       * @param beanStore The bean store to use for creating beans.
047       */
048      protected Builder(BeanStore beanStore) {
049         this.beanStore = beanStore;
050         entries = list();
051      }
052
053      /**
054       * Returns the bean store used by this builder.
055       *
056       * @return The bean store used by this builder.
057       */
058      public BeanStore beanStore() {
059         return beanStore;
060      }
061
062      /**
063       * Prepends the specified rest op arg classes to the list.
064       *
065       * @param values The values to add. Must not be <jk>null</jk>.
066       * @return This object.
067       * @throws IllegalArgumentException if any class does not extend from {@link RestOpArg}.
068       */
069      public Builder add(Class<?>...values) {
070         prependAll(entries, reqcat("values", RestOpArg.class, values));
071         return this;
072      }
073
074      /**
075       * Builds the list.
076       *
077       * @return A new {@link RestOpArgList}.
078       */
079      public RestOpArgList build() {
080         return new RestOpArgList(this);
081      }
082   }
083
084   /**
085    * Static creator.
086    *
087    * @param beanStore The bean store to use for creating beans.
088    * @return A new builder for this object.
089    */
090   public static Builder create(BeanStore beanStore) {
091      return new Builder(beanStore);
092   }
093
094   private final Class<? extends RestOpArg>[] entries;
095
096   /**
097    * Constructor.
098    *
099    * @param builder The builder containing the contents for this list. Must not be <jk>null</jk>.
100    */
101   @SuppressWarnings({
102      "unchecked" // Type erasure requires cast for builder list
103   })
104   protected RestOpArgList(Builder builder) {
105      entries = builder.entries.stream().toArray(Class[]::new);
106   }
107
108   /**
109    * Returns the contents of this list as a {@link Class} array.
110    *
111    * @return The contents of this list as a {@link Class} array.
112    */
113   public Class<? extends RestOpArg>[] asArray() {
114      return entries;
115   }
116}