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.issue;
021    
022    import com.google.common.base.Predicate;
023    import com.google.common.collect.Iterables;
024    import org.sonar.api.issue.Issue;
025    import org.sonar.api.issue.ProjectIssues;
026    import org.sonar.api.issue.internal.DefaultIssue;
027    
028    import javax.annotation.Nullable;
029    
030    /**
031     * Expose list of issues for the current project
032     * @since 4.0
033     */
034    public class DefaultProjectIssues implements ProjectIssues {
035    
036      private final IssueCache cache;
037    
038      public DefaultProjectIssues(IssueCache cache) {
039        this.cache = cache;
040      }
041    
042      @Override
043      public Iterable<Issue> issues() {
044        return (Iterable) Iterables.filter(cache.all(), new ResolvedPredicate(false));
045      }
046    
047      @Override
048      public Iterable<Issue> resolvedIssues() {
049        return (Iterable) Iterables.filter(cache.all(), new ResolvedPredicate(true));
050      }
051    
052      private static class ResolvedPredicate implements Predicate<DefaultIssue> {
053        private final boolean resolved;
054    
055        private ResolvedPredicate(boolean resolved) {
056          this.resolved = resolved;
057        }
058    
059        @Override
060        public boolean apply(@Nullable DefaultIssue issue) {
061          if (issue != null) {
062            return resolved ? issue.resolution() != null : issue.resolution() == null;
063          }
064          return false;
065        }
066      }
067    }