OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.change;
15
 
16
import java.beans.PropertyChangeEvent;
17
import java.util.Collection;
18
import java.util.List;
19
 
20
import org.apache.commons.collections.CollectionUtils;
21
 
22
/**
23
 * Permet de signaler un changement dans une collection, et surtout de voir les éléments
24
 * ajoutés/rétirés/toujours là.
25
 *
26
 * @author ILM Informatique 1 sept. 2004
27
 */
28
public class CollectionChangeEvent extends PropertyChangeEvent {
29
 
30
    /**
31
     * Crée un evenement avec les propriétés spécifiées. Ne pas oublier de cloner/dupliquer la
32
     * collection avant de la changer, pour pouvoir renseigner oldVal.
33
     *
34
     * @param src la source.
35
     * @param propName le nom de la propriété.
36
     * @param oldVal the old value, must not be <code>null</code>.
37
     * @param newVal the new value, must not be <code>null</code>.
38
     * @return un nouveau PropertyChangeEvent.
39
     * @throws NullPointerException if oldVal of newVal is <code>null</code>.
40
     */
132 ilm 41
    static public CollectionChangeEvent create(Object src, String propName, Collection<?> oldVal, Collection<?> newVal) {
17 ilm 42
        if (oldVal == null || newVal == null)
43
            throw new NullPointerException();
44
        return new CollectionChangeEvent(src, propName, oldVal, newVal);
45
    }
46
 
47
    // *** Instance
48
 
49
    // use static
132 ilm 50
    protected CollectionChangeEvent(Object source, String propertyName, Collection<?> oldValue, Collection<?> newValue) {
17 ilm 51
        super(source, propertyName, oldValue, newValue);
52
    }
53
 
177 ilm 54
    @Override
55
    public Collection<?> getOldValue() {
56
        // checked by constructor
57
        return (Collection<?>) super.getOldValue();
58
    }
59
 
60
    @Override
61
    public Collection<?> getNewValue() {
62
        // checked by constructor
63
        return (Collection<?>) super.getNewValue();
64
    }
65
 
17 ilm 66
    public Collection getItemsAdded() {
177 ilm 67
        return CollectionUtils.subtract(this.getNewValue(), this.getOldValue());
17 ilm 68
    }
69
 
70
    public Collection getItemsRemoved() {
177 ilm 71
        return CollectionUtils.subtract(this.getOldValue(), this.getNewValue());
17 ilm 72
    }
73
 
74
    public Collection getItemsNotChanged() {
177 ilm 75
        return CollectionUtils.intersection(this.getNewValue(), this.getOldValue());
17 ilm 76
    }
77
 
78
    /**
79
     * Does the new collection is the old one plus some items.
80
     *
81
     * @return <code>true</code> if no items were removed.
82
     */
132 ilm 83
    public final boolean isOnlyAddition() {
17 ilm 84
        return this.getItemsRemoved().size() == 0;
85
    }
86
 
87
    /**
88
     * Does the new collection is the old one minus some items.
89
     *
90
     * @return <code>true</code> if no items were added.
91
     */
132 ilm 92
    public final boolean isOnlyRemoval() {
17 ilm 93
        return this.getItemsAdded().size() == 0;
94
    }
95
 
96
    /**
97
     * Returns the indexes that have been added to a list. Items at theses indexes in the new list
98
     * were not part of the old one.
99
     *
100
     * @return a List of Integer.
101
     */
132 ilm 102
    public final List<Integer> getIndexesAdded() {
17 ilm 103
        if (!this.isOnlyAddition())
104
            throw new IllegalStateException("items were also removed");
105
        return this.getIndexesChanged();
106
    }
107
 
108
    /**
109
     * Returns the indexes that have been removed from a list. Items at theses indexes in the old
110
     * list aren't part of the new one anymore.
111
     *
112
     * @return a List of Integer.
113
     */
132 ilm 114
    public final List<Integer> getIndexesRemoved() {
17 ilm 115
        if (!this.isOnlyRemoval())
116
            throw new IllegalStateException("items were also added");
117
        return this.getIndexesChanged();
118
    }
119
 
120
    /**
121
     * Returns the list of intervals added.
122
     *
123
     * @return a List of int[2], inclusive.
124
     */
132 ilm 125
    public final List<int[]> getIntervalsAdded() {
17 ilm 126
        return org.openconcerto.utils.CollectionUtils.aggregate(this.getIndexesAdded());
127
    }
128
 
129
    /**
130
     * Returns the list of intervals removed.
131
     *
132
     * @return a List of int[2], inclusive.
133
     */
132 ilm 134
    public final List<int[]> getIntervalsRemoved() {
17 ilm 135
        return org.openconcerto.utils.CollectionUtils.aggregate(this.getIndexesRemoved());
136
    }
137
 
132 ilm 138
    private final List<Integer> getIndexesChanged() {
17 ilm 139
        if (!(this.getNewValue() instanceof List))
140
            throw new IllegalStateException("the values must be List");
141
        return org.openconcerto.utils.CollectionUtils.getIndexesChanged((List) this.getOldValue(), (List) this.getNewValue());
142
    }
143
 
144
}