GitCommand.java

  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. import java.text.MessageFormat;
  40. import java.util.concurrent.Callable;
  41. import java.util.concurrent.atomic.AtomicBoolean;

  42. import org.eclipse.jgit.api.errors.GitAPIException;
  43. import org.eclipse.jgit.internal.JGitText;
  44. import org.eclipse.jgit.lib.Repository;

  45. /**
  46.  * Common superclass of all commands in the package {@code org.eclipse.jgit.api}
  47.  * <p>
  48.  * This class ensures that all commands fulfill the
  49.  * {@link java.util.concurrent.Callable} interface. It also has a property
  50.  * {@link #repo} holding a reference to the git
  51.  * {@link org.eclipse.jgit.lib.Repository} this command should work with.
  52.  * <p>
  53.  * Finally this class stores a state telling whether it is allowed to call
  54.  * {@link #call()} on this instance. Instances of
  55.  * {@link org.eclipse.jgit.api.GitCommand} can only be used for one single
  56.  * successful call to {@link #call()}. Afterwards this instance may not be used
  57.  * anymore to set/modify any properties or to call {@link #call()} again. This
  58.  * is achieved by setting the {@link #callable} property to false after the
  59.  * successful execution of {@link #call()} and to check the state (by calling
  60.  * {@link #checkCallable()}) before setting of properties and inside
  61.  * {@link #call()}.
  62.  *
  63.  * @param <T>
  64.  *            the return type which is expected from {@link #call()}
  65.  */
  66. public abstract class GitCommand<T> implements Callable<T> {
  67.     /** The repository this command is working with */
  68.     final protected Repository repo;

  69.     /**
  70.      * a state which tells whether it is allowed to call {@link #call()} on this
  71.      * instance.
  72.      */
  73.     private AtomicBoolean callable = new AtomicBoolean(true);

  74.     /**
  75.      * Creates a new command which interacts with a single repository
  76.      *
  77.      * @param repo
  78.      *            the {@link org.eclipse.jgit.lib.Repository} this command
  79.      *            should interact with
  80.      */
  81.     protected GitCommand(Repository repo) {
  82.         this.repo = repo;
  83.     }

  84.     /**
  85.      * Get repository this command is working on
  86.      *
  87.      * @return the {@link org.eclipse.jgit.lib.Repository} this command is
  88.      *         interacting with
  89.      */
  90.     public Repository getRepository() {
  91.         return repo;
  92.     }

  93.     /**
  94.      * Set's the state which tells whether it is allowed to call {@link #call()}
  95.      * on this instance. {@link #checkCallable()} will throw an exception when
  96.      * called and this property is set to {@code false}
  97.      *
  98.      * @param callable
  99.      *            if <code>true</code> it is allowed to call {@link #call()} on
  100.      *            this instance.
  101.      */
  102.     protected void setCallable(boolean callable) {
  103.         this.callable.set(callable);
  104.     }

  105.     /**
  106.      * Checks that the property {@link #callable} is {@code true}. If not then
  107.      * an {@link java.lang.IllegalStateException} is thrown
  108.      *
  109.      * @throws java.lang.IllegalStateException
  110.      *             when this method is called and the property {@link #callable}
  111.      *             is {@code false}
  112.      */
  113.     protected void checkCallable() {
  114.         if (!callable.get())
  115.             throw new IllegalStateException(MessageFormat.format(
  116.                     JGitText.get().commandWasCalledInTheWrongState
  117.                     , this.getClass().getName()));
  118.     }

  119.     /**
  120.      * {@inheritDoc}
  121.      * <p>
  122.      * Execute the command
  123.      */
  124.     @Override
  125.     public abstract T call() throws GitAPIException;
  126. }