1 /* |
|
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package org.graalvm.options; |
|
26 |
|
27 import java.util.Arrays; |
|
28 import java.util.Collections; |
|
29 import java.util.Iterator; |
|
30 import java.util.LinkedHashMap; |
|
31 import java.util.List; |
|
32 import java.util.Map; |
|
33 import java.util.NoSuchElementException; |
|
34 |
|
35 /** |
|
36 * An interface to a set of {@link OptionDescriptor}s. |
|
37 * |
|
38 * @since 1.0 |
|
39 */ |
|
40 public interface OptionDescriptors extends Iterable<OptionDescriptor> { |
|
41 |
|
42 /** |
|
43 * An empty set of option descriptors. |
|
44 * |
|
45 * @since 1.0 |
|
46 */ |
|
47 OptionDescriptors EMPTY = new OptionDescriptors() { |
|
48 |
|
49 public Iterator<OptionDescriptor> iterator() { |
|
50 return Collections.<OptionDescriptor> emptyList().iterator(); |
|
51 } |
|
52 |
|
53 public OptionDescriptor get(String key) { |
|
54 return null; |
|
55 } |
|
56 }; |
|
57 |
|
58 /** |
|
59 * Gets the {@link OptionDescriptor} matching a given option name or {@code null} if this option |
|
60 * descriptor set does not contain a matching option name. |
|
61 * |
|
62 * @since 1.0 |
|
63 */ |
|
64 OptionDescriptor get(String optionName); |
|
65 |
|
66 /** |
|
67 * Creates a union options descriptor out of multiple given descriptors. The operation |
|
68 * descriptors are not checked for duplicate keys. The option descriptors are iterated in |
|
69 * declaration order. |
|
70 * |
|
71 * @since 1.0 |
|
72 */ |
|
73 static OptionDescriptors createUnion(OptionDescriptors... descriptors) { |
|
74 if (descriptors.length == 0) { |
|
75 return EMPTY; |
|
76 } else if (descriptors.length == 1) { |
|
77 return descriptors[0]; |
|
78 } else { |
|
79 return new UnionOptionDescriptors(descriptors); |
|
80 } |
|
81 } |
|
82 |
|
83 /** |
|
84 * {@inheritDoc} |
|
85 * |
|
86 * @since 1.0 |
|
87 */ |
|
88 @Override |
|
89 Iterator<OptionDescriptor> iterator(); |
|
90 |
|
91 /** |
|
92 * Creates an {@link OptionDescriptors} instance from a list. The option descriptors |
|
93 * implementation is backed by a {@link LinkedHashMap} that preserves ordering. |
|
94 * |
|
95 * @since 1.0 |
|
96 */ |
|
97 static OptionDescriptors create(List<OptionDescriptor> descriptors) { |
|
98 if (descriptors == null || descriptors.isEmpty()) { |
|
99 return EMPTY; |
|
100 } |
|
101 return new OptionDescriptorsMap(descriptors); |
|
102 } |
|
103 } |
|
104 |
|
105 class OptionDescriptorsMap implements OptionDescriptors { |
|
106 |
|
107 final Map<String, OptionDescriptor> descriptors = new LinkedHashMap<>(); |
|
108 |
|
109 OptionDescriptorsMap(List<OptionDescriptor> descriptorList) { |
|
110 for (OptionDescriptor descriptor : descriptorList) { |
|
111 descriptors.put(descriptor.getName(), descriptor); |
|
112 } |
|
113 } |
|
114 |
|
115 @Override |
|
116 public OptionDescriptor get(String optionName) { |
|
117 return descriptors.get(optionName); |
|
118 } |
|
119 |
|
120 @Override |
|
121 public Iterator<OptionDescriptor> iterator() { |
|
122 return descriptors.values().iterator(); |
|
123 } |
|
124 |
|
125 } |
|
126 |
|
127 final class UnionOptionDescriptors implements OptionDescriptors { |
|
128 |
|
129 final OptionDescriptors[] descriptorsList; |
|
130 |
|
131 UnionOptionDescriptors(OptionDescriptors[] descriptors) { |
|
132 // defensive copy |
|
133 this.descriptorsList = Arrays.copyOf(descriptors, descriptors.length); |
|
134 } |
|
135 |
|
136 public Iterator<OptionDescriptor> iterator() { |
|
137 return new Iterator<OptionDescriptor>() { |
|
138 |
|
139 Iterator<OptionDescriptor> descriptors = descriptorsList[0].iterator(); |
|
140 int descriptorsIndex = 0; |
|
141 OptionDescriptor next = null; |
|
142 |
|
143 public boolean hasNext() { |
|
144 return fetchNext() != null; |
|
145 } |
|
146 |
|
147 private OptionDescriptor fetchNext() { |
|
148 if (next != null) { |
|
149 return next; |
|
150 } |
|
151 if (descriptors.hasNext()) { |
|
152 next = descriptors.next(); |
|
153 return next; |
|
154 } else if (descriptorsIndex < descriptorsList.length - 1) { |
|
155 descriptorsIndex++; |
|
156 descriptors = descriptorsList[descriptorsIndex].iterator(); |
|
157 return fetchNext(); |
|
158 } else { |
|
159 return null; |
|
160 } |
|
161 } |
|
162 |
|
163 public OptionDescriptor next() { |
|
164 OptionDescriptor fetchedNext = fetchNext(); |
|
165 if (fetchedNext != null) { |
|
166 // consume next |
|
167 this.next = null; |
|
168 return fetchedNext; |
|
169 } else { |
|
170 throw new NoSuchElementException(); |
|
171 } |
|
172 } |
|
173 }; |
|
174 } |
|
175 |
|
176 public OptionDescriptor get(String value) { |
|
177 for (OptionDescriptors descriptors : descriptorsList) { |
|
178 OptionDescriptor descriptor = descriptors.get(value); |
|
179 if (descriptor != null) { |
|
180 return descriptor; |
|
181 } |
|
182 } |
|
183 return null; |
|
184 } |
|
185 |
|
186 } |
|