Fixing compareTo for RendererResolvedPolicy
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / policy / RendererResolvedPolicy.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.policy;
10
11 import javax.annotation.Nonnull;
12 import javax.annotation.concurrent.Immutable;
13
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.EndpointPolicyParticipation;
15
16 import com.google.common.base.Preconditions;
17
18 @Immutable
19 public class RendererResolvedPolicy implements Comparable<RendererResolvedPolicy> {
20
21     private final EndpointPolicyParticipation rendererEndpointParticipation;
22     private final ResolvedRuleGroup ruleGroup;
23
24     public RendererResolvedPolicy(@Nonnull EndpointPolicyParticipation rendererEndpointParticipation,
25             @Nonnull ResolvedRuleGroup ruleGroupInfo) {
26         this.rendererEndpointParticipation = Preconditions.checkNotNull(rendererEndpointParticipation);
27         this.ruleGroup = Preconditions.checkNotNull(ruleGroupInfo);
28     }
29
30     public EndpointPolicyParticipation getRendererEndpointParticipation() {
31         return rendererEndpointParticipation;
32     }
33
34     public ResolvedRuleGroup getRuleGroup() {
35         return ruleGroup;
36     }
37
38     @Override
39     public int compareTo(RendererResolvedPolicy resolvedPolicy) {
40         int comp = ruleGroup.compareTo(resolvedPolicy.getRuleGroup());
41         if (comp == 0 && (rendererEndpointParticipation.getIntValue() != resolvedPolicy
42             .getRendererEndpointParticipation().getIntValue())) {
43             return 1;
44         }
45         return comp;
46     }
47
48     @Override
49     public int hashCode() {
50         final int prime = 31;
51         int result = 1;
52         result = prime * result
53                 + ((rendererEndpointParticipation == null) ? 0 : rendererEndpointParticipation.hashCode());
54         result = prime * result + ((ruleGroup == null) ? 0 : ruleGroup.hashCode());
55         return result;
56     }
57
58     @Override
59     public boolean equals(Object obj) {
60         if (this == obj)
61             return true;
62         if (obj == null)
63             return false;
64         if (getClass() != obj.getClass())
65             return false;
66         RendererResolvedPolicy other = (RendererResolvedPolicy) obj;
67         if (rendererEndpointParticipation != other.rendererEndpointParticipation)
68             return false;
69         if (ruleGroup == null) {
70             if (other.ruleGroup != null)
71                 return false;
72         } else if (!ruleGroup.equals(other.ruleGroup))
73             return false;
74         return true;
75     }
76
77     @Override
78     public String toString() {
79         return "RendererResolvedPolicy [rendererEndpointParticipation=" + rendererEndpointParticipation + ", ruleGroup="
80                 + ruleGroup + "]";
81     }
82
83 }