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.converter;
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 RestConverter} objects.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Converters">Converters</a>
031 * </ul>
032 */
033public class RestConverterList {
034   /**
035    * Builder class.
036    */
037   public static class Builder extends BeanBuilder<RestConverterList> {
038
039      List<BeanCreator<RestConverter>> entries;
040
041      /**
042       * Create an empty builder.
043       *
044       * @param beanStore The bean store to use for creating beans.
045       */
046      protected Builder(BeanStore beanStore) {
047         super(RestConverterList.class, beanStore);
048         this.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 RestConverter>...values) {
059         for (var v : values)
060            entries.add(beanStore().createBean(RestConverter.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(RestConverter...values) {
071         for (var v : values)
072            entries.add(beanStore().createBean(RestConverter.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 RestConverterList buildDefault() {
090         return new RestConverterList(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 final RestConverter[] entries;
105
106   /**
107    * Constructor.
108    *
109    * @param builder The builder containing the contents for this list.
110    */
111   protected RestConverterList(Builder builder) {
112      // @formatter:off
113      entries =
114         builder
115            .entries
116            .stream()
117            .map(BeanCreator::run)
118            .toArray(RestConverter[]::new);
119      // @formatter:on
120   }
121
122   /**
123    * Returns the contents of this list as a {@link RestConverter} array.
124    *
125    * @return The contents of this list as a {@link RestConverter} array.
126    */
127   public RestConverter[] asArray() {
128      return entries;
129   }
130}