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.entity;
20  
21  import com.fasterxml.jackson.annotation.JsonIdentityInfo;
22  import com.fasterxml.jackson.annotation.ObjectIdGenerators;
23  import java.util.Objects;
24  import org.apache.syncope.core.persistence.api.entity.Entity;
25  import org.apache.syncope.core.persistence.api.entity.Implementation;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "key")
30  public abstract class AbstractEntity implements Entity {
31  
32      private static final long serialVersionUID = -9017214159540857901L;
33  
34      protected static final Logger LOG = LoggerFactory.getLogger(AbstractEntity.class);
35  
36      protected void checkType(final Object object, final Class<?> clazz) {
37          if (object != null && !clazz.isInstance(object)) {
38              throw new ClassCastException("Expected " + clazz.getName() + ", got " + object.getClass().getName());
39          }
40      }
41  
42      protected void checkImplementationType(final Implementation object, final String expected) {
43          if (object != null && !object.getType().equals(expected)) {
44              throw new ClassCastException("Expected " + expected + ", got " + object.getType());
45          }
46      }
47  
48      @Override
49      public boolean equals(final Object obj) {
50          if (this == obj) {
51              return true;
52          }
53          if (!(obj instanceof AbstractEntity)) {
54              return false;
55          }
56          AbstractEntity entity = (AbstractEntity) obj;
57          return Objects.equals(getKey(), entity.getKey());
58      }
59  
60      @Override
61      public int hashCode() {
62          return Objects.hash(getKey());
63      }
64  
65      @Override
66      public String toString() {
67          return new StringBuilder().append(getClass().getSimpleName()).
68                  append('[').append(getKey()).append(']').toString();
69      }
70  }