選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Attribute.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2010, Marc Strapetz <marc.strapetz@syntevo.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.attributes;
  11. /**
  12. * Represents an attribute.
  13. * <p>
  14. * According to the man page, an attribute can have the following states:
  15. * <ul>
  16. * <li>Set - represented by
  17. * {@link org.eclipse.jgit.attributes.Attribute.State#SET}</li>
  18. * <li>Unset - represented by
  19. * {@link org.eclipse.jgit.attributes.Attribute.State#UNSET}</li>
  20. * <li>Set to a value - represented by
  21. * {@link org.eclipse.jgit.attributes.Attribute.State#CUSTOM}</li>
  22. * <li>Unspecified - used to revert an attribute . This is crucial in order to
  23. * mark an attribute as unspecified in the attributes map and thus preventing
  24. * following (with lower priority) nodes from setting the attribute to a value
  25. * at all</li>
  26. * </ul>
  27. *
  28. * @since 3.7
  29. */
  30. public final class Attribute {
  31. /**
  32. * The attribute value state
  33. * see also https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
  34. */
  35. public enum State {
  36. /** the attribute is set */
  37. SET,
  38. /** the attribute is unset */
  39. UNSET,
  40. /**
  41. * the attribute appears as if it would not be defined at all
  42. *
  43. * @since 4.2
  44. */
  45. UNSPECIFIED,
  46. /** the attribute is set to a custom value */
  47. CUSTOM
  48. }
  49. private final String key;
  50. private final State state;
  51. private final String value;
  52. /**
  53. * Creates a new instance
  54. *
  55. * @param key
  56. * the attribute key. Should not be <code>null</code>.
  57. * @param state
  58. * the attribute state. It should be either
  59. * {@link org.eclipse.jgit.attributes.Attribute.State#SET} or
  60. * {@link org.eclipse.jgit.attributes.Attribute.State#UNSET}. In
  61. * order to create a custom value attribute prefer the use of
  62. * {@link #Attribute(String, String)} constructor.
  63. */
  64. public Attribute(String key, State state) {
  65. this(key, state, null);
  66. }
  67. private Attribute(String key, State state, String value) {
  68. if (key == null)
  69. throw new NullPointerException(
  70. "The key of an attribute should not be null"); //$NON-NLS-1$
  71. if (state == null)
  72. throw new NullPointerException(
  73. "The state of an attribute should not be null"); //$NON-NLS-1$
  74. this.key = key;
  75. this.state = state;
  76. this.value = value;
  77. }
  78. /**
  79. * Creates a new instance.
  80. *
  81. * @param key
  82. * the attribute key. Should not be <code>null</code>.
  83. * @param value
  84. * the custom attribute value
  85. */
  86. public Attribute(String key, String value) {
  87. this(key, State.CUSTOM, value);
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public boolean equals(Object obj) {
  92. if (this == obj)
  93. return true;
  94. if (!(obj instanceof Attribute))
  95. return false;
  96. Attribute other = (Attribute) obj;
  97. if (!key.equals(other.key))
  98. return false;
  99. if (state != other.state)
  100. return false;
  101. if (value == null) {
  102. if (other.value != null)
  103. return false;
  104. } else if (!value.equals(other.value))
  105. return false;
  106. return true;
  107. }
  108. /**
  109. * Get key
  110. *
  111. * @return the attribute key (never returns <code>null</code>)
  112. */
  113. public String getKey() {
  114. return key;
  115. }
  116. /**
  117. * Return the state.
  118. *
  119. * @return the state (never returns <code>null</code>)
  120. */
  121. public State getState() {
  122. return state;
  123. }
  124. /**
  125. * Get value
  126. *
  127. * @return the attribute value (may be <code>null</code>)
  128. */
  129. public String getValue() {
  130. return value;
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public int hashCode() {
  135. final int prime = 31;
  136. int result = 1;
  137. result = prime * result + key.hashCode();
  138. result = prime * result + state.hashCode();
  139. result = prime * result + ((value == null) ? 0 : value.hashCode());
  140. return result;
  141. }
  142. /** {@inheritDoc} */
  143. @Override
  144. public String toString() {
  145. switch (state) {
  146. case SET:
  147. return key;
  148. case UNSET:
  149. return "-" + key; //$NON-NLS-1$
  150. case UNSPECIFIED:
  151. return "!" + key; //$NON-NLS-1$
  152. case CUSTOM:
  153. default:
  154. return key + "=" + value; //$NON-NLS-1$
  155. }
  156. }
  157. }