equals(Object)   F
last analyzed

Complexity

Conditions 22

Size

Total Lines 40
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 22
c 1
b 0
f 0
dl 0
loc 40
rs 0
eloc 40

How to fix   Complexity   

Complexity

Complex classes like unicon.matthews.oneroster.service.repository.MongoEnrollment.equals(Object) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/**
2
 * 
3
 */
4
package unicon.matthews.oneroster.service.repository;
5
6
import org.apache.commons.lang3.builder.ToStringBuilder;
7
import org.apache.commons.lang3.builder.ToStringStyle;
8
import org.springframework.data.annotation.Id;
9
import org.springframework.data.mongodb.core.mapping.Document;
10
11
import unicon.matthews.oneroster.Enrollment;
12
13
/**
14
 * @author ggilbert
15
 *
16
 */
17
@Document
18
public class MongoEnrollment {
19
  @Id private String id;
20
  private Enrollment enrollment;
21
  private String tenantId;
22
  private String orgId;
23
  private String userSourcedId;
24
  private String classSourcedId;
25
  
26
  private MongoEnrollment() {}
27
  
28
  public String getId() {
29
    return id;
30
  }
31
32
  public Enrollment getEnrollment() {
33
    return enrollment;
34
  }
35
36
  public String getTenantId() {
37
    return tenantId;
38
  }
39
40
  public String getOrgId() {
41
    return orgId;
42
  }
43
44
  public String getUserSourcedId() {
45
    return userSourcedId;
46
  }
47
48
  public String getClassSourcedId() {
49
    return classSourcedId;
50
  }
51
52
  @Override
53
  public String toString() {
54
    return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
55
  }
56
57
  @Override
58
  public int hashCode() {
59
    final int prime = 31;
60
    int result = 1;
61
    result = prime * result + ((classSourcedId == null) ? 0 : classSourcedId.hashCode());
62
    result = prime * result + ((enrollment == null) ? 0 : enrollment.hashCode());
63
    result = prime * result + ((id == null) ? 0 : id.hashCode());
64
    result = prime * result + ((orgId == null) ? 0 : orgId.hashCode());
65
    result = prime * result + ((tenantId == null) ? 0 : tenantId.hashCode());
66
    result = prime * result + ((userSourcedId == null) ? 0 : userSourcedId.hashCode());
67
    return result;
68
  }
69
70
  @Override
71
  public boolean equals(Object obj) {
72
    if (this == obj)
73
      return true;
74
    if (obj == null)
75
      return false;
76
    if (getClass() != obj.getClass())
77
      return false;
78
    MongoEnrollment other = (MongoEnrollment) obj;
79
    if (classSourcedId == null) {
80
      if (other.classSourcedId != null)
81
        return false;
82
    } else if (!classSourcedId.equals(other.classSourcedId))
83
      return false;
84
    if (enrollment == null) {
85
      if (other.enrollment != null)
86
        return false;
87
    } else if (!enrollment.equals(other.enrollment))
88
      return false;
89
    if (id == null) {
90
      if (other.id != null)
91
        return false;
92
    } else if (!id.equals(other.id))
93
      return false;
94
    if (orgId == null) {
95
      if (other.orgId != null)
96
        return false;
97
    } else if (!orgId.equals(other.orgId))
98
      return false;
99
    if (tenantId == null) {
100
      if (other.tenantId != null)
101
        return false;
102
    } else if (!tenantId.equals(other.tenantId))
103
      return false;
104
    if (userSourcedId == null) {
105
      if (other.userSourcedId != null)
106
        return false;
107
    } else if (!userSourcedId.equals(other.userSourcedId))
108
      return false;
109
    return true;
110
  }
111
112
  public static class Builder {
113
    private MongoEnrollment _mongoEnrollment = new MongoEnrollment();
114
    
115
    public Builder withUserSourcedId(String userSourcedId) {
116
      _mongoEnrollment.userSourcedId = userSourcedId;
117
      return this;
118
    }
119
    
120
    public Builder withClassSourcedId(String classSourcedId) {
121
      _mongoEnrollment.classSourcedId = classSourcedId;
122
      return this;
123
    }
124
    
125
    public Builder withEnrollment(Enrollment enrollment) {
126
      _mongoEnrollment.enrollment = enrollment;
127
      return this;
128
    }
129
    
130
    public Builder withTenantId(String tenantId) {
131
      _mongoEnrollment.tenantId = tenantId;
132
      return this;
133
    }
134
    
135
    public Builder withOrgId(String orgId) {
136
      _mongoEnrollment.orgId = orgId;
137
      return this;
138
    }
139
    
140
    public Builder withId(String id) {
141
      _mongoEnrollment.id = id;
142
      return this;
143
    }
144
    
145
    public MongoEnrollment build() {
146
      return _mongoEnrollment;
147
    }
148
  }
149
150
151
}
152