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.camel.component.dataset;
018
019import java.util.LinkedList;
020import java.util.List;
021
022/**
023 * A DataSet that allows a list of static payloads to be used to create each message exchange
024 * along with using a pluggable transformer to customize the messages.
025 *
026 * @version
027 */
028public class ListDataSet extends DataSetSupport {
029    private List<Object> defaultBodies;
030
031    public ListDataSet() {
032        super(0);
033    }
034
035    public ListDataSet(List<Object> defaultBodies) {
036        this.defaultBodies = defaultBodies;
037        setSize(defaultBodies.size());
038    }
039
040    // Properties
041    //-------------------------------------------------------------------------
042
043    public List<Object> getDefaultBodies() {
044        if (defaultBodies == null) {
045            defaultBodies = new LinkedList<>();
046        }
047
048        return defaultBodies;
049    }
050
051    public void setDefaultBodies(List<Object> defaultBodies) {
052        this.defaultBodies = defaultBodies;
053        setSize(defaultBodies.size());
054    }
055
056    // Implementation methods
057    //-------------------------------------------------------------------------
058
059    /**
060     * Creates the message body for a given message.  If the messageIndex is greater than the size
061     * of the list, use the modulus.
062     */
063    protected Object createMessageBody(long messageIndex) {
064        int listIndex = (int) (messageIndex % getDefaultBodies().size());
065
066        return getDefaultBodies().get(listIndex);
067    }
068}