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.commons.function; 018 019import java.util.*; 020 021/** 022 * A simple in-memory {@link BeanChannel} implementation backed by an {@link ArrayList}. 023 * 024 * <p> 025 * Collects parsed elements into an in-memory list on the parser side, and iterates over the list 026 * on the serializer side. No factory or DI framework required — instantiated directly via no-arg 027 * constructor. 028 * 029 * <p> 030 * This is the default built-in implementation suitable for use cases where holding all elements 031 * in memory is acceptable. For large datasets or database-backed scenarios, implement 032 * {@link BeanChannel} (or {@link BeanConsumer} / {@link BeanSupplier}) directly. 033 * 034 * <h5 class='section'>Example (getter-only round-trip property):</h5> 035 * <p class='bjava'> 036 * <jk>public class</jk> ItemCollection { 037 * <jk>private final</jk> ListBeanChannel<Item> <jv>items</jv> = <jk>new</jk> ListBeanChannel<>(); 038 * 039 * <ja>@BeanProp</ja>(elementType=Item.<jk>class</jk>) 040 * <jk>public</jk> ListBeanChannel<Item> getItems() { <jk>return</jk> <jv>items</jv>; } 041 * <jc>// No setter needed — parser calls acceptThrows() on the existing instance</jc> 042 * } 043 * </p> 044 * 045 * <h5 class='section'>See Also:</h5><ul> 046 * <li class='jc'>{@link BeanChannel} - Round-trip lifecycle interface 047 * <li class='jc'>{@link BeanConsumer} - Parse-only lifecycle interface 048 * <li class='jc'>{@link BeanSupplier} - Serialize-only lifecycle interface 049 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Marshalling">Marshalling</a> 050 * </ul> 051 * 052 * @param <T> The type of bean stored in this channel. 053 */ 054public class ListBeanChannel<T> implements BeanChannel<T> { 055 056 private final List<T> list = new ArrayList<>(); 057 058 /** 059 * Constructor. 060 */ 061 public ListBeanChannel() { /* intentionally empty */ } 062 063 @Override 064 public void acceptThrows(T item) { 065 list.add(item); 066 } 067 068 @Override 069 public Iterator<T> iterator() { 070 return list.iterator(); 071 } 072 073 /** 074 * Returns the underlying list of elements. 075 * 076 * @return The list of elements collected during parsing, or to be serialized. 077 */ 078 public List<T> getList() { 079 return list; 080 } 081}