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.common.lib.to;
20  
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
23  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
24  import java.io.Serializable;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Optional;
28  import org.apache.commons.lang3.builder.EqualsBuilder;
29  import org.apache.commons.lang3.builder.HashCodeBuilder;
30  
31  public abstract class ItemContainer implements Serializable {
32  
33      private static final long serialVersionUID = 3637981417797873343L;
34  
35      private final List<Item> items = new ArrayList<>();
36  
37      @JsonIgnore
38      public Optional<Item> getConnObjectKeyItem() {
39          return getItems().stream().filter(Item::isConnObjectKey).findFirst();
40      }
41  
42      protected boolean addConnObjectKeyItem(final Item connObjectItem) {
43          connObjectItem.setMandatoryCondition("true");
44          connObjectItem.setConnObjectKey(true);
45  
46          return add(connObjectItem);
47      }
48  
49      public boolean setConnObjectKeyItem(final Item connObjectKeyItem) {
50          return Optional.ofNullable(connObjectKeyItem).
51                  map(this::addConnObjectKeyItem).
52                  orElseGet(() -> getConnObjectKeyItem().map(items::remove).orElse(false));
53      }
54  
55      @JacksonXmlElementWrapper(localName = "items")
56      @JacksonXmlProperty(localName = "item")
57      public List<Item> getItems() {
58          return items;
59      }
60  
61      public boolean add(final Item item) {
62          return Optional.ofNullable(item).
63                  filter(itemTO -> items.contains(itemTO) || items.add(itemTO)).isPresent();
64      }
65  
66      @Override
67      public boolean equals(final Object obj) {
68          if (this == obj) {
69              return true;
70          }
71          if (obj == null) {
72              return false;
73          }
74          if (getClass() != obj.getClass()) {
75              return false;
76          }
77          final ItemContainer other = (ItemContainer) obj;
78          return new EqualsBuilder().
79                  append(items, other.items).
80                  build();
81      }
82  
83      @Override
84      public int hashCode() {
85          return new HashCodeBuilder().
86                  append(items).
87                  build();
88      }
89  }