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.guard;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020
021import java.util.*;
022
023import org.apache.juneau.commons.inject.*;
024
025/**
026 * A list of {@link RestGuard} objects.
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Guards">Guards</a>
030 * </ul>
031 */
032public class RestGuardList {
033
034   /**
035    * Builder class.
036    */
037   public static class Builder {
038
039      private final BeanStore beanStore;
040      List<BeanInstantiator.Builder<RestGuard>> entries;
041
042      /**
043       * Constructor.
044       *
045       * @param beanStore The bean store to use for creating beans.
046       */
047      protected Builder(BeanStore beanStore) {
048         this.beanStore = beanStore;
049         entries = list();
050      }
051
052      /**
053       * Returns the bean store used by this builder.
054       *
055       * @return The bean store used by this builder.
056       */
057      public BeanStore beanStore() {
058         return beanStore;
059      }
060
061      /**
062       * Appends the specified rest guard classes to the list.
063       *
064       * @param values The values to add.
065       *    <br>Must not be <jk>null</jk>.
066       * @return This object.
067       */
068      @SuppressWarnings({
069         "unchecked" // Varargs with generics require unchecked cast
070      })
071      public Builder append(Class<? extends RestGuard>...values) {
072         for (var v : values)
073            entries.add(BeanInstantiator.of(RestGuard.class, beanStore).type(v));
074         return this;
075      }
076
077      /**
078       * Appends the specified rest guard objects to the list.
079       *
080       * @param values The values to add.
081       *    <br>Must not be <jk>null</jk>.
082       * @return This object.
083       */
084      public Builder append(RestGuard...values) {
085         for (var v : values)
086            entries.add(BeanInstantiator.of(RestGuard.class, beanStore).impl(v));
087         return this;
088      }
089
090      /**
091       * Builds the list.
092       *
093       * @return A new {@link RestGuardList}.
094       */
095      public RestGuardList build() {
096         return new RestGuardList(this);
097      }
098   }
099
100   /**
101    * Static creator.
102    *
103    * @param beanStore The bean store to use for creating beans.
104    * @return A new builder for this object.
105    */
106   public static Builder create(BeanStore beanStore) {
107      return new Builder(beanStore);
108   }
109
110   private RestGuard[] entries;
111
112   /**
113    * Constructor.
114    *
115    * @param builder The builder containing the contents for this list.
116    *    <br>Must not be <jk>null</jk>.
117    */
118   protected RestGuardList(Builder builder) {
119      entries = builder.entries.stream().map(BeanInstantiator.Builder::run).toArray(RestGuard[]::new);
120   }
121
122   /**
123    * Returns the entries in this list.
124    *
125    * @return The entries in this list.
126    */
127   public RestGuard[] asArray() {
128      return entries;
129   }
130}