xref: /JGit/org.eclipse.jgit/src/org/eclipse/jgit/transport/PostUploadHookChain.java (revision d1f7efcf2b715777b0bd74f4c9c6deba102cef4c)
1d9bbb04cSTerry Parker /*
2d9bbb04cSTerry Parker  * Copyright (C) 2015, Google Inc.
3d9bbb04cSTerry Parker  *
4d9bbb04cSTerry Parker  * This program and the accompanying materials are made available
5d9bbb04cSTerry Parker  * under the terms of the Eclipse Distribution License v1.0 which
6d9bbb04cSTerry Parker  * accompanies this distribution, is reproduced below, and is
7d9bbb04cSTerry Parker  * available at http://www.eclipse.org/org/documents/edl-v10.php
8d9bbb04cSTerry Parker  *
9d9bbb04cSTerry Parker  * All rights reserved.
10d9bbb04cSTerry Parker  *
11d9bbb04cSTerry Parker  * Redistribution and use in source and binary forms, with or
12d9bbb04cSTerry Parker  * without modification, are permitted provided that the following
13d9bbb04cSTerry Parker  * conditions are met:
14d9bbb04cSTerry Parker  *
15d9bbb04cSTerry Parker  * - Redistributions of source code must retain the above copyright
16d9bbb04cSTerry Parker  *   notice, this list of conditions and the following disclaimer.
17d9bbb04cSTerry Parker  *
18d9bbb04cSTerry Parker  * - Redistributions in binary form must reproduce the above
19d9bbb04cSTerry Parker  *   copyright notice, this list of conditions and the following
20d9bbb04cSTerry Parker  *   disclaimer in the documentation and/or other materials provided
21d9bbb04cSTerry Parker  *   with the distribution.
22d9bbb04cSTerry Parker  *
23d9bbb04cSTerry Parker  * - Neither the name of the Eclipse Foundation, Inc. nor the
24d9bbb04cSTerry Parker  *   names of its contributors may be used to endorse or promote
25d9bbb04cSTerry Parker  *   products derived from this software without specific prior
26d9bbb04cSTerry Parker  *   written permission.
27d9bbb04cSTerry Parker  *
28d9bbb04cSTerry Parker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29d9bbb04cSTerry Parker  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
30d9bbb04cSTerry Parker  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31d9bbb04cSTerry Parker  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32d9bbb04cSTerry Parker  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33d9bbb04cSTerry Parker  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34d9bbb04cSTerry Parker  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35d9bbb04cSTerry Parker  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36d9bbb04cSTerry Parker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37d9bbb04cSTerry Parker  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38d9bbb04cSTerry Parker  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39d9bbb04cSTerry Parker  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40d9bbb04cSTerry Parker  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41d9bbb04cSTerry Parker  */
42d9bbb04cSTerry Parker 
43d9bbb04cSTerry Parker package org.eclipse.jgit.transport;
44d9bbb04cSTerry Parker 
45*d1f7efcfSIvan Frade import java.util.Collections;
46d9bbb04cSTerry Parker import java.util.List;
47*d1f7efcfSIvan Frade import java.util.stream.Collectors;
48d9bbb04cSTerry Parker 
49d9bbb04cSTerry Parker import org.eclipse.jgit.storage.pack.PackStatistics;
50d9bbb04cSTerry Parker 
51d9bbb04cSTerry Parker /**
52781e106aSMatthias Sohn  * {@link org.eclipse.jgit.transport.PostUploadHook} that delegates to a list of
53781e106aSMatthias Sohn  * other hooks.
54d9bbb04cSTerry Parker  * <p>
55d9bbb04cSTerry Parker  * Hooks are run in the order passed to the constructor.
56d9bbb04cSTerry Parker  *
57d9bbb04cSTerry Parker  * @since 4.1
58d9bbb04cSTerry Parker  */
59d9bbb04cSTerry Parker public class PostUploadHookChain implements PostUploadHook {
60*d1f7efcfSIvan Frade 	private final List<PostUploadHook> hooks;
61d9bbb04cSTerry Parker 
62d9bbb04cSTerry Parker 	/**
63d9bbb04cSTerry Parker 	 * Create a new hook chaining the given hooks together.
64d9bbb04cSTerry Parker 	 *
65d9bbb04cSTerry Parker 	 * @param hooks
66d9bbb04cSTerry Parker 	 *            hooks to execute, in order.
67d9bbb04cSTerry Parker 	 * @return a new chain of the given hooks.
68d9bbb04cSTerry Parker 	 */
newChain(List<PostUploadHook> hooks)69*d1f7efcfSIvan Frade 	public static PostUploadHook newChain(List<PostUploadHook> hooks) {
70*d1f7efcfSIvan Frade 		List<PostUploadHook> newHooks = hooks.stream()
71*d1f7efcfSIvan Frade 				.filter(hook -> !hook.equals(PostUploadHook.NULL))
72*d1f7efcfSIvan Frade 				.collect(Collectors.toList());
73*d1f7efcfSIvan Frade 
74*d1f7efcfSIvan Frade 		if (newHooks.isEmpty()) {
75d9bbb04cSTerry Parker 			return PostUploadHook.NULL;
76*d1f7efcfSIvan Frade 		} else if (newHooks.size() == 1) {
77*d1f7efcfSIvan Frade 			return newHooks.get(0);
78*d1f7efcfSIvan Frade 		} else {
79*d1f7efcfSIvan Frade 			return new PostUploadHookChain(newHooks);
80*d1f7efcfSIvan Frade 		}
81d9bbb04cSTerry Parker 	}
82d9bbb04cSTerry Parker 
83781e106aSMatthias Sohn 	/** {@inheritDoc} */
847ac182f4SDavid Pursehouse 	@Override
onPostUpload(PackStatistics stats)85d9bbb04cSTerry Parker 	public void onPostUpload(PackStatistics stats) {
86*d1f7efcfSIvan Frade 		for (PostUploadHook hook : hooks) {
87*d1f7efcfSIvan Frade 			hook.onPostUpload(stats);
88*d1f7efcfSIvan Frade 		}
89d9bbb04cSTerry Parker 	}
90d9bbb04cSTerry Parker 
PostUploadHookChain(List<PostUploadHook> hooks)91*d1f7efcfSIvan Frade 	private PostUploadHookChain(List<PostUploadHook> hooks) {
92*d1f7efcfSIvan Frade 		this.hooks = Collections.unmodifiableList(hooks);
93d9bbb04cSTerry Parker 	}
94d9bbb04cSTerry Parker }
95