| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.ast.comments; |
| 23 | |
| 24 | import com.github.javaparser.Range; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.Set; |
| 28 | import java.util.TreeSet; |
| 29 | import java.util.stream.Collectors; |
| 30 | |
| 31 | import static com.github.javaparser.ast.Node.NODE_BY_BEGIN_POSITION; |
| 32 | |
| 33 | /** |
| 34 | * The comments contained in a certain parsed piece of source code. |
| 35 | */ |
| 36 | public class CommentsCollection { |
| 37 | private final TreeSet<Comment> comments = new TreeSet<>(NODE_BY_BEGIN_POSITION); |
| 38 | |
| 39 | public CommentsCollection() { |
| 40 | } |
| 41 | |
| 42 | public CommentsCollection(Collection<Comment> commentsToCopy) { |
| 43 | comments.addAll(commentsToCopy); |
| 44 | } |
| 45 | |
| 46 | public Set<LineComment> getLineComments() { |
| 47 | return comments.stream() |
| 48 | .filter(comment -> comment instanceof LineComment) |
| 49 | .map(comment -> (LineComment) comment) |
| 50 | .collect(Collectors.toCollection(() -> new TreeSet<>(NODE_BY_BEGIN_POSITION))); |
| 51 | } |
| 52 | |
| 53 | public Set<BlockComment> getBlockComments() { |
| 54 | return comments.stream() |
| 55 | .filter(comment -> comment instanceof BlockComment) |
| 56 | .map(comment -> (BlockComment) comment) |
| 57 | .collect(Collectors.toCollection(() -> new TreeSet<>(NODE_BY_BEGIN_POSITION))); |
| 58 | } |
| 59 | |
| 60 | public Set<JavadocComment> getJavadocComments() { |
| 61 | return comments.stream() |
| 62 | .filter(comment -> comment instanceof JavadocComment) |
| 63 | .map(comment -> (JavadocComment) comment) |
| 64 | .collect(Collectors.toCollection(() -> new TreeSet<>(NODE_BY_BEGIN_POSITION))); |
| 65 | } |
| 66 | |
| 67 | public void addComment(Comment comment) { |
| 68 | comments.add(comment); |
| 69 | } |
| 70 | |
| 71 | public boolean contains(Comment comment) { |
| 72 | if (!comment.getRange().isPresent()) { |
| 73 | return false; |
| 74 | } |
| 75 | Range commentRange = comment.getRange().get(); |
| 76 | for (Comment c : getComments()) { |
| 77 | if (!c.getRange().isPresent()) { |
| 78 | return false; |
| 79 | } |
| 80 | Range cRange = c.getRange().get(); |
| 81 | // we tolerate a difference of one element in the end column: |
| 82 | // it depends how \r and \n are calculated... |
| 83 | if (cRange.begin.equals(commentRange.begin) && |
| 84 | cRange.end.line == commentRange.end.line && |
| 85 | Math.abs(cRange.end.column - commentRange.end.column) < 2) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | public TreeSet<Comment> getComments() { |
| 93 | return comments; |
| 94 | } |
| 95 | |
| 96 | public int size() { |
| 97 | return comments.size(); |
| 98 | } |
| 99 | |
| 100 | public CommentsCollection minus(CommentsCollection other) { |
| 101 | CommentsCollection result = new CommentsCollection(); |
| 102 | result.comments.addAll( |
| 103 | comments.stream() |
| 104 | .filter(comment -> !other.contains(comment)) |
| 105 | .collect(Collectors.toList())); |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | public CommentsCollection copy() { |
| 110 | return new CommentsCollection(comments); |
| 111 | } |
| 112 | } |
| 113 |
Members