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