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.apache.maven.doxia.parser.module;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.Collection;
026import java.util.LinkedHashMap;
027import java.util.Map;
028
029/**
030 * Simple implementation of the ParserModuleManager interface.
031 *
032 * @since 1.6
033 */
034@Singleton
035@Named
036public class DefaultParserModuleManager implements ParserModuleManager {
037    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
038    @Inject
039    private Map<String, ParserModule> parserModules;
040
041    private Collection<ParserModule> parserModulesValues;
042
043    /**
044     * {@inheritDoc}
045     *
046     * @return a {@link java.util.Collection} object.
047     */
048    public Collection<ParserModule> getParserModules() {
049        if (parserModulesValues == null) {
050            Map<Class<?>, ParserModule> parserModulesTmp = new LinkedHashMap<>();
051            for (ParserModule module : parserModules.values()) {
052                parserModulesTmp.put(module.getClass(), module);
053            }
054            parserModulesValues = parserModulesTmp.values();
055        }
056
057        return parserModulesValues;
058    }
059
060    /** {@inheritDoc} */
061    public ParserModule getParserModule(String id) throws ParserModuleNotFoundException {
062        ParserModule parserModule = parserModules.get(id);
063
064        if (parserModule == null) {
065            throw new ParserModuleNotFoundException("Cannot find parser module id '" + id + "'");
066        }
067
068        return parserModule;
069    }
070}