001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 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     */
020    
021    package org.sonar.api.batch.debt.internal;
022    
023    import com.google.common.base.Predicate;
024    import com.google.common.collect.ArrayListMultimap;
025    import com.google.common.collect.Iterables;
026    import com.google.common.collect.Multimap;
027    import org.sonar.api.batch.debt.DebtCharacteristic;
028    import org.sonar.api.batch.debt.DebtModel;
029    
030    import javax.annotation.CheckForNull;
031    
032    import java.util.List;
033    
034    import static com.google.common.collect.Lists.newArrayList;
035    
036    public class DefaultDebtModel implements DebtModel {
037    
038      /**
039       * Sub-characteristics list can be retrieved with the characteristic key
040       * Characteristics list can be retrieved by with the null key
041       */
042      private Multimap<String, DebtCharacteristic> characteristicsByKey;
043    
044      public DefaultDebtModel() {
045        characteristicsByKey = ArrayListMultimap.create();
046      }
047    
048      public DefaultDebtModel addCharacteristic(DebtCharacteristic characteristic) {
049        characteristicsByKey.put(null, characteristic);
050        return this;
051      }
052    
053      public DefaultDebtModel addSubCharacteristic(DebtCharacteristic subCharacteristic, String characteristicKey) {
054        characteristicsByKey.put(characteristicKey, subCharacteristic);
055        return this;
056      }
057    
058      @Override
059      public List<DebtCharacteristic> characteristics() {
060        return newArrayList(characteristicsByKey.get(null));
061      }
062    
063      @Override
064      public List<DebtCharacteristic> subCharacteristics(String characteristicKey) {
065        return newArrayList(characteristicsByKey.get(characteristicKey));
066      }
067    
068      @Override
069      public List<DebtCharacteristic> allCharacteristics() {
070        return newArrayList(characteristicsByKey.values());
071      }
072    
073      @Override
074      @CheckForNull
075      public DebtCharacteristic characteristicByKey(final String key) {
076        return Iterables.find(characteristicsByKey.values(), new Predicate<DebtCharacteristic>() {
077          @Override
078          public boolean apply(DebtCharacteristic input) {
079            return key.equals(input.key());
080          }
081        }, null);
082      }
083    
084      @CheckForNull
085      public DebtCharacteristic characteristicById(final int id) {
086        return Iterables.find(characteristicsByKey.values(), new Predicate<DebtCharacteristic>() {
087          @Override
088          public boolean apply(DebtCharacteristic input) {
089            return id == ((DefaultDebtCharacteristic) input).id();
090          }
091        }, null);
092      }
093    }