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