您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GitCommand.java 4.6KB

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