-
Notifications
You must be signed in to change notification settings - Fork 188
/
PrismContainerArrayList.java
138 lines (114 loc) · 3.68 KB
/
PrismContainerArrayList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism.xjc;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import org.apache.commons.lang.Validate;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Collection;
import java.util.List;
/**
* @author lazyman
*
* Changed to extend AbstractList instead of ArrayList, as some functionality of ArrayList
* (e.g. its optimized Itr class) does not work with class (PrismContainerArrayList), as of Java7.
*
* TODO: account for concurrent structural modifications using modCount property
*/
public abstract class PrismContainerArrayList<T extends Containerable> extends AbstractList<T> implements Serializable {
private PrismContainer<T> container;
// For deserialization
public PrismContainerArrayList() {
}
public PrismContainerArrayList(PrismContainer<T> container) {
Validate.notNull(container);
this.container = container;
}
protected abstract T createItem(PrismContainerValue value);
protected abstract PrismContainerValue getValueFrom(T t);
@Override
public T get(int i) {
testIndex(i);
return createItem(getValues().get(i));
}
private List<PrismContainerValue<T>> getValues() {
return container.getValues();
}
@Override
public int size() {
return getValues().size();
}
private void testIndex(int i) {
if (i < 0 || i >= getValues().size()) {
throw new IndexOutOfBoundsException("Can't get index '" + i
+ "', values size is '" + getValues().size() + "'.");
}
}
@Override
public T remove(int i) {
testIndex(i);
PrismContainerValue value = getValues().get(i);
getValues().remove(i);
return createItem(value);
}
@Override
public boolean removeAll(Collection<?> objects) {
boolean changed = false;
for (Object object : objects) {
if (!changed) {
changed = remove(object);
} else {
remove(object);
}
}
return changed;
}
@Override
public boolean remove(Object o) {
T t = (T) o;
PrismContainerValue value = getValueFrom(t);
return container.remove(value);
}
@Override
public boolean add(T t) {
PrismContainerValue value = getValueFrom(t);
try {
return container.add(value);
} catch (SchemaException ex) {
throw new SystemException(ex.getMessage(), ex);
}
}
@Override
public boolean addAll(Collection<? extends T> ts) {
boolean changed = false;
for (T t : ts) {
if (!changed) {
changed = add(t);
} else {
add(t);
}
}
return changed;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
}