001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.matcher;
014
015import static java.util.stream.Collectors.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.cp.*;
022import org.apache.juneau.internal.*;
023
024/**
025 * A list of {@link RestMatcher} objects.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.RestOpAnnotatedMethods">@RestOp-Annotated Methods</a>
029 * </ul>
030 */
031public class RestMatcherList {
032
033   //-----------------------------------------------------------------------------------------------------------------
034   // Static
035   //-----------------------------------------------------------------------------------------------------------------
036
037   /**
038    * Static creator.
039    *
040    * @param beanStore The bean store to use for creating beans.
041    * @return A new builder for this object.
042    */
043   public static Builder create(BeanStore beanStore) {
044      return new Builder(beanStore);
045   }
046
047   //-----------------------------------------------------------------------------------------------------------------
048   // Builder
049   //-----------------------------------------------------------------------------------------------------------------
050
051   /**
052    * Builder class.
053    */
054   @FluentSetters
055   public static class Builder extends BeanBuilder<RestMatcherList> {
056
057      List<BeanCreator<RestMatcher>> entries;
058
059      /**
060       * Constructor.
061       *
062       * @param beanStore The bean store to use for creating beans.
063       */
064      protected Builder(BeanStore beanStore) {
065         super(RestMatcherList.class, beanStore);
066         entries = list();
067      }
068
069      @Override /* BeanBuilder */
070      protected RestMatcherList buildDefault() {
071         return new RestMatcherList(this);
072      }
073
074      //-------------------------------------------------------------------------------------------------------------
075      // Properties
076      //-------------------------------------------------------------------------------------------------------------
077
078      /**
079       * Appends the specified rest matcher classes to the list.
080       *
081       * @param values The values to add.
082       * @return This object.
083       */
084      @SuppressWarnings("unchecked")
085      public Builder append(Class<? extends RestMatcher>...values) {
086         for (Class<? extends RestMatcher> v : values)
087            entries.add(beanStore().createBean(RestMatcher.class).type(v));
088         return this;
089      }
090
091      /**
092       * Appends the specified rest matcher objects to the list.
093       *
094       * @param values The values to add.
095       * @return This object.
096       */
097      public Builder append(RestMatcher...values) {
098         for (RestMatcher v : values)
099            entries.add(beanStore().createBean(RestMatcher.class).impl(v));
100         return this;
101      }
102
103      // <FluentSetters>
104
105      @Override /* GENERATED - org.apache.juneau.BeanBuilder */
106      public Builder impl(Object value) {
107         super.impl(value);
108         return this;
109      }
110
111      @Override /* GENERATED - org.apache.juneau.BeanBuilder */
112      public Builder type(Class<?> value) {
113         super.type(value);
114         return this;
115      }
116
117      // </FluentSetters>
118   }
119
120   //-----------------------------------------------------------------------------------------------------------------
121   // Instance
122   //-----------------------------------------------------------------------------------------------------------------
123
124   private final RestMatcher[] optionalEntries;
125   private final RestMatcher[] requiredEntries;
126
127   /**
128    * Constructor.
129    *
130    * @param builder The builder containing the contents for this list.
131    */
132   protected RestMatcherList(Builder builder) {
133      List<RestMatcher> l =
134         builder
135            .entries
136            .stream()
137            .map(BeanCreator::run)
138            .collect(toList());
139      optionalEntries = l.stream().filter(x -> ! x.required()).toArray(RestMatcher[]::new);
140      requiredEntries = l.stream().filter(RestMatcher::required).toArray(RestMatcher[]::new);
141   }
142
143   /**
144    * Returns the entries in this list that are specified as optional.
145    *
146    * @return An unmodifiable list of entries in this list that are specified as optional.
147    */
148   public RestMatcher[] getOptionalEntries() {
149      return optionalEntries;
150   }
151
152   /**
153    * Returns the entries in this list that are specified as required.
154    *
155    * @return An unmodifiable list of entries in this list that are specified as required.
156    */
157   public RestMatcher[] getRequiredEntries() {
158      return requiredEntries;
159   }
160}