View Javadoc
1   /*
2    * Copyright (C) 2015, Ivan Motsch <ivan.motsch@bsiag.com>
3    *
4    * This program and the accompanying materials are made available
5    * under the terms of the Eclipse Distribution License v1.0 which
6    * accompanies this distribution, is reproduced below, and is
7    * available at http://www.eclipse.org/org/documents/edl-v10.php
8    *
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or
12   * without modification, are permitted provided that the following
13   * conditions are met:
14   *
15   * - Redistributions of source code must retain the above copyright
16   *   notice, this list of conditions and the following disclaimer.
17   *
18   * - Redistributions in binary form must reproduce the above
19   *   copyright notice, this list of conditions and the following
20   *   disclaimer in the documentation and/or other materials provided
21   *   with the distribution.
22   *
23   * - Neither the name of the Eclipse Foundation, Inc. nor the
24   *   names of its contributors may be used to endorse or promote
25   *   products derived from this software without specific prior
26   *   written permission.
27   *
28   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
30   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41   */
42  
43  package org.eclipse.jgit.attributes;
44  
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.LinkedHashMap;
48  import java.util.Map;
49  
50  import org.eclipse.jgit.attributes.Attribute.State;
51  
52  /**
53   * Represents a set of attributes for a path
54   * <p>
55   *
56   * @since 4.2
57   */
58  public final class Attributes {
59  	private final Map<String, Attribute> map = new LinkedHashMap<>();
60  
61  	/**
62  	 * Creates a new instance
63  	 *
64  	 * @param attributes
65  	 */
66  	public Attributes(Attribute... attributes) {
67  		if (attributes != null) {
68  			for (Attribute a : attributes) {
69  				put(a);
70  			}
71  		}
72  	}
73  
74  	/**
75  	 * @return true if the set does not contain any attributes
76  	 */
77  	public boolean isEmpty() {
78  		return map.isEmpty();
79  	}
80  
81  	/**
82  	 * @param key
83  	 * @return the attribute or null
84  	 */
85  	public Attribute get(String key) {
86  		return map.get(key);
87  	}
88  
89  	/**
90  	 * @return all attributes
91  	 */
92  	public Collection<Attribute> getAll() {
93  		return new ArrayList<>(map.values());
94  	}
95  
96  	/**
97  	 * @param a
98  	 */
99  	public void put(Attribute a) {
100 		map.put(a.getKey(), a);
101 	}
102 
103 	/**
104 	 * @param key
105 	 */
106 	public void remove(String key) {
107 		map.remove(key);
108 	}
109 
110 	/**
111 	 * @param key
112 	 * @return true if the {@link Attributes} contains this key
113 	 */
114 	public boolean containsKey(String key) {
115 		return map.containsKey(key);
116 	}
117 
118 	/**
119 	 * Returns the state.
120 	 *
121 	 * @param key
122 	 *
123 	 * @return the state (never returns <code>null</code>)
124 	 */
125 	public Attribute.State getState(String key) {
126 		Attribute a = map.get(key);
127 		return a != null ? a.getState() : Attribute.State.UNSPECIFIED;
128 	}
129 
130 	/**
131 	 * @param key
132 	 * @return true if the key is {@link State#SET}, false in all other cases
133 	 */
134 	public boolean isSet(String key) {
135 		return (getState(key) == State.SET);
136 	}
137 
138 	/**
139 	 * @param key
140 	 * @return true if the key is {@link State#UNSET}, false in all other cases
141 	 */
142 	public boolean isUnset(String key) {
143 		return (getState(key) == State.UNSET);
144 	}
145 
146 	/**
147 	 * @param key
148 	 * @return true if the key is {@link State#UNSPECIFIED}, false in all other
149 	 *         cases
150 	 */
151 	public boolean isUnspecified(String key) {
152 		return (getState(key) == State.UNSPECIFIED);
153 	}
154 
155 	/**
156 	 * @param key
157 	 * @return true if the key is {@link State#CUSTOM}, false in all other cases
158 	 *         see {@link #getValue(String)} for the value of the key
159 	 */
160 	public boolean isCustom(String key) {
161 		return (getState(key) == State.CUSTOM);
162 	}
163 
164 	/**
165 	 * @param key
166 	 * @return the attribute value (may be <code>null</code>)
167 	 */
168 	public String getValue(String key) {
169 		Attribute a = map.get(key);
170 		return a != null ? a.getValue() : null;
171 	}
172 
173 	@Override
174 	public String toString() {
175 		StringBuilder buf = new StringBuilder();
176 		buf.append(getClass().getSimpleName());
177 		buf.append("["); //$NON-NLS-1$
178 		buf.append(" "); //$NON-NLS-1$
179 		for (Attribute a : map.values()) {
180 			buf.append(a.toString());
181 			buf.append(" "); //$NON-NLS-1$
182 		}
183 		buf.append("]"); //$NON-NLS-1$
184 		return buf.toString();
185 	}
186 
187 	@Override
188 	public int hashCode() {
189 		return map.hashCode();
190 	}
191 
192 	@Override
193 	public boolean equals(Object obj) {
194 		if (this == obj)
195 			return true;
196 		if (!(obj instanceof Attributes))
197 			return false;
198 		Attributes other = (Attributes) obj;
199 		return this.map.equals(other.map);
200 	}
201 
202 }