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