001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import java.util.Arrays;
005import java.util.List;
006import java.util.Objects;
007import java.util.stream.Collectors;
008
009import org.openstreetmap.josm.gui.util.ChangeNotifier;
010import org.openstreetmap.josm.tools.Utils;
011
012/**
013 * ChangesetCommentModel is an observable model for the changeset comment edited
014 * in the {@link UploadDialog}.
015 * @since 3133
016 */
017public class ChangesetCommentModel extends ChangeNotifier {
018    private String comment = "";
019
020    /**
021     * Sets the current changeset comment and notifies observers if the comment has changed.
022     *
023     * @param comment the new upload comment. Empty string assumed if null.
024     */
025    public void setComment(String comment) {
026        String oldValue = this.comment;
027        this.comment = comment == null ? "" : comment;
028        if (!Objects.equals(oldValue, this.comment)) {
029            fireStateChanged();
030        }
031    }
032
033    /**
034     * Replies the current changeset comment in this model.
035     *
036     * @return the current changeset comment in this model.
037     */
038    public String getComment() {
039        return comment == null ? "" : comment;
040    }
041
042    /**
043     * Extracts the list of hashtags from the comment text.
044     * @return the list of hashtags from the comment text. Can be empty, but not null.
045     * @since 13109
046     */
047    public List<String> findHashTags() {
048        return Arrays.stream(comment.split("\\s"))
049                .map(s -> Utils.strip(s, ",;"))
050                .filter(s -> s.matches("#[a-zA-Z][a-zA-Z_\\-0-9]+"))
051                .collect(Collectors.toList());
052    }
053}