OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 21... Line 21...
21
import java.util.Collections;
21
import java.util.Collections;
22
import java.util.List;
22
import java.util.List;
23
 
23
 
24
public abstract class ListChangeIndex<T> implements ListChange<T> {
24
public abstract class ListChangeIndex<T> implements ListChange<T> {
25
 
25
 
26
    protected final static <T> Collection<T> copy(Collection<T> col) {
26
    protected final static <T> Collection<T> copy(final Collection<T> col) {
27
        Collection<T> res;
27
        Collection<T> res;
28
        try {
28
        try {
29
            // tries to keep the same class
29
            // tries to keep the same class
30
            res = CopyUtils.copy(col);
30
            res = CopyUtils.copy(col);
31
        } catch (RuntimeException e) {
31
        } catch (final RuntimeException e) {
32
            // but this doesn't always work (see sublist())
32
            // but this doesn't always work (see sublist())
33
            // so just use a plain ArrayList
33
            // so just use a plain ArrayList
34
            res = new ArrayList<T>(col);
34
            res = new ArrayList<T>(col);
35
        }
35
        }
36
        return res;
36
        return res;
37
    }
37
    }
38
 
38
 
39
    private final int index0;
39
    private final int index0;
40
    private final int index1;
40
    private final int index1;
41
 
41
 
42
    public ListChangeIndex(int index0, int index1) {
42
    public ListChangeIndex(final int index0, final int index1) {
43
        super();
43
        super();
44
        this.index0 = index0;
44
        this.index0 = index0;
45
        this.index1 = index1;
45
        this.index1 = index1;
46
    }
46
    }
47
 
47
 
Line 59... Line 59...
59
 
59
 
60
    public static class Rm<T> extends ListChangeIndex<T> {
60
    public static class Rm<T> extends ListChangeIndex<T> {
61
 
61
 
62
        private final List<T> removed;
62
        private final List<T> removed;
63
 
63
 
64
        public Rm(int index0, int index1, final List<T> removed) {
64
        public Rm(final int index0, final int index1, final List<T> removed) {
65
            super(index0, index1);
65
            super(index0, index1);
66
            // ok to cast : either it copies it : List<T>, either it uses an arrayList which
66
            // ok to cast : either it copies it : List<T>, either it uses an arrayList which
67
            // implements List<T>
67
            // implements List<T>
68
            this.removed = (List<T>) copy(removed);
68
            this.removed = (List<T>) copy(removed);
69
        }
69
        }
70
 
70
 
-
 
71
        @Override
71
        public <U> void apply(List<U> l, ITransformer<T, U> transf) {
72
        public <U> void apply(final List<U> l, final ITransformer<T, U> transf) {
72
            // sublist exclusive
73
            // sublist exclusive
73
            l.subList(this.getIndex0(), this.getIndex1() + 1).clear();
74
            l.subList(this.getIndex0(), this.getIndex1() + 1).clear();
74
        }
75
        }
75
 
76
 
-
 
77
        @Override
76
        public List<? extends T> getItemsAdded() {
78
        public List<? extends T> getItemsAdded() {
77
            return Collections.emptyList();
79
            return Collections.emptyList();
78
        }
80
        }
79
 
81
 
-
 
82
        @Override
80
        public List<T> getItemsRemoved() {
83
        public List<T> getItemsRemoved() {
81
            return this.removed;
84
            return this.removed;
82
        }
85
        }
83
 
86
 
84
        @Override
87
        @Override
Line 89... Line 92...
89
 
92
 
90
    public static class Add<T> extends ListChangeIndex<T> {
93
    public static class Add<T> extends ListChangeIndex<T> {
91
 
94
 
92
        private final Collection<? extends T> added;
95
        private final Collection<? extends T> added;
93
 
96
 
94
        public Add(int index0, Collection<? extends T> added) {
97
        public Add(final int index0, final Collection<? extends T> added) {
95
            super(index0, index0);
98
            super(index0, index0);
96
            this.added = copy(added);
99
            this.added = copy(added);
97
        }
100
        }
98
 
101
 
-
 
102
        @Override
99
        public <U> void apply(List<U> l, ITransformer<T, U> transf) {
103
        public <U> void apply(final List<U> l, final ITransformer<T, U> transf) {
100
            final List<U> toAdd = new ArrayList<U>();
104
            final List<U> toAdd = new ArrayList<U>();
101
            for (final T t : this.added) {
105
            for (final T t : this.added) {
102
                toAdd.add(transf.transformChecked(t));
106
                toAdd.add(transf.transformChecked(t));
103
            }
107
            }
104
            l.addAll(this.getIndex0(), toAdd);
108
            l.addAll(this.getIndex0(), toAdd);
105
        }
109
        }
106
 
110
 
-
 
111
        @Override
107
        public Collection<? extends T> getItemsAdded() {
112
        public Collection<? extends T> getItemsAdded() {
108
            return this.added;
113
            return this.added;
109
        }
114
        }
110
 
115
 
-
 
116
        @Override
111
        public List<? extends T> getItemsRemoved() {
117
        public List<? extends T> getItemsRemoved() {
112
            return Collections.emptyList();
118
            return Collections.emptyList();
113
        }
119
        }
114
 
120
 
115
        @Override
121
        @Override
Line 121... Line 127...
121
    public static class Set<T> extends ListChangeIndex<T> {
127
    public static class Set<T> extends ListChangeIndex<T> {
122
 
128
 
123
        private final T removed;
129
        private final T removed;
124
        private final T added;
130
        private final T added;
125
 
131
 
126
        public Set(int index, T removed, T added) {
132
        public Set(final int index, final T removed, final T added) {
127
            super(index, index);
133
            super(index, index);
128
            this.removed = removed;
134
            this.removed = removed;
129
            this.added = added;
135
            this.added = added;
130
        }
136
        }
131
 
137
 
-
 
138
        @Override
132
        public <U> void apply(List<U> l, ITransformer<T, U> transf) {
139
        public <U> void apply(final List<U> l, final ITransformer<T, U> transf) {
133
            l.set(this.getIndex0(), transf.transformChecked(this.added));
140
            l.set(this.getIndex0(), transf.transformChecked(this.added));
134
        }
141
        }
135
 
142
 
-
 
143
        @Override
136
        public List<? extends T> getItemsAdded() {
144
        public List<? extends T> getItemsAdded() {
137
            return Collections.singletonList(this.added);
145
            return Collections.singletonList(this.added);
138
        }
146
        }
139
 
147
 
-
 
148
        @Override
140
        public List<? extends T> getItemsRemoved() {
149
        public List<? extends T> getItemsRemoved() {
141
            return Collections.singletonList(this.removed);
150
            return Collections.singletonList(this.removed);
142
        }
151
        }
143
 
152
 
144
        @Override
153
        @Override