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    private static final long serialVersionUID = 5725337076388822924L;
031
032    private final List<StructuredDataMessage> structuredDataMessageList;
033
034    public StructuredDataCollectionMessage(final List<StructuredDataMessage> messages) {
035        this.structuredDataMessageList = messages;
036    }
037
038    @Override
039    public Iterator<StructuredDataMessage> iterator() {
040        return structuredDataMessageList.iterator();
041    }
042
043    @Override
044    public String getFormattedMessage() {
045        final StringBuilder sb = new StringBuilder();
046        formatTo(sb);
047        return sb.toString();
048    }
049
050    @Override
051    public String getFormat() {
052        final StringBuilder sb = new StringBuilder();
053        for (final StructuredDataMessage msg : structuredDataMessageList) {
054            if (msg.getFormat() != null) {
055                if (sb.length() > 0) {
056                    sb.append(", ");
057                }
058                sb.append(msg.getFormat());
059            }
060        }
061        return sb.toString();
062    }
063
064    @Override
065    public void formatTo(final StringBuilder buffer) {
066        for (final StructuredDataMessage msg : structuredDataMessageList) {
067            msg.formatTo(buffer);
068        }
069    }
070
071    @Override
072    public Object[] getParameters() {
073        final List<Object[]> objectList = new ArrayList<>();
074        int count = 0;
075        for (final StructuredDataMessage msg : structuredDataMessageList) {
076            final Object[] objects = msg.getParameters();
077            if (objects != null) {
078                objectList.add(objects);
079                count += objects.length;
080            }
081        }
082        final Object[] objects = new Object[count];
083        int index = 0;
084        for (final Object[] objs : objectList) {
085           for (final Object obj : objs) {
086               objects[index++] = obj;
087           }
088        }
089        return objects;
090    }
091
092    @Override
093    public Throwable getThrowable() {
094        for (final StructuredDataMessage msg : structuredDataMessageList) {
095            final Throwable t = msg.getThrowable();
096            if (t != null) {
097                return t;
098            }
099        }
100        return null;
101    }
102}