74cee2f4eec8143ee10454679919bc511e6b18e4
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / commands / BaseCommand.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.netvirt.elan.l2gw.ha.commands;
9
10 import java.util.ArrayList;
11 import java.util.Comparator;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15
16 public abstract class BaseCommand<T> {
17
18     /**
19      * Abstract method give diff between two List passed based on comparator.
20      * @param updated Updated List
21      * @param original Origina list to be compared with
22      * @param comparator based on which diff will be returned
23      * @param <U> U extends DataObject
24      * @return List of diff based on comparator
25      */
26     public <U> List<U> diffOf(@Nullable List<U> updated, @Nullable final List<U> original,
27             final Comparator<U> comparator) {
28         if (updated == null) {
29             return new ArrayList<>();
30         }
31         if (original == null) {
32             return new ArrayList<>(updated);
33         }
34
35         List<U> result = new ArrayList<>();
36         for (U ele : updated) {
37             boolean present = false;
38             for (U orig : original) {
39                 if (0 == comparator.compare(ele, orig)) {
40                     present = true;
41                     break;
42                 }
43             }
44             if (!present) {
45                 result.add(ele);
46             }
47         }
48         return result;
49     }
50
51     /**
52      * Abstract method give diff between two List passed.
53      * @param updated Updated List
54      * @param original Origina list to be compared with
55      * @return List of diff based
56      */
57     @Nonnull
58     public List<T> diffOf(List<T> updated, final List<T> original) {
59         if (updated == null) {
60             return new ArrayList<>();
61         }
62         if (original == null) {
63             return new ArrayList<>(updated);
64         }
65         List<T> result = new ArrayList<>();
66         for (T ele : updated) {
67             boolean present = false;
68             for (T orig : original) {
69                 if (areEqual(ele, orig)) {
70                     present = true;
71                     break;
72                 }
73             }
74             if (!present) {
75                 result.add(ele);
76             }
77         }
78         return result;
79     }
80
81     public abstract boolean areEqual(T objA, T objB);
82
83 }