View Javadoc

1   /*
2    * Copyright (C) 2014 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.atlassian.jira.rest.client.api.domain;
17  
18  import com.google.common.base.Objects;
19  import com.google.common.collect.Maps;
20  
21  import javax.annotation.Nullable;
22  import java.util.Map;
23  
24  public class Permissions {
25  	/**
26  	 * Permission key for ability to log work done against an issue. Only useful if Time Tracking is turned on.
27  	 */
28  	public static final String WORK_ISSUE = "WORK_ISSUE";
29  	private final Map<String, Permission> permissionMap;
30  
31  	public Permissions(final Iterable<Permission> permissions) {
32  		this.permissionMap = Maps.uniqueIndex(permissions, Permission.TO_KEY);
33  	}
34  
35  	public Map<String, Permission> getPermissionMap() {
36  		return permissionMap;
37  	}
38  
39  	public boolean havePermission(final String permissionKey) {
40  		final Permission permission = getPermission(permissionKey);
41  		return (permission != null && permission.havePermission());
42  	}
43  
44  	@Nullable
45  	public Permission getPermission(final String permissionKey) {
46  		return permissionMap.get(permissionKey);
47  	}
48  
49  	@Override
50  	public String toString() {
51  		return Objects.toStringHelper(this)
52  				.add("permissionMap", permissionMap)
53  				.toString();
54  	}
55  
56  	@Override
57  	public boolean equals(Object o) {
58  		if (o instanceof Permissions) {
59  			Permissions that = (Permissions) o;
60  			return Objects.equal(permissionMap, that.permissionMap);
61  		}
62  		return false;
63  	}
64  
65  	@Override
66  	public int hashCode() {
67  		return permissionMap.hashCode();
68  	}
69  }