001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.artifact;
020
021import org.eclipse.aether.artifact.ArtifactType;
022import org.eclipse.aether.artifact.ArtifactTypeRegistry;
023
024/**
025 * An artifact type registry which first consults its own mappings and in case of an unknown type falls back to another
026 * type registry.
027 */
028public class OverlayArtifactTypeRegistry extends SimpleArtifactTypeRegistry {
029
030    private final ArtifactTypeRegistry delegate;
031
032    /**
033     * Creates a new artifact type registry with initially no registered artifact types and the specified fallback
034     * registry. Use {@link #add(ArtifactType)} to populate the registry.
035     *
036     * @param delegate The artifact type registry to fall back to, may be {@code null}.
037     */
038    public OverlayArtifactTypeRegistry(ArtifactTypeRegistry delegate) {
039        this.delegate = delegate;
040    }
041
042    /**
043     * Adds the specified artifact type to the registry.
044     *
045     * @param type The artifact type to add, must not be {@code null}.
046     * @return This registry for chaining, never {@code null}.
047     */
048    @Override
049    public OverlayArtifactTypeRegistry add(ArtifactType type) {
050        super.add(type);
051        return this;
052    }
053
054    @Override
055    public ArtifactType get(String typeId) {
056        ArtifactType type = super.get(typeId);
057
058        if (type == null && delegate != null) {
059            type = delegate.get(typeId);
060        }
061
062        return type;
063    }
064}