Moved util methods from PolicyResolver
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / resolver / EgKey.java
1 package org.opendaylight.groupbasedpolicy.resolver;
2
3 import javax.annotation.concurrent.Immutable;
4
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
6 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
7
8 import com.google.common.collect.ComparisonChain;
9 import com.google.common.collect.Ordering;
10
11 /**
12  * A tuple referencing an endpoint group and its enclosing tenant
13  * @author readams
14  */
15 @Immutable
16 public class EgKey implements Comparable<EgKey> {
17     private final TenantId tenantId;
18     private final EndpointGroupId egId;
19     @Override
20     public int hashCode() {
21         final int prime = 31;
22         int result = 1;
23         result = prime * result + ((egId == null) ? 0 : egId.hashCode());
24         result = prime * result +
25                  ((tenantId == null) ? 0 : tenantId.hashCode());
26         return result;
27     }
28     @Override
29     public boolean equals(Object obj) {
30         if (this == obj)
31             return true;
32         if (obj == null)
33             return false;
34         if (getClass() != obj.getClass())
35             return false;
36         EgKey other = (EgKey) obj;
37         if (egId == null) {
38             if (other.egId != null)
39                 return false;
40         } else if (!egId.equals(other.egId))
41             return false;
42         if (tenantId == null) {
43             if (other.tenantId != null)
44                 return false;
45         } else if (!tenantId.equals(other.tenantId))
46             return false;
47         return true;
48     }
49     public EgKey(TenantId tenantId, EndpointGroupId egId) {
50         super();
51         this.tenantId = tenantId;
52         this.egId = egId;
53     }
54
55     @Override
56     public int compareTo(EgKey o) {
57         String tid = null;
58         if (tenantId != null) tid = tenantId.getValue();
59         String otid = null;
60         if (o.tenantId != null) otid = o.tenantId.getValue();
61         String egid = null;
62         if (egId != null) tid = egId.getValue();
63         String oegid = null;
64         if (o.egId != null) oegid = o.egId.getValue();
65         return ComparisonChain.start()
66             .compare(tid, otid, 
67                      Ordering.natural().nullsLast())
68             .compare(egid, oegid, 
69                      Ordering.natural().nullsLast())
70             .result();
71     }
72
73     @Override
74     public String toString() {
75         return "EgKey [tenantId=" + tenantId + ", egId=" + egId + "]";
76     }
77     public TenantId getTenantId() {
78         return tenantId;
79     }
80     public EndpointGroupId getEgId() {
81         return egId;
82     }
83 }