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 package org.sonar.batch.scan2;
021
022 import com.google.common.base.Preconditions;
023 import org.sonar.api.BatchComponent;
024 import org.sonar.api.batch.measure.MetricFinder;
025 import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
026 import org.sonar.batch.index.Cache;
027 import org.sonar.batch.index.Cache.Entry;
028 import org.sonar.batch.index.Caches;
029
030 /**
031 * Cache of all measures. This cache is shared amongst all project modules.
032 */
033 public class AnalyzerMeasureCache implements BatchComponent {
034
035 // project key -> component key -> metric key -> measure
036 private final Cache<DefaultMeasure> cache;
037
038 public AnalyzerMeasureCache(Caches caches, MetricFinder metricFinder) {
039 caches.registerValueCoder(DefaultMeasure.class, new DefaultMeasureValueCoder(metricFinder));
040 cache = caches.createCache("measures");
041 }
042
043 public Iterable<Entry<DefaultMeasure>> entries() {
044 return cache.entries();
045 }
046
047 public Iterable<DefaultMeasure> byModule(String projectKey) {
048 return cache.values(projectKey);
049 }
050
051 public DefaultMeasure<?> byMetric(String projectKey, String resourceKey, String metricKey) {
052 return cache.get(projectKey, resourceKey, metricKey);
053 }
054
055 public AnalyzerMeasureCache put(String projectKey, String resourceKey, DefaultMeasure<?> measure) {
056 Preconditions.checkNotNull(projectKey);
057 Preconditions.checkNotNull(resourceKey);
058 Preconditions.checkNotNull(measure);
059 cache.put(projectKey, resourceKey, measure.metric().key(), measure);
060 return this;
061 }
062
063 public boolean contains(String projectKey, String resourceKey, DefaultMeasure<?> measure) {
064 Preconditions.checkNotNull(projectKey);
065 Preconditions.checkNotNull(resourceKey);
066 Preconditions.checkNotNull(measure);
067 return cache.containsKey(projectKey, resourceKey, measure.metric().key());
068 }
069
070 public Iterable<DefaultMeasure> all() {
071 return cache.values();
072 }
073
074 }