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.internal.impl.collect;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.eclipse.aether.RepositorySystemSession;
025import org.eclipse.aether.artifact.ArtifactType;
026import org.eclipse.aether.artifact.ArtifactTypeRegistry;
027
028/**
029 * A short-lived artifact type registry that caches results from a presumably slower type registry.
030 * Internal helper class for collector implementations.
031 */
032public class CachingArtifactTypeRegistry implements ArtifactTypeRegistry {
033
034    private final ArtifactTypeRegistry delegate;
035
036    private final Map<String, ArtifactType> types;
037
038    public static ArtifactTypeRegistry newInstance(RepositorySystemSession session) {
039        return newInstance(session.getArtifactTypeRegistry());
040    }
041
042    public static ArtifactTypeRegistry newInstance(ArtifactTypeRegistry delegate) {
043        return (delegate != null) ? new CachingArtifactTypeRegistry(delegate) : null;
044    }
045
046    private CachingArtifactTypeRegistry(ArtifactTypeRegistry delegate) {
047        this.delegate = delegate;
048        types = new HashMap<>();
049    }
050
051    public ArtifactType get(String typeId) {
052        ArtifactType type = types.get(typeId);
053
054        if (type == null) {
055            type = delegate.get(typeId);
056            types.put(typeId, type);
057        }
058
059        return type;
060    }
061}