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.artifact.repository.metadata;
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 }