All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.cling.invoker.ProtoLookup Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.maven.cling.invoker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.api.services.Lookup;
import org.apache.maven.api.services.LookupException;

import static java.util.Objects.requireNonNull;

/**
 * Proto-{@link Lookup} offer ways to provide early components to invoker.
 */
public class ProtoLookup implements Lookup {
    private final Map, Object> components;

    private ProtoLookup(Map, Object> components) {
        this.components = components;
    }

    @Override
    public  T lookup(Class type) {
        Optional optional = lookupOptional(type);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            throw new LookupException("No mapping for key: " + type.getName());
        }
    }

    @Override
    public  T lookup(Class type, String name) {
        return lookup(type);
    }

    @Override
    public  Optional lookupOptional(Class type) {
        return Optional.ofNullable(type.cast(components.get(type)));
    }

    @Override
    public  Optional lookupOptional(Class type, String name) {
        return lookupOptional(type);
    }

    @Override
    public  List lookupList(Class type) {
        return List.of();
    }

    @Override
    public  Map lookupMap(Class type) {
        return Map.of();
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private final Map, Object> components = new HashMap<>();

        public ProtoLookup build() {
            return new ProtoLookup(components);
        }

        public  Builder addMapping(Class type, T component) {
            requireNonNull(type, "type");
            requireNonNull(component, "component");
            if (components.put(type, component) != null) {
                throw new IllegalStateException("Duplicate mapping for type: " + type.getName());
            }
            return this;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy