001/*
002 * Copyright (C) 2012 Facebook, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may
005 * not use this file except in compliance with the License. You may obtain
006 * a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations
014 * under the License.
015 */
016package com.facebook.swift.codec.metadata;
017
018import com.google.common.collect.ImmutableList;
019import com.google.common.collect.Lists;
020
021import javax.annotation.concurrent.NotThreadSafe;
022import java.util.List;
023
024/**
025 * MetadataErrors collects all known issues with metadata extraction.  This allows all known
026 * problems to be reported together instead of one at a time.
027 * <p/>
028 * This code is heavily based on https://github.com/dain/platform/blob/master/configuration/src/main/java/com/proofpoint/configuration/Problems.java
029 */
030@NotThreadSafe
031public class MetadataErrors
032{
033    private final List<MetadataErrorException> errors = Lists.newArrayList();
034    private final List<MetadataWarningException> warnings = Lists.newArrayList();
035    private final Monitor monitor;
036
037    public interface Monitor
038    {
039        void onError(MetadataErrorException errorMessage);
040
041        void onWarning(MetadataWarningException warningMessage);
042    }
043
044    public static final NullMonitor NULL_MONITOR = new NullMonitor();
045
046    private static final class NullMonitor implements MetadataErrors.Monitor
047    {
048        @Override
049        public void onError(MetadataErrorException unused)
050        {
051        }
052
053        @Override
054        public void onWarning(MetadataWarningException unused)
055        {
056        }
057    }
058
059    public MetadataErrors()
060    {
061        this.monitor = NULL_MONITOR;
062    }
063
064    public MetadataErrors(Monitor monitor)
065    {
066        this.monitor = monitor;
067    }
068
069    public void throwIfHasErrors()
070            throws MetadataErrorException
071    {
072        if (!errors.isEmpty()) {
073            MetadataErrorException exception = new MetadataErrorException(
074                    "Metadata extraction encountered %s errors and %s warnings",
075                    errors.size(),
076                    warnings.size()
077            );
078            for (MetadataErrorException error : errors) {
079                exception.addSuppressed(error);
080            }
081            for (MetadataWarningException warning : warnings) {
082                exception.addSuppressed(warning);
083            }
084            throw exception;
085        }
086    }
087
088    public List<MetadataErrorException> getErrors()
089    {
090        return ImmutableList.copyOf(errors);
091    }
092
093    public void addError(String format, Object... params)
094    {
095        MetadataErrorException message = new MetadataErrorException(format, params);
096        errors.add(message);
097        monitor.onError(message);
098    }
099
100    public void addError(Throwable e, String format, Object... params)
101    {
102        MetadataErrorException message = new MetadataErrorException(e, format, params);
103        errors.add(message);
104        monitor.onError(message);
105    }
106
107    public List<MetadataWarningException> getWarnings()
108    {
109        return ImmutableList.copyOf(warnings);
110    }
111
112    public void addWarning(String format, Object... params)
113    {
114        MetadataWarningException message = new MetadataWarningException(format, params);
115        warnings.add(message);
116        monitor.onWarning(message);
117    }
118
119    public void addWarning(Throwable e, String format, Object... params)
120    {
121        MetadataWarningException message = new MetadataWarningException(e, format, params);
122        warnings.add(message);
123        monitor.onWarning(message);
124    }
125
126    public String toString()
127    {
128        StringBuilder builder = new StringBuilder();
129        for (MetadataErrorException error : errors) {
130            builder.append(error.getMessage()).append('\n');
131        }
132        for (MetadataWarningException warning : warnings) {
133            builder.append(warning.getMessage()).append('\n');
134        }
135        return builder.toString();
136    }
137}