change so that initialization state verification is responsability of ComponentProvider implementation, not user of ComponentProvider
add NoComponentProvider implementation so that componentProvider property can be never null
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.computation.component;
+
+import static com.google.common.base.Preconditions.checkState;
+
+abstract class AbstractComponentProvider implements ComponentProvider {
+ private boolean initialized = false;
+
+ @Override
+ public void ensureInitialized() {
+ if (!this.initialized) {
+ ensureInitializedImpl();
+ this.initialized = true;
+ }
+ }
+
+ protected abstract void ensureInitializedImpl();
+
+ @Override
+ public void reset() {
+ resetImpl();
+ this.initialized = false;
+ }
+
+ protected abstract void resetImpl();
+
+ @Override
+ public Component getByRef(int componentRef) {
+ checkState(this.initialized, "%s has not been initialized", getClass().getSimpleName());
+ return getByRefImpl(componentRef);
+ }
+
+ protected abstract Component getByRefImpl(int componentRef);
+}
package org.sonar.server.computation.component;
public interface ComponentProvider {
- void init();
+ /**
+ * does nothing if already initialized
+ */
+ void ensureInitialized();
void reset();
/**
* @throws IllegalStateException if no component is found for the specified ref
+ * @throws IllegalStateException if provider has not been initialized
*/
Component getByRef(int componentRef);
}
package org.sonar.server.computation.component;
import com.google.common.base.Function;
-import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.junit.rules.ExternalResource;
import org.sonar.server.computation.batch.TreeRootHolderRule;
-import static com.google.common.base.Preconditions.checkState;
-
/**
* Implementation of {@link DbIdsRepository} as a JUnit {@link org.junit.Rule} which supports both
* {@link ViewsComponent} and {@link ReportComponent}.
*/
public class MutableDbIdsRepositoryRule extends ExternalResource implements MutableDbIdsRepository {
- @CheckForNull
private final ComponentProvider componentProvider;
- private boolean providerInitialized = false;
private MutableDbIdsRepository delegate = newDelegate();
- private MutableDbIdsRepositoryRule(@Nullable ComponentProvider componentProvider) {
+ private MutableDbIdsRepositoryRule(ComponentProvider componentProvider) {
this.componentProvider = componentProvider;
}
public static MutableDbIdsRepositoryRule standalone() {
- return new MutableDbIdsRepositoryRule(null);
+ return new MutableDbIdsRepositoryRule(NoComponentProvider.INSTANCE);
}
public static MutableDbIdsRepositoryRule create(TreeRootHolderRule treeRootHolder) {
@Override
protected void before() throws Throwable {
this.delegate = newDelegate();
- if (this.componentProvider != null) {
- this.providerInitialized = false;
- }
}
public DbIdsRepository setComponentId(int componentRef, long componentId) {
- checkAndInitProvider();
+ this.componentProvider.ensureInitialized();
return delegate.setComponentId(componentProvider.getByRef(componentRef), componentId);
}
public DbIdsRepository setSnapshotId(int componentRef, long snapshotId) {
- checkAndInitProvider();
+ this.componentProvider.ensureInitialized();
return delegate.setSnapshotId(componentProvider.getByRef(componentRef), snapshotId);
}
- private void checkAndInitProvider() {
- checkState(this.componentProvider != null, "Can not use methods set taking a ref if no ComponentProvider has been set");
- if (!this.providerInitialized) {
- this.componentProvider.init();
- this.providerInitialized = true;
- }
- }
-
@Override
public DbIdsRepository setComponentId(Component component, long componentId) {
return delegate.setComponentId(component, componentId);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.computation.component;
+
+public enum NoComponentProvider implements ComponentProvider {
+ INSTANCE;
+
+ private static final String ERROR_MSG = "Can not add a measure by Component ref if MeasureRepositoryRule has not been created for some Component provider";
+
+ @Override
+ public void ensureInitialized() {
+ throw new IllegalStateException(ERROR_MSG);
+ }
+
+ @Override
+ public void reset() {
+ // do nothing
+ }
+
+ @Override
+ public Component getByRef(int componentRef) {
+ throw new IllegalStateException(ERROR_MSG);
+ }
+}
import static com.google.common.base.Preconditions.checkState;
-public final class TreeComponentProvider implements ComponentProvider {
+public final class TreeComponentProvider extends AbstractComponentProvider {
+ private final Component root;
private final Map<String, Component> componentsByRef = new HashMap<>();
public TreeComponentProvider(Component root) {
+ this.root = root;
+ ensureInitialized();
+ }
+
+ private static String getRef(Component component) {
+ return component.getType().isReportType() ? String.valueOf(component.getReportAttributes().getRef()) : component.getKey();
+ }
+
+ @Override
+ protected void ensureInitializedImpl() {
new DepthTraversalTypeAwareCrawler(
new TypeAwareVisitorAdapter(CrawlerDepthLimit.LEAVES, ComponentVisitor.Order.PRE_ORDER) {
@Override
}).visit(root);
}
- private static String getRef(Component component) {
- return component.getType().isReportType() ? String.valueOf(component.getReportAttributes().getRef()) : component.getKey();
- }
-
- @Override
- public void init() {
- // nothing to do, init done in constructor
- }
-
@Override
- public void reset() {
+ protected void resetImpl() {
// we can not reset
}
@Override
- public Component getByRef(int componentRef) {
+ protected Component getByRefImpl(int componentRef) {
Component component = componentsByRef.get(String.valueOf(componentRef));
checkState(component != null, "Can not find Component for ref " + componentRef);
return component;
*/
package org.sonar.server.computation.component;
-public final class TreeRootHolderComponentProvider implements ComponentProvider {
+public final class TreeRootHolderComponentProvider extends AbstractComponentProvider {
private final TreeRootHolder treeRootHolder;
private TreeComponentProvider delegate;
}
@Override
- public void init() {
+ protected void ensureInitializedImpl() {
if (this.delegate == null) {
this.delegate = new TreeComponentProvider(treeRootHolder.getRoot());
- this.delegate.init();
+ this.delegate.ensureInitialized();
}
}
@Override
- public void reset() {
+ protected void resetImpl() {
this.delegate = null;
}
@Override
- public Component getByRef(int componentRef) {
+ protected Component getByRefImpl(int componentRef) {
return delegate.getByRef(componentRef);
}
}
import org.junit.rules.ExternalResource;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.ComponentProvider;
+import org.sonar.server.computation.component.NoComponentProvider;
import org.sonar.server.computation.component.TreeComponentProvider;
import org.sonar.server.computation.component.TreeRootHolder;
import org.sonar.server.computation.component.TreeRootHolderComponentProvider;
* providers.
*/
public class MeasureRepositoryRule extends ExternalResource implements MeasureRepository {
- @CheckForNull
private final ComponentProvider componentProvider;
@CheckForNull
private final MetricRepositoryRule metricRepositoryRule;
}
};
- private MeasureRepositoryRule(@Nullable ComponentProvider componentProvider, @Nullable MetricRepositoryRule metricRepositoryRule) {
+ private MeasureRepositoryRule(ComponentProvider componentProvider, @Nullable MetricRepositoryRule metricRepositoryRule) {
this.componentProvider = componentProvider;
this.metricRepositoryRule = metricRepositoryRule;
}
@Override
protected void after() {
- if (componentProvider != null) {
- componentProvider.reset();
- }
+ componentProvider.reset();
baseMeasures.clear();
rawMeasures.clear();
}
public static MeasureRepositoryRule create() {
- return new MeasureRepositoryRule(null, null);
+ return new MeasureRepositoryRule(NoComponentProvider.INSTANCE, null);
}
public static MeasureRepositoryRule create(TreeRootHolder treeRootHolder, MetricRepositoryRule metricRepositoryRule) {
}
private void checkAndInitProvidersState() {
- checkState(componentProvider != null, "Can not add a measure by Component ref if MeasureRepositoryRule has not been created for some Component provider");
checkState(metricRepositoryRule != null, "Can not add a measure by metric key if MeasureRepositoryRule has not been created for a MetricRepository");
- componentProvider.init();
+ componentProvider.ensureInitialized();
}
public boolean isEmpty() {