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  
17  package com.atlassian.jira.rest.client.api.domain;
18  
19  import com.google.common.base.Objects;
20  import com.google.common.base.Optional;
21  import com.google.common.collect.Iterables;
22  
23  import javax.annotation.Nullable;
24  import java.util.Collections;
25  
26  /**
27   * Represents operations group
28   *
29   * @since 2.0
30   */
31  public class OperationGroup implements Operation {
32  	@Nullable private final String id;
33  	@Nullable private final OperationHeader header;
34  	private final Iterable<OperationLink> links;
35  	private final Iterable<OperationGroup> groups;
36  	@Nullable private final Integer weight;
37  
38  	public OperationGroup(@Nullable final String id, final Iterable<OperationLink> links,
39  			final Iterable<OperationGroup> groups, @Nullable final OperationHeader header,
40  			@Nullable final Integer weight) {
41  		this.id = id;
42  		this.header = header;
43  		this.links = links;
44  		this.groups = groups;
45  		this.weight = weight;
46  	}
47  
48  	@Nullable
49  	public String getId() {
50  		return id;
51  	}
52  
53  	@Override
54  	public <T> Optional<T> accept(final OperationVisitor<T> visitor) {
55  		final Optional<T> result = visitor.visit(this);
56  		if (result.isPresent()) {
57  			return result;
58  		} else {
59  			final Iterable<Operation> operations = Iterables.concat(
60  					header != null ? Collections.singleton(header) : Collections.<Operation>emptyList(),
61  					links, groups);
62  			return accept(operations, visitor);
63  		}
64  	}
65  
66  	static <T> Optional<T> accept(final Iterable<? extends Operation> operations, final OperationVisitor<T> visitor) {
67  		for (Operation operation : operations) {
68  			Optional<T> result = operation.accept(visitor);
69  			if (result.isPresent()) {
70  				return result;
71  			}
72  		}
73  		return Optional.absent();
74  	}
75  
76  	@Nullable
77  	public OperationHeader getHeader() {
78  		return header;
79  	}
80  
81  	public Iterable<OperationLink> getLinks() {
82  		return links;
83  	}
84  
85  	public Iterable<OperationGroup> getGroups() {
86  		return groups;
87  	}
88  
89  	@Nullable
90  	public Integer getWeight() {
91  		return weight;
92  	}
93  
94  	@Override
95  	public int hashCode() {
96  		return Objects.hashCode(id, header, links, groups, weight);
97  	}
98  
99  	@Override
100 	public boolean equals(Object obj) {
101 		if (this == obj) {
102 			return true;
103 		}
104 		if (obj == null || getClass() != obj.getClass()) {
105 			return false;
106 		}
107 		final OperationGroup other = (OperationGroup) obj;
108 		return Objects.equal(this.id, other.id)
109 				&& Objects.equal(this.header, other.header)
110 				&& Iterables.elementsEqual(this.links, other.links)
111 				&& Iterables.elementsEqual(this.groups, other.groups)
112 				&& Objects.equal(this.weight, other.weight);
113 	}
114 
115 	@Override
116 	public String toString() {
117 		return Objects.toStringHelper(this)
118 				.add("id", id)
119 				.add("header", header)
120 				.add("links", Iterables.toString(links))
121 				.add("groups", Iterables.toString(groups))
122 				.add("weight", weight)
123 				.toString();
124 	}
125 }