001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.batch.indexer;
021
022 import com.google.common.collect.ArrayListMultimap;
023 import com.google.common.collect.ListMultimap;
024 import com.google.common.collect.Lists;
025 import org.sonar.api.database.model.Snapshot;
026 import org.sonar.api.measures.Measure;
027 import org.sonar.api.measures.MeasuresFilter;
028 import org.sonar.api.measures.MeasuresFilters;
029 import org.sonar.api.resources.Project;
030 import org.sonar.api.resources.Resource;
031
032 import java.util.Collection;
033 import java.util.Collections;
034 import java.util.List;
035
036 public class Bucket<RESOURCE extends Resource> {
037
038 private RESOURCE resource;
039 private Snapshot snapshot;
040 private ListMultimap<String, Measure> measuresByMetric = ArrayListMultimap.create();
041 private boolean sourceSaved = false;
042 private Bucket<Project> project;
043 private Bucket<?> parent;
044 private List<Bucket<?>> children;
045
046 public Bucket(RESOURCE resource) {
047 this.resource = resource;
048 }
049
050 public RESOURCE getResource() {
051 return resource;
052 }
053
054 public Bucket<Project> getProject() {
055 return project;
056 }
057
058 public Bucket<RESOURCE> setProject(Bucket<Project> project) {
059 this.project = project;
060 return this;
061 }
062
063 public Snapshot getSnapshot() {
064 return snapshot;
065 }
066
067 public Integer getSnapshotId() {
068 if (snapshot != null) {
069 return snapshot.getId();
070 }
071 return null;
072 }
073
074 public void setSnapshot(Snapshot snapshot) {
075 this.snapshot = snapshot;
076 }
077
078 public void setParent(Bucket parent) {
079 this.parent = parent;
080 if (parent != null) {
081 parent.addChild(this);
082 }
083 }
084
085 private void addChild(Bucket bucket) {
086 if (children == null) {
087 children = Lists.newArrayList();
088 }
089 children.add(bucket);
090 }
091
092 private void removeChild(Bucket bucket) {
093 if (children != null) {
094 children.remove(bucket);
095 }
096 }
097
098 public List<Bucket<?>> getChildren() {
099 if (children == null) {
100 return Collections.emptyList();
101 }
102 return children;
103 }
104
105 public Bucket getParent() {
106 return parent;
107 }
108
109 public void addMeasure(Measure measure) {
110 measuresByMetric.put(measure.getMetric().getKey(), measure);
111 }
112
113 public boolean isSourceSaved() {
114 return sourceSaved;
115 }
116
117 public void setSourceSaved(boolean b) {
118 this.sourceSaved = b;
119 }
120
121 public void clear() {
122 measuresByMetric = null;
123 children = null;
124 if (parent != null) {
125 parent.removeChild(this);
126 }
127 }
128
129 public boolean isExcluded() {
130 return resource.isExcluded();
131 }
132
133 public boolean isPersisted() {
134 return resource.getId() != null;
135 }
136
137 public Integer getResourceId() {
138 return resource.getId();
139 }
140
141 public <M> M getMeasures(final MeasuresFilter<M> filter) {
142 Collection<Measure> unfiltered;
143 if (filter instanceof MeasuresFilters.MetricFilter) {
144 unfiltered = measuresByMetric.get(((MeasuresFilters.MetricFilter) filter).filterOnMetric().getKey());
145 } else {
146 unfiltered = measuresByMetric.values();
147 }
148 return filter.filter(unfiltered);
149 }
150 }