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.server.converter; 018 019import static org.apache.juneau.commons.utils.CollectionUtils.*; 020 021import java.util.*; 022 023import org.apache.juneau.commons.inject.*; 024 025/** 026 * A list of {@link RestConverter} objects. 027 * 028 * <h5 class='section'>See Also:</h5><ul> 029 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Converters">Converters</a> 030 * </ul> 031 */ 032public class RestConverterList { 033 034 /** 035 * Builder class. 036 */ 037 public static class Builder { 038 039 private final BeanStore beanStore; 040 List<BeanInstantiator.Builder<RestConverter>> entries; 041 042 /** 043 * Create an empty builder. 044 * 045 * @param beanStore The bean store to use for creating beans. 046 */ 047 protected Builder(BeanStore beanStore) { 048 this.beanStore = beanStore; 049 this.entries = list(); 050 } 051 052 /** 053 * Returns the bean store used by this builder. 054 * 055 * @return The bean store used by this builder. 056 */ 057 public BeanStore beanStore() { 058 return beanStore; 059 } 060 061 /** 062 * Appends the specified rest converter classes to the list. 063 * 064 * @param values The values to add. 065 * <br>Must not be <jk>null</jk>. 066 * @return This object. 067 */ 068 @SuppressWarnings({ 069 "unchecked" // Varargs with generics require unchecked cast 070 }) 071 public Builder append(Class<? extends RestConverter>...values) { 072 for (var v : values) 073 entries.add(BeanInstantiator.of(RestConverter.class, beanStore).type(v)); 074 return this; 075 } 076 077 /** 078 * Appends the specified rest converter objects to the list. 079 * 080 * @param values The values to add. 081 * <br>Must not be <jk>null</jk>. 082 * @return This object. 083 */ 084 public Builder append(RestConverter...values) { 085 for (var v : values) 086 entries.add(BeanInstantiator.of(RestConverter.class, beanStore).impl(v)); 087 return this; 088 } 089 090 /** 091 * Builds the list. 092 * 093 * @return A new {@link RestConverterList}. 094 */ 095 public RestConverterList build() { 096 return new RestConverterList(this); 097 } 098 } 099 100 /** 101 * Static creator. 102 * 103 * @param beanStore The bean store to use for creating beans. 104 * @return A new builder for this object. 105 */ 106 public static Builder create(BeanStore beanStore) { 107 return new Builder(beanStore); 108 } 109 110 private final RestConverter[] entries; 111 112 /** 113 * Constructor. 114 * 115 * @param builder The builder containing the contents for this list. 116 * <br>Must not be <jk>null</jk>. 117 */ 118 protected RestConverterList(Builder builder) { 119 // @formatter:off 120 entries = 121 builder 122 .entries 123 .stream() 124 .map(BeanInstantiator.Builder::run) 125 .toArray(RestConverter[]::new); 126 // @formatter:on 127 } 128 129 /** 130 * Returns the contents of this list as a {@link RestConverter} array. 131 * 132 * @return The contents of this list as a {@link RestConverter} array. 133 */ 134 public RestConverter[] asArray() { 135 return entries; 136 } 137}