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