Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.entities; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.base.Strings; 21 : import com.google.gerrit.common.Nullable; 22 : import com.google.gerrit.extensions.api.projects.CommentLinkInfo; 23 : 24 : /** Info about a single commentlink section in a config. */ 25 : @AutoValue 26 3 : public abstract class StoredCommentLinkInfo { 27 : public abstract String getName(); 28 : 29 : /** A regular expression to match for the commentlink to apply. */ 30 : @Nullable 31 : public abstract String getMatch(); 32 : 33 : /** 34 : * The link to replace the match with. 35 : * 36 : * <p>The constructed link is using {@link #getLink()} {@link #getPrefix()} {@link #getSuffix()} 37 : * and {@link #getText()}, and has the shape of 38 : * 39 : * <p>{@code PREFIX<a href="LINK">TEXT</a>SUFFIX} 40 : */ 41 : @Nullable 42 : public abstract String getLink(); 43 : 44 : /** The optional text before the link tag that the match is replaced with. */ 45 : @Nullable 46 : public abstract String getPrefix(); 47 : 48 : /** The optional text after the link tag that the match is replaced with. */ 49 : @Nullable 50 : public abstract String getSuffix(); 51 : 52 : /** The content of the link tag that the match is replaced with. If not set full match is used. */ 53 : @Nullable 54 : public abstract String getText(); 55 : 56 : /** Weather this comment link is active. {@code null} means true. */ 57 : @Nullable 58 : public abstract Boolean getEnabled(); 59 : 60 : /** If set, {@link StoredCommentLinkInfo} has to be overridden to take any effect. */ 61 : public abstract boolean getOverrideOnly(); 62 : 63 : /** 64 : * Creates an enabled {@link StoredCommentLinkInfo} that can be overridden but doesn't do anything 65 : * on its own. 66 : */ 67 : public static StoredCommentLinkInfo enabled(String name) { 68 1 : return builder(name).setOverrideOnly(true).build(); 69 : } 70 : 71 : /** 72 : * Creates a disabled {@link StoredCommentLinkInfo} that can be overridden but doesn't do anything 73 : * on it's own. 74 : */ 75 : public static StoredCommentLinkInfo disabled(String name) { 76 1 : return builder(name).setOverrideOnly(true).setEnabled(false).build(); 77 : } 78 : 79 : /** Creates and returns a new {@link StoredCommentLinkInfo.Builder} instance. */ 80 : public static Builder builder(String name) { 81 3 : checkArgument(name != null, "invalid commentlink.name"); 82 3 : return new AutoValue_StoredCommentLinkInfo.Builder().setName(name).setOverrideOnly(false); 83 : } 84 : 85 : /** Creates and returns a new {@link StoredCommentLinkInfo} instance with the same values. */ 86 : public static StoredCommentLinkInfo fromInfo(CommentLinkInfo src, Boolean enabled) { 87 1 : return builder(src.name) 88 1 : .setMatch(src.match) 89 1 : .setLink(src.link) 90 1 : .setPrefix(src.prefix) 91 1 : .setSuffix(src.suffix) 92 1 : .setText(src.text) 93 1 : .setEnabled(enabled) 94 1 : .setOverrideOnly(false) 95 1 : .build(); 96 : } 97 : 98 : /** Returns an {@link CommentLinkInfo} instance with the same values. */ 99 : public CommentLinkInfo toInfo() { 100 2 : CommentLinkInfo info = new CommentLinkInfo(); 101 2 : info.name = getName(); 102 2 : info.match = getMatch(); 103 2 : info.link = getLink(); 104 2 : info.prefix = getPrefix(); 105 2 : info.suffix = getSuffix(); 106 2 : info.text = getText(); 107 2 : info.enabled = getEnabled(); 108 2 : return info; 109 : } 110 : 111 : @AutoValue.Builder 112 3 : public abstract static class Builder { 113 : public abstract Builder setName(String value); 114 : 115 : public abstract Builder setMatch(@Nullable String value); 116 : 117 : public abstract Builder setLink(@Nullable String value); 118 : 119 : public abstract Builder setPrefix(@Nullable String value); 120 : 121 : public abstract Builder setSuffix(@Nullable String value); 122 : 123 : public abstract Builder setText(@Nullable String value); 124 : 125 : public abstract Builder setEnabled(@Nullable Boolean value); 126 : 127 : public abstract Builder setOverrideOnly(boolean value); 128 : 129 : public StoredCommentLinkInfo build() { 130 3 : checkArgument(getName() != null, "invalid commentlink.name"); 131 3 : setPrefix(Strings.emptyToNull(getPrefix())); 132 3 : setSuffix(Strings.emptyToNull(getSuffix())); 133 3 : setText(Strings.emptyToNull(getText())); 134 3 : if (!getOverrideOnly()) { 135 3 : checkArgument( 136 3 : !Strings.isNullOrEmpty(getMatch()), "invalid commentlink.%s.match", getName()); 137 3 : checkArgument( 138 3 : !Strings.isNullOrEmpty(getLink()), 139 : "commentlink.%s must have link specified", 140 3 : getName()); 141 : } 142 3 : return autoBuild(); 143 : } 144 : 145 : protected abstract StoredCommentLinkInfo autoBuild(); 146 : 147 : protected abstract String getName(); 148 : 149 : protected abstract String getMatch(); 150 : 151 : protected abstract String getLink(); 152 : 153 : protected abstract String getPrefix(); 154 : 155 : protected abstract String getSuffix(); 156 : 157 : protected abstract String getText(); 158 : 159 : protected abstract boolean getOverrideOnly(); 160 : } 161 : }