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.logging.log4j.message;
018
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023import org.apache.logging.log4j.util.StringBuilderFormattable;
024
025/**
026 * A collection of StructuredDataMessages.
027 */
028public class StructuredDataCollectionMessage implements StringBuilderFormattable,
029        MessageCollectionMessage<StructuredDataMessage> {
030
031    private List<StructuredDataMessage> structuredDataMessageList;
032
033    public StructuredDataCollectionMessage(List<StructuredDataMessage> messages) {
034        this.structuredDataMessageList = messages;
035    }
036
037    @Override
038    public Iterator<StructuredDataMessage> iterator() {
039        return structuredDataMessageList.iterator();
040    }
041
042    @Override
043    public String getFormattedMessage() {
044        StringBuilder sb = new StringBuilder();
045        formatTo(sb);
046        return sb.toString();
047    }
048
049    @Override
050    public String getFormat() {
051        StringBuilder sb = new StringBuilder();
052        for (StructuredDataMessage msg : structuredDataMessageList) {
053            if (msg.getFormat() != null) {
054                if (sb.length() > 0) {
055                    sb.append(", ");
056                }
057                sb.append(msg.getFormat());
058            }
059        }
060        return sb.toString();
061    }
062
063    @Override
064    public void formatTo(StringBuilder buffer) {
065        for (StructuredDataMessage msg : structuredDataMessageList) {
066            msg.formatTo(buffer);
067        }
068    }
069
070    @Override
071    public Object[] getParameters() {
072        List<Object[]> objectList = new ArrayList<>();
073        int count = 0;
074        for (StructuredDataMessage msg : structuredDataMessageList) {
075            Object[] objects = msg.getParameters();
076            if (objects != null) {
077                objectList.add(objects);
078                count += objects.length;
079            }
080        }
081        Object[] objects = new Object[count];
082        int index = 0;
083        for (Object[] objs : objectList) {
084           for (Object obj : objs) {
085               objects[index++] = obj;
086           }
087        }
088        return objects;
089    }
090
091    @Override
092    public Throwable getThrowable() {
093        for (StructuredDataMessage msg : structuredDataMessageList) {
094            Throwable t = msg.getThrowable();
095            if (t != null) {
096                return t;
097            }
098        }
099        return null;
100    }
101}