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