View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.settings;
20  
21  import java.util.AbstractList;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Objects;
25  import java.util.function.Consumer;
26  import java.util.function.Function;
27  import java.util.function.Supplier;
28  
29  class WrapperList<T, U> extends AbstractList<T> {
30      private final Supplier<List<U>> getter;
31      private final Consumer<List<U>> setter;
32      private final Function<U, T> mapper;
33      private final Function<T, U> revMapper;
34  
35      WrapperList(List<U> list, Function<U, T> mapper, Function<T, U> revMapper) {
36          this(() -> list, null, mapper, revMapper);
37      }
38  
39      WrapperList(Supplier<List<U>> getter, Consumer<List<U>> setter, Function<U, T> mapper, Function<T, U> revMapper) {
40          this.getter = getter;
41          this.setter = setter;
42          this.mapper = mapper;
43          this.revMapper = revMapper;
44      }
45  
46      @Override
47      public T get(int index) {
48          return mapper.apply(getter.get().get(index));
49      }
50  
51      @Override
52      public int size() {
53          return getter.get().size();
54      }
55  
56      @Override
57      public boolean add(T t) {
58          Objects.requireNonNull(t);
59          if (setter != null) {
60              List<U> list = new ArrayList<>(getter.get());
61              boolean ret = list.add(revMapper.apply(t));
62              setter.accept(list);
63              return ret;
64          } else {
65              return getter.get().add(revMapper.apply(t));
66          }
67      }
68  
69      @Override
70      public T set(int index, T element) {
71          Objects.requireNonNull(element);
72          if (setter != null) {
73              List<U> list = new ArrayList<>(getter.get());
74              U ret = list.set(index, revMapper.apply(element));
75              setter.accept(list);
76              return mapper.apply(ret);
77          } else {
78              return mapper.apply(getter.get().set(index, revMapper.apply(element)));
79          }
80      }
81  
82      @Override
83      public void add(int index, T element) {
84          Objects.requireNonNull(element);
85          if (setter != null) {
86              List<U> list = new ArrayList<>(getter.get());
87              list.add(index, revMapper.apply(element));
88              setter.accept(list);
89          } else {
90              getter.get().add(index, revMapper.apply(element));
91          }
92      }
93  
94      @Override
95      public T remove(int index) {
96          if (setter != null) {
97              List<U> list = new ArrayList<>(getter.get());
98              U ret = list.remove(index);
99              setter.accept(list);
100             return mapper.apply(ret);
101         } else {
102             return mapper.apply(getter.get().remove(index));
103         }
104     }
105 }