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