View Javadoc
1   package org.apache.maven.internal.impl;
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.AbstractMap;
23  import java.util.AbstractSet;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.NoSuchElementException;
27  import java.util.Set;
28  
29  class PropertiesAsMap extends AbstractMap<String, String>
30  {
31  
32      private final Map<Object, Object> properties;
33  
34      PropertiesAsMap( Map<Object, Object> properties )
35      {
36          this.properties = properties;
37      }
38  
39      @Override
40      public Set<Entry<String, String>> entrySet()
41      {
42          return new AbstractSet<Entry<String, String>>()
43          {
44              @Override
45              public Iterator<Entry<String, String>> iterator()
46              {
47                  Iterator<Entry<Object, Object>> iterator = properties.entrySet().iterator();
48                  return new Iterator<Entry<String, String>>()
49                  {
50                      Entry<Object, Object> next;
51  
52                      @Override
53                      public boolean hasNext()
54                      {
55                          while ( next == null && iterator.hasNext() )
56                          {
57                              Entry<Object, Object> e = iterator.next();
58                              if ( PropertiesAsMap.matches( e ) )
59                              {
60                                  next = e;
61                                  break;
62                              }
63                          }
64                          return next != null;
65                      }
66  
67                      @Override
68                      public Entry<String, String> next()
69                      {
70                          if ( next == null )
71                          {
72                              throw new NoSuchElementException();
73                          }
74                          return new Entry<String, String>()
75                          {
76                              @Override
77                              public String getKey()
78                              {
79                                  return (String) next.getKey();
80                              }
81  
82                              @Override
83                              public String getValue()
84                              {
85                                  return (String) next.getValue();
86                              }
87  
88                              @Override
89                              public String setValue( String value )
90                              {
91                                  return (String) next.setValue( value );
92                              }
93                          };
94                      }
95                  };
96              }
97  
98              @Override
99              public int size()
100             {
101                 return (int) properties.entrySet()
102                         .stream().filter( PropertiesAsMap::matches )
103                         .count();
104             }
105         };
106     }
107 
108     private static boolean matches( Entry<Object, Object> entry )
109     {
110         return entry.getKey() instanceof String
111                 && entry.getValue() instanceof String;
112     }
113 }