OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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 org.openconcerto.utils.CopyUtils;
17
import org.openconcerto.utils.cc.ITransformer;
18
 
19
import java.util.ArrayList;
20
import java.util.Collection;
21
import java.util.Collections;
22
import java.util.List;
23
 
24
public abstract class ListChangeIndex<T> implements ListChange<T> {
25
 
182 ilm 26
    protected final static <T> Collection<T> copy(final Collection<T> col) {
17 ilm 27
        Collection<T> res;
28
        try {
29
            // tries to keep the same class
30
            res = CopyUtils.copy(col);
182 ilm 31
        } catch (final RuntimeException e) {
17 ilm 32
            // but this doesn't always work (see sublist())
33
            // so just use a plain ArrayList
34
            res = new ArrayList<T>(col);
35
        }
36
        return res;
37
    }
38
 
39
    private final int index0;
40
    private final int index1;
41
 
182 ilm 42
    public ListChangeIndex(final int index0, final int index1) {
17 ilm 43
        super();
44
        this.index0 = index0;
45
        this.index1 = index1;
46
    }
47
 
48
    protected final int getIndex0() {
49
        return this.index0;
50
    }
51
 
52
    protected final int getIndex1() {
53
        return this.index1;
54
    }
55
 
56
    public abstract Collection<? extends T> getItemsAdded();
57
 
58
    public abstract List<? extends T> getItemsRemoved();
59
 
60
    public static class Rm<T> extends ListChangeIndex<T> {
61
 
62
        private final List<T> removed;
63
 
182 ilm 64
        public Rm(final int index0, final int index1, final List<T> removed) {
17 ilm 65
            super(index0, index1);
66
            // ok to cast : either it copies it : List<T>, either it uses an arrayList which
67
            // implements List<T>
68
            this.removed = (List<T>) copy(removed);
69
        }
70
 
182 ilm 71
        @Override
72
        public <U> void apply(final List<U> l, final ITransformer<T, U> transf) {
17 ilm 73
            // sublist exclusive
74
            l.subList(this.getIndex0(), this.getIndex1() + 1).clear();
75
        }
76
 
182 ilm 77
        @Override
17 ilm 78
        public List<? extends T> getItemsAdded() {
79
            return Collections.emptyList();
80
        }
81
 
182 ilm 82
        @Override
17 ilm 83
        public List<T> getItemsRemoved() {
84
            return this.removed;
85
        }
86
 
87
        @Override
88
        public String toString() {
89
            return "@" + this.getIndex0() + ";" + this.getIndex1() + " removed " + this.getItemsRemoved();
90
        }
91
    }
92
 
93
    public static class Add<T> extends ListChangeIndex<T> {
94
 
95
        private final Collection<? extends T> added;
96
 
182 ilm 97
        public Add(final int index0, final Collection<? extends T> added) {
17 ilm 98
            super(index0, index0);
99
            this.added = copy(added);
100
        }
101
 
182 ilm 102
        @Override
103
        public <U> void apply(final List<U> l, final ITransformer<T, U> transf) {
17 ilm 104
            final List<U> toAdd = new ArrayList<U>();
105
            for (final T t : this.added) {
106
                toAdd.add(transf.transformChecked(t));
107
            }
108
            l.addAll(this.getIndex0(), toAdd);
109
        }
110
 
182 ilm 111
        @Override
17 ilm 112
        public Collection<? extends T> getItemsAdded() {
113
            return this.added;
114
        }
115
 
182 ilm 116
        @Override
17 ilm 117
        public List<? extends T> getItemsRemoved() {
118
            return Collections.emptyList();
119
        }
120
 
121
        @Override
122
        public String toString() {
123
            return "@" + this.getIndex0() + " added " + this.getItemsAdded();
124
        }
125
    }
126
 
127
    public static class Set<T> extends ListChangeIndex<T> {
128
 
129
        private final T removed;
130
        private final T added;
131
 
182 ilm 132
        public Set(final int index, final T removed, final T added) {
17 ilm 133
            super(index, index);
134
            this.removed = removed;
135
            this.added = added;
136
        }
137
 
182 ilm 138
        @Override
139
        public <U> void apply(final List<U> l, final ITransformer<T, U> transf) {
17 ilm 140
            l.set(this.getIndex0(), transf.transformChecked(this.added));
141
        }
142
 
182 ilm 143
        @Override
17 ilm 144
        public List<? extends T> getItemsAdded() {
145
            return Collections.singletonList(this.added);
146
        }
147
 
182 ilm 148
        @Override
17 ilm 149
        public List<? extends T> getItemsRemoved() {
150
            return Collections.singletonList(this.removed);
151
        }
152
 
153
        @Override
154
        public String toString() {
155
            return "@" + this.getIndex0() + " replaced " + this.removed + " by " + this.added;
156
        }
157
    }
158
 
159
}