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.cling.invoker;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Optional;
25  
26  import org.apache.maven.api.services.Lookup;
27  import org.apache.maven.api.services.LookupException;
28  
29  import static java.util.Objects.requireNonNull;
30  
31  /**
32   * Proto-{@link Lookup} offer ways to provide early components to invoker.
33   */
34  public class ProtoLookup implements Lookup {
35      private final Map<Class<?>, Object> components;
36  
37      private ProtoLookup(Map<Class<?>, Object> components) {
38          this.components = components;
39      }
40  
41      @Override
42      public <T> T lookup(Class<T> type) {
43          Optional<T> optional = lookupOptional(type);
44          if (optional.isPresent()) {
45              return optional.get();
46          } else {
47              throw new LookupException("No mapping for key: " + type.getName());
48          }
49      }
50  
51      @Override
52      public <T> T lookup(Class<T> type, String name) {
53          return lookup(type);
54      }
55  
56      @Override
57      public <T> Optional<T> lookupOptional(Class<T> type) {
58          return Optional.ofNullable(type.cast(components.get(type)));
59      }
60  
61      @Override
62      public <T> Optional<T> lookupOptional(Class<T> type, String name) {
63          return lookupOptional(type);
64      }
65  
66      @Override
67      public <T> List<T> lookupList(Class<T> type) {
68          return List.of();
69      }
70  
71      @Override
72      public <T> Map<String, T> lookupMap(Class<T> type) {
73          return Map.of();
74      }
75  
76      public static Builder builder() {
77          return new Builder();
78      }
79  
80      public static class Builder {
81          private final Map<Class<?>, Object> components = new HashMap<>();
82  
83          public ProtoLookup build() {
84              return new ProtoLookup(components);
85          }
86  
87          public <T> Builder addMapping(Class<T> type, T component) {
88              requireNonNull(type, "type");
89              requireNonNull(component, "component");
90              if (components.put(type, component) != null) {
91                  throw new IllegalStateException("Duplicate mapping for type: " + type.getName());
92              }
93              return this;
94          }
95      }
96  }