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