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.request;
20  
21  import org.apache.commons.lang3.builder.EqualsBuilder;
22  import org.apache.commons.lang3.builder.HashCodeBuilder;
23  import org.apache.syncope.common.lib.BaseBean;
24  import org.apache.syncope.common.lib.types.PatchOperation;
25  
26  public abstract class AbstractPatch implements BaseBean {
27  
28      private static final long serialVersionUID = -4729181508529829580L;
29  
30      protected abstract static class Builder<P extends AbstractPatch, B extends Builder<P, B>> {
31  
32          protected P instance;
33  
34          protected abstract P newInstance();
35  
36          protected P getInstance() {
37              if (instance == null) {
38                  instance = newInstance();
39              }
40              return instance;
41          }
42  
43          @SuppressWarnings("unchecked")
44          public B operation(final PatchOperation operation) {
45              getInstance().setOperation(operation);
46              return (B) this;
47          }
48  
49          public P build() {
50              if (getInstance().getOperation() == null) {
51                  instance.setOperation(PatchOperation.ADD_REPLACE);
52              }
53              return getInstance();
54          }
55      }
56  
57      private PatchOperation operation;
58  
59      public PatchOperation getOperation() {
60          return operation;
61      }
62  
63      public void setOperation(final PatchOperation operation) {
64          this.operation = operation;
65      }
66  
67      @Override
68      public int hashCode() {
69          return new HashCodeBuilder().
70                  append(operation).
71                  build();
72      }
73  
74      @Override
75      public boolean equals(final Object obj) {
76          if (this == obj) {
77              return true;
78          }
79          if (obj == null) {
80              return false;
81          }
82          if (getClass() != obj.getClass()) {
83              return false;
84          }
85          final AbstractPatch other = (AbstractPatch) obj;
86          return new EqualsBuilder().
87                  append(operation, other.operation).
88                  build();
89      }
90  }