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 final 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    public OverlayArtifactTypeRegistry add(ArtifactType type) {
049        super.add(type);
050        return this;
051    }
052
053    public ArtifactType get(String typeId) {
054        ArtifactType type = super.get(typeId);
055
056        if (type == null && delegate != null) {
057            type = delegate.get(typeId);
058        }
059
060        return type;
061    }
062}