| /*=============================================================================# |
| # Copyright (c) 2021 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.ltk.issues.core.impl; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.core.runtime.CoreException; |
| |
| import org.eclipse.statet.jcommons.collections.ImCollections; |
| import org.eclipse.statet.jcommons.collections.ImList; |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| import org.eclipse.statet.jcommons.status.StatusException; |
| import org.eclipse.statet.jcommons.status.eplatform.EStatusUtils; |
| |
| import org.eclipse.statet.ltk.core.WorkingContext; |
| import org.eclipse.statet.ltk.issues.core.IssueRequestor; |
| import org.eclipse.statet.ltk.issues.core.IssueTypeSet; |
| import org.eclipse.statet.ltk.issues.core.IssueTypeSet.ProblemTypeDef; |
| import org.eclipse.statet.ltk.issues.core.IssueTypeSet.TaskTypeDef; |
| import org.eclipse.statet.ltk.issues.core.Problem; |
| import org.eclipse.statet.ltk.issues.core.Task; |
| |
| |
| @NonNullByDefault |
| public abstract class BasicIssueRequestor implements IssueRequestor { |
| |
| private static final int STATE_OPEN= 1; |
| private static final int STATE_FINISHING= 2; |
| private static final int STATE_FINISHED= 3; |
| |
| |
| public final static class ProblemCategory { |
| |
| private final String id; |
| private final ProblemTypeDef def; |
| |
| private final boolean isEnabled; |
| |
| private final List<Problem> acceptedIssues= new ArrayList<>(); |
| |
| |
| public ProblemCategory(final ProblemTypeDef def, final boolean isEnabled) { |
| this.id= def.getId(); |
| this.def= def; |
| this.isEnabled= isEnabled; |
| } |
| |
| |
| public String getId() { |
| return this.id; |
| } |
| |
| public ProblemTypeDef getDef() { |
| return this.def; |
| } |
| |
| public List<Problem> getAcceptedIssues() { |
| return this.acceptedIssues; |
| } |
| |
| } |
| |
| public final static class TaskCategory { |
| |
| private final TaskTypeDef def; |
| |
| private final boolean isEnabled; |
| |
| private final List<Task> acceptedIssues= new ArrayList<>(); |
| |
| |
| public TaskCategory(final TaskTypeDef def, final boolean isEnabled) { |
| this.def= def; |
| this.isEnabled= isEnabled; |
| } |
| |
| |
| public TaskTypeDef getDef() { |
| return this.def; |
| } |
| |
| public List<Task> getAcceptedIssues() { |
| return this.acceptedIssues; |
| } |
| |
| } |
| |
| |
| private final IssueTypeSet issueTypeSet; |
| |
| private final ImList<ProblemCategory> problemCategories; |
| private final @Nullable TaskCategory taskCategory; |
| |
| private int state= 1; |
| |
| |
| public BasicIssueRequestor(final IssueTypeSet issueTypeSet, |
| final WorkingContext workingContext) { |
| this.issueTypeSet= issueTypeSet; |
| |
| this.problemCategories= createProblemCategories(issueTypeSet.getProblemCategories(), |
| workingContext ); |
| this.taskCategory= createTaskCategories(issueTypeSet.getTaskCategory(), |
| workingContext ); |
| } |
| |
| |
| protected ImList<ProblemCategory> createProblemCategories(final ImList<ProblemTypeDef> defs, |
| final WorkingContext requiredWorkingSet) { |
| final var list= new ArrayList<ProblemCategory>(defs.size()); |
| for (final var problemTypeDef : defs) { |
| if (problemTypeDef.getTypes(requiredWorkingSet) != null) { |
| list.add(new ProblemCategory(problemTypeDef, shouldAccept(problemTypeDef))); |
| } |
| } |
| return ImCollections.toList(list); |
| } |
| |
| protected boolean shouldAccept(final ProblemTypeDef def) { |
| return true; |
| } |
| |
| protected @Nullable TaskCategory createTaskCategories(final @Nullable TaskTypeDef def, |
| final WorkingContext requiredWorkingSet) { |
| if (def != null && def.getType(requiredWorkingSet) != null) { |
| return new TaskCategory(def, shouldAccept(def)); |
| } |
| return null; |
| } |
| |
| protected boolean shouldAccept(final TaskTypeDef def) { |
| return true; |
| } |
| |
| |
| public IssueTypeSet getIssueTypeSet() { |
| return this.issueTypeSet; |
| } |
| |
| protected final ImList<ProblemCategory> getProblemCategories() { |
| return this.problemCategories; |
| } |
| |
| protected final @Nullable ProblemCategory getProblemCategory(final String id) { |
| for (final var category : this.problemCategories) { |
| if (category.id == id) { |
| return category; |
| } |
| } |
| return null; |
| } |
| |
| protected final @Nullable TaskCategory getTaskCategory() { |
| return this.taskCategory; |
| } |
| |
| |
| @Override |
| public boolean isInterestedInProblems(final String categoryId) { |
| final var category= getProblemCategory(categoryId); |
| return (category != null && category.isEnabled); |
| } |
| |
| @Override |
| public void acceptProblems(final Problem problem) { |
| final var category= getProblemCategory(problem.getCategoryId()); |
| if (category != null && category.isEnabled) { |
| category.acceptedIssues.add(problem); |
| } |
| } |
| |
| @Override |
| public void acceptProblems(final String categoryId, final List<Problem> problems) { |
| final var category= getProblemCategory(categoryId); |
| if (category != null && category.isEnabled) { |
| category.acceptedIssues.addAll(problems); |
| } |
| } |
| |
| |
| @Override |
| public boolean isInterestedInTasks() { |
| final var category= getTaskCategory(); |
| return (category != null && category.isEnabled); |
| } |
| |
| @Override |
| public void acceptTask(final Task task) { |
| final var category= getTaskCategory(); |
| if (category != null && category.isEnabled) { |
| category.acceptedIssues.add(task); |
| } |
| } |
| |
| |
| @Override |
| public void finish() throws StatusException { |
| if (this.state != STATE_OPEN) { |
| throw new IllegalStateException("Already finished"); |
| } |
| this.state= STATE_FINISHING; |
| |
| try { |
| reportProblems(this.problemCategories); |
| reportTasks(this.taskCategory); |
| } |
| catch (final CoreException e) { |
| throw EStatusUtils.convert(e); |
| } |
| finally { |
| this.state= STATE_FINISHED; |
| } |
| } |
| |
| |
| protected abstract void reportProblems(final ImList<ProblemCategory> problemCategories) |
| throws CoreException; |
| |
| protected abstract void reportTasks(final @Nullable TaskCategory taskCategory) |
| throws CoreException; |
| |
| |
| } |