View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.persistence.jpa.content;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  import org.apache.commons.lang3.builder.EqualsBuilder;
24  import org.apache.commons.lang3.builder.HashCodeBuilder;
25  import org.apache.commons.lang3.builder.ToStringBuilder;
26  
27  class MultiParentNode<T> {
28  
29      private final T object;
30  
31      private final Set<MultiParentNode<T>> children = new HashSet<>();
32  
33      private int level;
34  
35      private boolean exploited = false;
36  
37      MultiParentNode(final T object) {
38          this.object = object;
39      }
40  
41      public int getLevel() {
42          return level;
43      }
44  
45      public void setLevel(final int level) {
46          this.level = level;
47      }
48  
49      boolean isExploited() {
50          return exploited;
51      }
52  
53      void setExploited(final boolean exploited) {
54          this.exploited = exploited;
55      }
56  
57      public T getObject() {
58          return object;
59      }
60  
61      public boolean isParent(final MultiParentNode<T> child) {
62          return children.contains(child);
63      }
64  
65      public boolean isChild(final MultiParentNode<T> parent) {
66          return parent.isParent(this);
67      }
68  
69      public Set<MultiParentNode<T>> getChildren() {
70          return children;
71      }
72  
73      public void addParent(final MultiParentNode<T> parent) {
74          if (parent != null) {
75              parent.children.add(this);
76          }
77      }
78  
79      public void removeParent(final MultiParentNode<T> parent) {
80          if (parent != null) {
81              parent.children.remove(this);
82          }
83      }
84  
85      public void addChild(final MultiParentNode<T> child) {
86          if (child != null) {
87              children.add(child);
88          }
89      }
90  
91      public void removeChild(final MultiParentNode<T> child) {
92          if (child != null) {
93              children.remove(child);
94          }
95      }
96  
97      @Override
98      public int hashCode() {
99          return new HashCodeBuilder().
100                 append(object).
101                 append(children).
102                 append(level).
103                 append(exploited).
104                 build();
105     }
106 
107     @Override
108     public boolean equals(final Object obj) {
109         if (this == obj) {
110             return true;
111         }
112         if (obj == null) {
113             return false;
114         }
115         if (getClass() != obj.getClass()) {
116             return false;
117         }
118         @SuppressWarnings("unchecked")
119         final MultiParentNode<T> other = (MultiParentNode<T>) obj;
120         return new EqualsBuilder().
121                 append(object, other.object).
122                 append(children, other.children).
123                 append(level, other.level).
124                 append(exploited, other.exploited).
125                 build();
126     }
127 
128     @Override
129     public String toString() {
130         return new ToStringBuilder(this).
131                 append(object).
132                 append(children).
133                 append(level).
134                 append(exploited).
135                 build();
136     }
137 }