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