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