You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitCommand.java 4.8KB

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