001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.resources;
021
022import com.google.common.collect.Maps;
023import org.apache.commons.lang.ArrayUtils;
024import org.sonar.api.BatchComponent;
025import org.sonar.api.ServerComponent;
026
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * A class to store the list of languages
034 *
035 * @since 1.10
036 */
037public class Languages implements BatchComponent, ServerComponent {
038
039  private final Map<String, Language> map = Maps.newHashMap();
040
041  /**
042   * Creates a list of languages
043   */
044  public Languages(Language... languages) {
045    for (Language language : languages) {
046      map.put(language.getKey(), language);
047    }
048  }
049
050  /**
051   * No languages are installed
052   */
053  public Languages() {
054  }
055
056  /**
057   * @param keys the languages keys
058   * @return the list of suffix files associates to languages included in the current object
059   */
060  public String[] getSuffixes(String... keys) {
061    List<String> suffixes = new ArrayList<String>();
062
063    for (Map.Entry<String, Language> entry : map.entrySet()) {
064      if (ArrayUtils.isEmpty(keys) || ArrayUtils.contains(keys, entry.getKey())) {
065        suffixes.addAll(Arrays.asList(entry.getValue().getFileSuffixes()));
066      }
067    }
068    return suffixes.toArray(new String[suffixes.size()]);
069  }
070
071  /**
072   * Return a language from the current object based on its key
073   */
074  public Language get(String key) {
075    return map.get(key);
076  }
077
078  /**
079   * Adds a language to the current object
080   */
081  public void add(Language language) {
082    map.put(language.getKey(), language);
083  }
084}