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