com.google.gerrit.server.change.CommentThread Maven / Gradle / Ivy
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.change;
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gerrit.entities.Comment;
import com.google.gerrit.entities.HumanComment;
import java.util.List;
import java.util.Optional;
/**
* Representation of a comment thread.
*
* A comment thread consists of at least one comment.
*
* @param type of comments in the thread. Can also be {@link Comment} if the thread mixes
* comments of different types.
*/
@AutoValue
public abstract class CommentThread {
/** Comments in the thread in exactly the order they appear in the thread. */
public abstract ImmutableList comments();
/** Whether the whole thread is considered as unresolved. */
public boolean unresolved() {
Optional lastHumanComment =
Streams.findLast(
comments().stream()
.filter(HumanComment.class::isInstance)
.map(HumanComment.class::cast));
// We often use false == null for boolean fields. It's also a safe fall-back if no human comment
// is part of the thread.
return lastHumanComment.map(comment -> comment.unresolved).orElse(false);
}
public static Builder builder() {
return new AutoValue_CommentThread.Builder<>();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder comments(List value);
@CanIgnoreReturnValue
public Builder addComment(T comment) {
commentsBuilder().add(comment);
return this;
}
abstract ImmutableList.Builder commentsBuilder();
abstract ImmutableList comments();
abstract CommentThread autoBuild();
public CommentThread build() {
Preconditions.checkState(
!comments().isEmpty(), "A comment thread must contain at least one comment.");
return autoBuild();
}
}
}