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.

Attributes.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2015, Ivan Motsch <ivan.motsch@bsiag.com>,
  3. * Copyright (C) 2017, Obeo (mathieu.cartaud@obeo.fr)
  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. import java.util.ArrayList;
  45. import java.util.Collection;
  46. import java.util.LinkedHashMap;
  47. import java.util.Map;
  48. import org.eclipse.jgit.attributes.Attribute.State;
  49. import org.eclipse.jgit.lib.Constants;
  50. /**
  51. * Represents a set of attributes for a path
  52. * <p>
  53. *
  54. * @since 4.2
  55. */
  56. public final class Attributes {
  57. private final Map<String, Attribute> map = new LinkedHashMap<>();
  58. /**
  59. * Creates a new instance
  60. *
  61. * @param attributes
  62. */
  63. public Attributes(Attribute... attributes) {
  64. if (attributes != null) {
  65. for (Attribute a : attributes) {
  66. put(a);
  67. }
  68. }
  69. }
  70. /**
  71. * @return true if the set does not contain any attributes
  72. */
  73. public boolean isEmpty() {
  74. return map.isEmpty();
  75. }
  76. /**
  77. * @param key
  78. * @return the attribute or null
  79. */
  80. public Attribute get(String key) {
  81. return map.get(key);
  82. }
  83. /**
  84. * @return all attributes
  85. */
  86. public Collection<Attribute> getAll() {
  87. return new ArrayList<>(map.values());
  88. }
  89. /**
  90. * @param a
  91. */
  92. public void put(Attribute a) {
  93. map.put(a.getKey(), a);
  94. }
  95. /**
  96. * @param key
  97. */
  98. public void remove(String key) {
  99. map.remove(key);
  100. }
  101. /**
  102. * @param key
  103. * @return true if the {@link Attributes} contains this key
  104. */
  105. public boolean containsKey(String key) {
  106. return map.containsKey(key);
  107. }
  108. /**
  109. * Returns the state.
  110. *
  111. * @param key
  112. *
  113. * @return the state (never returns <code>null</code>)
  114. */
  115. public Attribute.State getState(String key) {
  116. Attribute a = map.get(key);
  117. return a != null ? a.getState() : Attribute.State.UNSPECIFIED;
  118. }
  119. /**
  120. * @param key
  121. * @return true if the key is {@link State#SET}, false in all other cases
  122. */
  123. public boolean isSet(String key) {
  124. return (getState(key) == State.SET);
  125. }
  126. /**
  127. * @param key
  128. * @return true if the key is {@link State#UNSET}, false in all other cases
  129. */
  130. public boolean isUnset(String key) {
  131. return (getState(key) == State.UNSET);
  132. }
  133. /**
  134. * @param key
  135. * @return true if the key is {@link State#UNSPECIFIED}, false in all other
  136. * cases
  137. */
  138. public boolean isUnspecified(String key) {
  139. return (getState(key) == State.UNSPECIFIED);
  140. }
  141. /**
  142. * @param key
  143. * @return true if the key is {@link State#CUSTOM}, false in all other cases
  144. * see {@link #getValue(String)} for the value of the key
  145. */
  146. public boolean isCustom(String key) {
  147. return (getState(key) == State.CUSTOM);
  148. }
  149. /**
  150. * @param key
  151. * @return the attribute value (may be <code>null</code>)
  152. */
  153. public String getValue(String key) {
  154. Attribute a = map.get(key);
  155. return a != null ? a.getValue() : null;
  156. }
  157. /**
  158. * Test if the given attributes implies to handle the related entry as a
  159. * binary file (i.e. if the entry has an -merge or a merge=binary attribute)
  160. * or if it can be content merged.
  161. *
  162. * @return <code>true</code> if the entry can be content merged,
  163. * <code>false</code> otherwise
  164. * @since 4.9
  165. */
  166. public boolean canBeContentMerged() {
  167. if (isUnset(Constants.ATTR_MERGE)) {
  168. return false;
  169. } else if (isCustom(Constants.ATTR_MERGE)
  170. && getValue(Constants.ATTR_MERGE)
  171. .equals(Constants.ATTR_BUILTIN_BINARY_MERGER)) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. @Override
  177. public String toString() {
  178. StringBuilder buf = new StringBuilder();
  179. buf.append(getClass().getSimpleName());
  180. buf.append("["); //$NON-NLS-1$
  181. buf.append(" "); //$NON-NLS-1$
  182. for (Attribute a : map.values()) {
  183. buf.append(a.toString());
  184. buf.append(" "); //$NON-NLS-1$
  185. }
  186. buf.append("]"); //$NON-NLS-1$
  187. return buf.toString();
  188. }
  189. @Override
  190. public int hashCode() {
  191. return map.hashCode();
  192. }
  193. @Override
  194. public boolean equals(Object obj) {
  195. if (this == obj)
  196. return true;
  197. if (!(obj instanceof Attributes))
  198. return false;
  199. Attributes other = (Attributes) obj;
  200. return this.map.equals(other.map);
  201. }
  202. }