1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.settings;
20
21 import java.io.ObjectStreamException;
22 import java.io.Serializable;
23 import java.util.AbstractList;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Objects;
27 import java.util.function.Consumer;
28 import java.util.function.Function;
29 import java.util.function.Supplier;
30
31 class WrapperList<T, U> extends AbstractList<T> implements Serializable {
32 private final Supplier<List<U>> getter;
33 private final Consumer<List<U>> setter;
34 private final Function<U, T> mapper;
35 private final Function<T, U> revMapper;
36
37 WrapperList(List<U> list, Function<U, T> mapper, Function<T, U> revMapper) {
38 this(() -> list, null, mapper, revMapper);
39 }
40
41 WrapperList(Supplier<List<U>> getter, Consumer<List<U>> setter, Function<U, T> mapper, Function<T, U> revMapper) {
42 this.getter = getter;
43 this.setter = setter;
44 this.mapper = mapper;
45 this.revMapper = revMapper;
46 }
47
48 @Override
49 public T get(int index) {
50 return mapper.apply(getter.get().get(index));
51 }
52
53 @Override
54 public int size() {
55 return getter.get().size();
56 }
57
58 @Override
59 public boolean add(T t) {
60 Objects.requireNonNull(t);
61 if (setter != null) {
62 List<U> list = new ArrayList<>(getter.get());
63 boolean ret = list.add(revMapper.apply(t));
64 setter.accept(list);
65 return ret;
66 } else {
67 return getter.get().add(revMapper.apply(t));
68 }
69 }
70
71 @Override
72 public T set(int index, T element) {
73 Objects.requireNonNull(element);
74 if (setter != null) {
75 List<U> list = new ArrayList<>(getter.get());
76 U ret = list.set(index, revMapper.apply(element));
77 setter.accept(list);
78 return mapper.apply(ret);
79 } else {
80 return mapper.apply(getter.get().set(index, revMapper.apply(element)));
81 }
82 }
83
84 @Override
85 public void add(int index, T element) {
86 Objects.requireNonNull(element);
87 if (setter != null) {
88 List<U> list = new ArrayList<>(getter.get());
89 list.add(index, revMapper.apply(element));
90 setter.accept(list);
91 } else {
92 getter.get().add(index, revMapper.apply(element));
93 }
94 }
95
96 @Override
97 public T remove(int index) {
98 if (setter != null) {
99 List<U> list = new ArrayList<>(getter.get());
100 U ret = list.remove(index);
101 setter.accept(list);
102 return mapper.apply(ret);
103 } else {
104 return mapper.apply(getter.get().remove(index));
105 }
106 }
107
108 private Object writeReplace() throws ObjectStreamException {
109 return new ArrayList<T>(this);
110 }
111 }