View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.transport;
12  
13  import java.util.Collection;
14  import java.util.List;
15  
16  /**
17   * {@link org.eclipse.jgit.transport.PostReceiveHook} that delegates to a list
18   * of other hooks.
19   * <p>
20   * Hooks are run in the order passed to the constructor.
21   */
22  public class PostReceiveHookChain implements PostReceiveHook {
23  	private final PostReceiveHook[] hooks;
24  	private final int count;
25  
26  	/**
27  	 * Create a new hook chaining the given hooks together.
28  	 *
29  	 * @param hooks
30  	 *            hooks to execute, in order.
31  	 * @return a new hook chain of the given hooks.
32  	 */
33  	public static PostReceiveHook newChain(
34  			List<? extends PostReceiveHook> hooks) {
35  		PostReceiveHook[] newHooks = new PostReceiveHook[hooks.size()];
36  		int i = 0;
37  		for (PostReceiveHook hook : hooks)
38  			if (hook != PostReceiveHook.NULL)
39  				newHooks[i++] = hook;
40  		switch (i) {
41  		case 0:
42  			return PostReceiveHook.NULL;
43  		case 1:
44  			return newHooks[0];
45  		default:
46  			return new PostReceiveHookChain(newHooks, i);
47  		}
48  	}
49  
50  	/** {@inheritDoc} */
51  	@Override
52  	public void onPostReceive(ReceivePack rp,
53  			Collection<ReceiveCommand> commands) {
54  		for (int i = 0; i < count; i++)
55  			hooks[i].onPostReceive(rp, commands);
56  	}
57  
58  	private PostReceiveHookChain(PostReceiveHook[] hooks, int count) {
59  		this.hooks = hooks;
60  		this.count = count;
61  	}
62  }