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