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