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.encoders;
014
015import static org.apache.juneau.internal.CollectionUtils.*;
016
017import java.util.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.internal.*;
021
022/**
023 * Builder class for creating instances of {@link EncoderGroup}.
024 */
025public class EncoderGroupBuilder {
026
027   private final List<Encoder> encoders;
028   private BeanContext beanContext = BeanContext.DEFAULT;
029
030   /**
031    * Create an empty encoder group builder.
032    */
033   public EncoderGroupBuilder() {
034      this.encoders = new ArrayList<>();
035   }
036
037   /**
038    * Clone an existing encoder group builder.
039    *
040    * @param copyFrom The encoder group that we're copying settings and encoders from.
041    */
042   public EncoderGroupBuilder(EncoderGroup copyFrom) {
043      this.encoders = new ArrayList<>();
044      addReverse(encoders, copyFrom.getEncoders());
045   }
046
047   /**
048    * Registers the specified encoders with this group.
049    *
050    * @param e The encoders to append to this group.
051    * @return This object (for method chaining).
052    */
053   public EncoderGroupBuilder append(Class<?>...e) {
054      for (int i = e.length-1; i >= 0; i--)
055         encoders.add(beanContext.newInstance(Encoder.class, e[i]));
056      return this;
057   }
058
059   /**
060    * Registers the specified encoders with this group.
061    *
062    * @param e The encoders to append to this group.
063    * @return This object (for method chaining).
064    */
065   public EncoderGroupBuilder append(Encoder...e) {
066      addReverse(encoders, e);
067      return this;
068   }
069
070   /**
071    * Registers the specified encoders with this group.
072    *
073    * @param e The encoders to append to this group.
074    * @return This object (for method chaining).
075    */
076   public EncoderGroupBuilder append(List<Encoder> e) {
077      addReverse(encoders, e);
078      return this;
079   }
080
081   /**
082    * Registers the encoders in the specified group with this group.
083    *
084    * @param eg The encoders to append to this group.
085    * @return This object (for method chaining).
086    */
087   public EncoderGroupBuilder append(EncoderGroup eg) {
088      append(eg.getEncoders());
089      return this;
090   }
091
092   /**
093    * Creates a new {@link EncoderGroup} object using a snapshot of the settings defined in this builder.
094    *
095    * <p>
096    * This method can be called multiple times to produce multiple encoder groups.
097    *
098    * @return A new {@link EncoderGroup} object.
099    */
100   public EncoderGroup build() {
101      List<Encoder> l = new ArrayList<>();
102      for (Object e : encoders)
103         l.add(beanContext.newInstance(Encoder.class, e));
104      return new EncoderGroup(ArrayUtils.toReverseArray(Encoder.class, l));
105   }
106}