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 {@link Callable} interface. 52 * It also has a property {@link #repo} holding a reference to the git 53 * {@link Repository} this command should work with. 54 * <p> 55 * Finally this class stores a state telling whether it is allowed to call 56 * {@link #call()} on this instance. Instances of {@link GitCommand} can only be 57 * used for one single successful call to {@link #call()}. Afterwards this 58 * instance may not be used anymore to set/modify any properties or to call 59 * {@link #call()} again. This is achieved by setting the {@link #callable} 60 * property to false after the successful execution of {@link #call()} and to 61 * check the state (by calling {@link #checkCallable()}) before setting of 62 * properties and inside {@link #call()}. 63 * 64 * @param <T> 65 * the return type which is expected from {@link #call()} 66 */ 67 public abstract class GitCommand<T> implements Callable<T> { 68 /** The repository this command is working with */ 69 final protected Repository repo; 70 71 /** 72 * a state which tells whether it is allowed to call {@link #call()} on this 73 * instance. 74 */ 75 private AtomicBoolean callable = new AtomicBoolean(true); 76 77 /** 78 * Creates a new command which interacts with a single repository 79 * 80 * @param repo 81 * the {@link Repository} this command should interact with 82 */ 83 protected GitCommand(Repository repo) { 84 this.repo = repo; 85 } 86 87 /** 88 * @return the {@link Repository} this command is interacting with 89 */ 90 public Repository getRepository() { 91 return repo; 92 } 93 94 /** 95 * Set's the state which tells whether it is allowed to call {@link #call()} 96 * on this instance. {@link #checkCallable()} will throw an exception when 97 * called and this property is set to {@code false} 98 * 99 * @param callable 100 * if <code>true</code> it is allowed to call {@link #call()} on 101 * this instance. 102 */ 103 protected void setCallable(boolean callable) { 104 this.callable.set(callable); 105 } 106 107 /** 108 * Checks that the property {@link #callable} is {@code true}. If not then 109 * an {@link IllegalStateException} is thrown 110 * 111 * @throws IllegalStateException 112 * when this method is called and the property {@link #callable} 113 * is {@code false} 114 */ 115 protected void checkCallable() { 116 if (!callable.get()) 117 throw new IllegalStateException(MessageFormat.format( 118 JGitText.get().commandWasCalledInTheWrongState 119 , this.getClass().getName())); 120 } 121 122 /** 123 * Executes the command 124 * 125 * @return T a result. Each command has its own return type 126 * @throws GitAPIException 127 * or subclass thereof when an error occurs 128 */ 129 @Override 130 public abstract T call() throws GitAPIException; 131 }