Fix broken hashCode & equals in FlowEntity related classes
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / matches / MatchIpv4Destination.java
1 /*
2  * Copyright © 2017 Red Hat, Inc. and others.
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 package org.opendaylight.genius.mdsalutil.matches;
9
10 import org.opendaylight.genius.mdsalutil.MDSALUtil;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
15
16 /**
17  * IPv4 destination match.
18  */
19 public class MatchIpv4Destination extends MatchInfoHelper<Ipv4Match, Ipv4MatchBuilder> {
20
21     private final Ipv4Prefix prefix;
22
23     public MatchIpv4Destination(Ipv4Prefix prefix) {
24         this.prefix = prefix;
25     }
26
27     public MatchIpv4Destination(long ip, long mask) {
28         this.prefix = new Ipv4Prefix(MDSALUtil.longToIp(ip, mask));
29     }
30
31     public MatchIpv4Destination(String ip, String mask) {
32         this.prefix = new Ipv4Prefix(ip + "/" + mask);
33     }
34
35     @Override
36     protected void applyValue(MatchBuilder matchBuilder, Ipv4Match value) {
37         matchBuilder.setLayer3Match(value);
38     }
39
40     @Override
41     protected void populateBuilder(Ipv4MatchBuilder builder) {
42         builder.setIpv4Destination(prefix);
43     }
44
45     public Ipv4Prefix getPrefix() {
46         return prefix;
47     }
48
49     @Override
50     public boolean equals(Object other) {
51         if (this == other) {
52             return true;
53         }
54         if (other == null || getClass() != other.getClass()) {
55             return false;
56         }
57         if (!super.equals(other)) {
58             return false;
59         }
60
61         MatchIpv4Destination that = (MatchIpv4Destination) other;
62
63         return prefix != null ? prefix.equals(that.prefix) : that.prefix == null;
64     }
65
66     @Override
67     public int hashCode() {
68         int result = super.hashCode();
69         result = 31 * result + (prefix != null ? prefix.hashCode() : 0);
70         return result;
71     }
72
73     @Override
74     public String toString() {
75         return "MatchIpv4Destination[" + prefix + "]";
76     }
77
78 }