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