View Javadoc
1   /*
2    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com> and
3    * other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v1.0 which accompanies this
7    * distribution, is reproduced below, and is available at
8    * http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or without
13   * modification, are permitted provided that the following conditions are met:
14   *
15   * - Redistributions of source code must retain the above copyright notice, this
16   * list of conditions and the following disclaimer.
17   *
18   * - Redistributions in binary form must reproduce the above copyright notice,
19   * this list of conditions and the following disclaimer in the documentation
20   * and/or other materials provided with the distribution.
21   *
22   * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
23   * contributors may be used to endorse or promote products derived from this
24   * software without specific prior written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36   * POSSIBILITY OF SUCH DAMAGE.
37   */
38  package org.eclipse.jgit.api;
39  
40  import java.text.MessageFormat;
41  import java.util.concurrent.Callable;
42  import java.util.concurrent.atomic.AtomicBoolean;
43  
44  import org.eclipse.jgit.api.errors.GitAPIException;
45  import org.eclipse.jgit.internal.JGitText;
46  import org.eclipse.jgit.lib.Repository;
47  
48  /**
49   * Common superclass of all commands in the package {@code org.eclipse.jgit.api}
50   * <p>
51   * This class ensures that all commands fulfill the
52   * {@link java.util.concurrent.Callable} interface. It also has a property
53   * {@link #repo} holding a reference to the git
54   * {@link org.eclipse.jgit.lib.Repository} this command should work with.
55   * <p>
56   * Finally this class stores a state telling whether it is allowed to call
57   * {@link #call()} on this instance. Instances of
58   * {@link org.eclipse.jgit.api.GitCommand} can only be used for one single
59   * successful call to {@link #call()}. Afterwards this instance may not be used
60   * anymore to set/modify any properties or to call {@link #call()} again. This
61   * is achieved by setting the {@link #callable} property to false after the
62   * successful execution of {@link #call()} and to check the state (by calling
63   * {@link #checkCallable()}) before setting of properties and inside
64   * {@link #call()}.
65   *
66   * @param <T>
67   *            the return type which is expected from {@link #call()}
68   */
69  public abstract class GitCommand<T> implements Callable<T> {
70  	/** The repository this command is working with */
71  	final protected Repository repo;
72  
73  	/**
74  	 * a state which tells whether it is allowed to call {@link #call()} on this
75  	 * instance.
76  	 */
77  	private AtomicBoolean callable = new AtomicBoolean(true);
78  
79  	/**
80  	 * Creates a new command which interacts with a single repository
81  	 *
82  	 * @param repo
83  	 *            the {@link org.eclipse.jgit.lib.Repository} this command
84  	 *            should interact with
85  	 */
86  	protected GitCommand(Repository repo) {
87  		this.repo = repo;
88  	}
89  
90  	/**
91  	 * Get repository this command is working on
92  	 *
93  	 * @return the {@link org.eclipse.jgit.lib.Repository} this command is
94  	 *         interacting with
95  	 */
96  	public Repository getRepository() {
97  		return repo;
98  	}
99  
100 	/**
101 	 * Set's the state which tells whether it is allowed to call {@link #call()}
102 	 * on this instance. {@link #checkCallable()} will throw an exception when
103 	 * called and this property is set to {@code false}
104 	 *
105 	 * @param callable
106 	 *            if <code>true</code> it is allowed to call {@link #call()} on
107 	 *            this instance.
108 	 */
109 	protected void setCallable(boolean callable) {
110 		this.callable.set(callable);
111 	}
112 
113 	/**
114 	 * Checks that the property {@link #callable} is {@code true}. If not then
115 	 * an {@link java.lang.IllegalStateException} is thrown
116 	 *
117 	 * @throws java.lang.IllegalStateException
118 	 *             when this method is called and the property {@link #callable}
119 	 *             is {@code false}
120 	 */
121 	protected void checkCallable() {
122 		if (!callable.get())
123 			throw new IllegalStateException(MessageFormat.format(
124 					JGitText.get().commandWasCalledInTheWrongState
125 					, this.getClass().getName()));
126 	}
127 
128 	/**
129 	 * {@inheritDoc}
130 	 * <p>
131 	 * Execute the command
132 	 */
133 	@Override
134 	public abstract T call() throws GitAPIException;
135 }