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