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.

Attribute.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 {@link State#SET}</li>
  50. * <li>Unset - represented by {@link State#UNSET}</li>
  51. * <li>Set to a value - represented by {@link State#CUSTOM}</li>
  52. * <li>Unspecified - used to revert an attribute . This is crucial in order to
  53. * mark an attribute as unspecified in the attributes map and thus preventing
  54. * following (with lower priority) nodes from setting the attribute to a value
  55. * at all</li>
  56. * </ul>
  57. * </p>
  58. *
  59. * @since 3.7
  60. */
  61. public final class Attribute {
  62. /**
  63. * The attribute value state
  64. * see also https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
  65. */
  66. public static enum State {
  67. /** the attribute is set */
  68. SET,
  69. /** the attribute is unset */
  70. UNSET,
  71. /**
  72. * the attribute appears as if it would not be defined at all
  73. *
  74. * @since 4.2
  75. */
  76. UNSPECIFIED,
  77. /** the attribute is set to a custom value */
  78. CUSTOM
  79. }
  80. private final String key;
  81. private final State state;
  82. private final String value;
  83. /**
  84. * Creates a new instance
  85. *
  86. * @param key
  87. * the attribute key. Should not be <code>null</code>.
  88. * @param state
  89. * the attribute state. It should be either {@link State#SET} or
  90. * {@link State#UNSET}. In order to create a custom value
  91. * attribute prefer the use of {@link #Attribute(String, String)}
  92. * constructor.
  93. */
  94. public Attribute(String key, State state) {
  95. this(key, state, null);
  96. }
  97. private Attribute(String key, State state, String value) {
  98. if (key == null)
  99. throw new NullPointerException(
  100. "The key of an attribute should not be null"); //$NON-NLS-1$
  101. if (state == null)
  102. throw new NullPointerException(
  103. "The state of an attribute should not be null"); //$NON-NLS-1$
  104. this.key = key;
  105. this.state = state;
  106. this.value = value;
  107. }
  108. /**
  109. * Creates a new instance.
  110. *
  111. * @param key
  112. * the attribute key. Should not be <code>null</code>.
  113. * @param value
  114. * the custom attribute value
  115. */
  116. public Attribute(String key, String value) {
  117. this(key, State.CUSTOM, value);
  118. }
  119. @Override
  120. public boolean equals(Object obj) {
  121. if (this == obj)
  122. return true;
  123. if (!(obj instanceof Attribute))
  124. return false;
  125. Attribute other = (Attribute) obj;
  126. if (!key.equals(other.key))
  127. return false;
  128. if (state != other.state)
  129. return false;
  130. if (value == null) {
  131. if (other.value != null)
  132. return false;
  133. } else if (!value.equals(other.value))
  134. return false;
  135. return true;
  136. }
  137. /**
  138. * @return the attribute key (never returns <code>null</code>)
  139. */
  140. public String getKey() {
  141. return key;
  142. }
  143. /**
  144. * Returns the state.
  145. *
  146. * @return the state (never returns <code>null</code>)
  147. */
  148. public State getState() {
  149. return state;
  150. }
  151. /**
  152. * @return the attribute value (may be <code>null</code>)
  153. */
  154. public String getValue() {
  155. return value;
  156. }
  157. @Override
  158. public int hashCode() {
  159. final int prime = 31;
  160. int result = 1;
  161. result = prime * result + key.hashCode();
  162. result = prime * result + state.hashCode();
  163. result = prime * result + ((value == null) ? 0 : value.hashCode());
  164. return result;
  165. }
  166. @Override
  167. public String toString() {
  168. switch (state) {
  169. case SET:
  170. return key;
  171. case UNSET:
  172. return "-" + key; //$NON-NLS-1$
  173. case UNSPECIFIED:
  174. return "!" + key; //$NON-NLS-1$
  175. case CUSTOM:
  176. default:
  177. return key + "=" + value; //$NON-NLS-1$
  178. }
  179. }
  180. }