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.List;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.Optional;
25  import java.util.stream.Collectors;
26  import java.util.stream.Stream;
27  
28  import org.apache.maven.api.ExtensibleEnum;
29  import org.apache.maven.api.Language;
30  import org.apache.maven.api.PathScope;
31  import org.apache.maven.api.ProjectScope;
32  import org.apache.maven.api.di.Inject;
33  import org.apache.maven.api.di.Named;
34  import org.apache.maven.api.di.SessionScoped;
35  import org.apache.maven.api.di.Singleton;
36  import org.apache.maven.api.services.ExtensibleEnumRegistry;
37  import org.apache.maven.api.services.LanguageRegistry;
38  import org.apache.maven.api.services.PathScopeRegistry;
39  import org.apache.maven.api.services.ProjectScopeRegistry;
40  import org.apache.maven.api.spi.ExtensibleEnumProvider;
41  import org.apache.maven.api.spi.LanguageProvider;
42  import org.apache.maven.api.spi.PathScopeProvider;
43  import org.apache.maven.api.spi.ProjectScopeProvider;
44  
45  import static org.apache.maven.internal.impl.Utils.nonNull;
46  
47  public class ExtensibleEnumRegistries {
48  
49      @Named
50      @SessionScoped
51      public static class DefaultPathScopeRegistry extends DefaultExtensibleEnumRegistry<PathScope, PathScopeProvider>
52              implements PathScopeRegistry {
53  
54          @Inject
55          public DefaultPathScopeRegistry(List<PathScopeProvider> providers) {
56              super(
57                      providers,
58                      PathScope.MAIN_COMPILE,
59                      PathScope.MAIN_RUNTIME,
60                      PathScope.TEST_COMPILE,
61                      PathScope.TEST_RUNTIME);
62          }
63      }
64  
65      @Named
66      @SessionScoped
67      public static class DefaultProjectScopeRegistry
68              extends DefaultExtensibleEnumRegistry<ProjectScope, ProjectScopeProvider> implements ProjectScopeRegistry {
69  
70          @Inject
71          public DefaultProjectScopeRegistry(List<ProjectScopeProvider> providers) {
72              super(providers, ProjectScope.MAIN, ProjectScope.TEST);
73          }
74      }
75  
76      @Named
77      @Singleton
78      public static class DefaultLanguageRegistry extends DefaultExtensibleEnumRegistry<Language, LanguageProvider>
79              implements LanguageRegistry {
80  
81          @Inject
82          public DefaultLanguageRegistry(List<LanguageProvider> providers) {
83              super(providers, Language.NONE, Language.JAVA_FAMILY);
84          }
85      }
86  
87      public abstract static class DefaultExtensibleEnumRegistry<
88                      T extends ExtensibleEnum, P extends ExtensibleEnumProvider<T>>
89              implements ExtensibleEnumRegistry<T> {
90  
91          protected final Map<String, T> values;
92  
93          DefaultExtensibleEnumRegistry(List<P> providers, T... builtinValues) {
94              values = Stream.<T>concat(
95                              Stream.of(builtinValues), providers.stream().flatMap(p -> p.provides().stream()))
96                      .collect(Collectors.toUnmodifiableMap(t -> t.id().toLowerCase(Locale.ROOT), t -> t));
97          }
98  
99          @Override
100         public Optional<T> lookup(String id) {
101             return Optional.ofNullable(values.get(nonNull(id, "id").toLowerCase(Locale.ROOT)));
102         }
103     }
104 }