Bug 3302: fix for GroupTable
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / equivalence / BucketsEquivalence.java
1 /*
2  * Copyright (c) 2015 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.ofoverlay.equivalence;
10
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14
15 import com.google.common.base.Equivalence;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
18
19 /**
20  * Custom Equivalence for {@link Buckets}
21  *
22  * @see GroupEquivalence
23  */
24 public class BucketsEquivalence extends Equivalence<Buckets> {
25
26     BucketsEquivalence() {
27     }
28
29     @Override
30     protected boolean doEquivalent(Buckets a, Buckets b) {
31
32         Set<Bucket> setA = new HashSet<>();
33         Set<Bucket> setB = new HashSet<>();
34         if (a.getBucket() != null) {
35             setA = new HashSet<>(a.getBucket());
36         }
37         if (b.getBucket() != null) {
38             setB = new HashSet<>(b.getBucket());
39         }
40         return setA.equals(setB);
41     }
42
43     @Override
44     protected int doHash(Buckets buckets) {
45
46         final int prime = 31;
47         int result = 1;
48         List<Bucket> bucketList = buckets.getBucket();
49         Set<Bucket> bucketSet = new HashSet<>();
50         if (bucketList != null) {
51             bucketSet = new HashSet<>(bucketList);
52         }
53         result = prime * result + bucketSet.hashCode();
54
55         return result;
56     }
57
58 }