001/*
002 * This library is part of OpenCms -
003 * The Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.loader.imagecache;
029
030import java.time.Duration;
031import java.util.Collections;
032import java.util.LinkedHashMap;
033import java.util.Map;
034import java.util.Objects;
035
036/**
037 * Metrics and failures produced by a maintenance request.<p>
038 */
039public final class CmsImageCacheMaintenanceResult {
040
041    /** Builder for maintenance results. */
042    public static final class Builder {
043
044        /** The recorded failures. */
045        private Map<String, Exception> m_failures = new LinkedHashMap<String, Exception>();
046
047        /** The operation. */
048        private final CmsImageCacheMaintenanceRequest.Operation m_operation;
049
050        /** The requested operation count. */
051        private final int m_requested;
052
053        /** The skipped operation count. */
054        private int m_skipped;
055
056        /** The successful operation count. */
057        private int m_succeeded;
058
059        /**
060         * Creates a builder.<p>
061         *
062         * @param operation the operation
063         * @param requested the requested operation count
064         */
065        public Builder(CmsImageCacheMaintenanceRequest.Operation operation, int requested) {
066
067            if (requested < 0) {
068                throw new IllegalArgumentException("The requested operation count must not be negative.");
069            }
070            m_operation = Objects.requireNonNull(operation, "operation");
071            m_requested = requested;
072        }
073
074        /**
075         * Records a failure.<p>
076         *
077         * @param key the entry key or operation identifier
078         * @param exception the failure
079         * @return this builder
080         */
081        public Builder addFailure(String key, Exception exception) {
082
083            m_failures.put(key, exception);
084            return this;
085        }
086
087        /**
088         * Records a skipped operation.<p>
089         *
090         * @return this builder
091         */
092        public Builder addSkipped() {
093
094            m_skipped += 1;
095            return this;
096        }
097
098        /**
099         * Records a successful operation.<p>
100         *
101         * @return this builder
102         */
103        public Builder addSuccess() {
104
105            m_succeeded += 1;
106            return this;
107        }
108
109        /**
110         * Creates the result.<p>
111         *
112         * @param elapsed the elapsed time
113         * @return the result
114         */
115        public CmsImageCacheMaintenanceResult build(Duration elapsed) {
116
117            if ((m_succeeded + m_skipped + m_failures.size()) != m_requested) {
118                throw new IllegalStateException("The maintenance result does not contain one outcome per request.");
119            }
120            return new CmsImageCacheMaintenanceResult(this, elapsed);
121        }
122    }
123
124    /** The elapsed time. */
125    private final Duration m_elapsed;
126
127    /** The failures. */
128    private final Map<String, Exception> m_failures;
129
130    /** The operation. */
131    private final CmsImageCacheMaintenanceRequest.Operation m_operation;
132
133    /** The requested operation count. */
134    private final int m_requested;
135
136    /** The skipped operation count. */
137    private final int m_skipped;
138
139    /** The successful operation count. */
140    private final int m_succeeded;
141
142    /**
143     * Creates a result.<p>
144     *
145     * @param builder the result builder
146     * @param elapsed the elapsed time
147     */
148    private CmsImageCacheMaintenanceResult(Builder builder, Duration elapsed) {
149
150        m_operation = builder.m_operation;
151        m_requested = builder.m_requested;
152        m_succeeded = builder.m_succeeded;
153        m_skipped = builder.m_skipped;
154        m_failures = Collections.unmodifiableMap(new LinkedHashMap<String, Exception>(builder.m_failures));
155        m_elapsed = elapsed;
156    }
157
158    /** Returns the elapsed time. */
159    public Duration getElapsed() {
160
161        return m_elapsed;
162    }
163
164    /** Returns the number of failures. */
165    public int getFailed() {
166
167        return m_failures.size();
168    }
169
170    /** Returns the failures keyed by cache entry. */
171    public Map<String, Exception> getFailures() {
172
173        return m_failures;
174    }
175
176    /** Returns the operation. */
177    public CmsImageCacheMaintenanceRequest.Operation getOperation() {
178
179        return m_operation;
180    }
181
182    /** Returns the requested operation count. */
183    public int getRequested() {
184
185        return m_requested;
186    }
187
188    /** Returns the skipped operation count. */
189    public int getSkipped() {
190
191        return m_skipped;
192    }
193
194    /** Returns the successful operation count. */
195    public int getSucceeded() {
196
197        return m_succeeded;
198    }
199
200    /** Returns whether all requested operations succeeded or were skipped. */
201    public boolean isSuccessful() {
202
203        return m_failures.isEmpty();
204    }
205}