Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Attribute.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2010, Marc Strapetz <marc.strapetz@syntevo.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at 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
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.attributes;
  44. /**
  45. * Represents an attribute.
  46. * <p>
  47. * According to the man page, an attribute can have the following states:
  48. * <ul>
  49. * <li>Set - represented by
  50. * {@link org.eclipse.jgit.attributes.Attribute.State#SET}</li>
  51. * <li>Unset - represented by
  52. * {@link org.eclipse.jgit.attributes.Attribute.State#UNSET}</li>
  53. * <li>Set to a value - represented by
  54. * {@link org.eclipse.jgit.attributes.Attribute.State#CUSTOM}</li>
  55. * <li>Unspecified - used to revert an attribute . This is crucial in order to
  56. * mark an attribute as unspecified in the attributes map and thus preventing
  57. * following (with lower priority) nodes from setting the attribute to a value
  58. * at all</li>
  59. * </ul>
  60. *
  61. * @since 3.7
  62. */
  63. public final class Attribute {
  64. /**
  65. * The attribute value state
  66. * see also https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
  67. */
  68. public enum State {
  69. /** the attribute is set */
  70. SET,
  71. /** the attribute is unset */
  72. UNSET,
  73. /**
  74. * the attribute appears as if it would not be defined at all
  75. *
  76. * @since 4.2
  77. */
  78. UNSPECIFIED,
  79. /** the attribute is set to a custom value */
  80. CUSTOM
  81. }
  82. private final String key;
  83. private final State state;
  84. private final String value;
  85. /**
  86. * Creates a new instance
  87. *
  88. * @param key
  89. * the attribute key. Should not be <code>null</code>.
  90. * @param state
  91. * the attribute state. It should be either
  92. * {@link org.eclipse.jgit.attributes.Attribute.State#SET} or
  93. * {@link org.eclipse.jgit.attributes.Attribute.State#UNSET}. In
  94. * order to create a custom value attribute prefer the use of
  95. * {@link #Attribute(String, String)} constructor.
  96. */
  97. public Attribute(String key, State state) {
  98. this(key, state, null);
  99. }
  100. private Attribute(String key, State state, String value) {
  101. if (key == null)
  102. throw new NullPointerException(
  103. "The key of an attribute should not be null"); //$NON-NLS-1$
  104. if (state == null)
  105. throw new NullPointerException(
  106. "The state of an attribute should not be null"); //$NON-NLS-1$
  107. this.key = key;
  108. this.state = state;
  109. this.value = value;
  110. }
  111. /**
  112. * Creates a new instance.
  113. *
  114. * @param key
  115. * the attribute key. Should not be <code>null</code>.
  116. * @param value
  117. * the custom attribute value
  118. */
  119. public Attribute(String key, String value) {
  120. this(key, State.CUSTOM, value);
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public boolean equals(Object obj) {
  125. if (this == obj)
  126. return true;
  127. if (!(obj instanceof Attribute))
  128. return false;
  129. Attribute other = (Attribute) obj;
  130. if (!key.equals(other.key))
  131. return false;
  132. if (state != other.state)
  133. return false;
  134. if (value == null) {
  135. if (other.value != null)
  136. return false;
  137. } else if (!value.equals(other.value))
  138. return false;
  139. return true;
  140. }
  141. /**
  142. * Get key
  143. *
  144. * @return the attribute key (never returns <code>null</code>)
  145. */
  146. public String getKey() {
  147. return key;
  148. }
  149. /**
  150. * Return the state.
  151. *
  152. * @return the state (never returns <code>null</code>)
  153. */
  154. public State getState() {
  155. return state;
  156. }
  157. /**
  158. * Get value
  159. *
  160. * @return the attribute value (may be <code>null</code>)
  161. */
  162. public String getValue() {
  163. return value;
  164. }
  165. /** {@inheritDoc} */
  166. @Override
  167. public int hashCode() {
  168. final int prime = 31;
  169. int result = 1;
  170. result = prime * result + key.hashCode();
  171. result = prime * result + state.hashCode();
  172. result = prime * result + ((value == null) ? 0 : value.hashCode());
  173. return result;
  174. }
  175. /** {@inheritDoc} */
  176. @Override
  177. public String toString() {
  178. switch (state) {
  179. case SET:
  180. return key;
  181. case UNSET:
  182. return "-" + key; //$NON-NLS-1$
  183. case UNSPECIFIED:
  184. return "!" + key; //$NON-NLS-1$
  185. case CUSTOM:
  186. default:
  187. return key + "=" + value; //$NON-NLS-1$
  188. }
  189. }
  190. }