1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }