Line data Source code
1 : // Copyright (C) 2013 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.extensions.api.changes; 16 : 17 : import com.google.common.collect.ArrayListMultimap; 18 : import com.google.common.collect.ListMultimap; 19 : import com.google.gerrit.extensions.client.ListChangesOption; 20 : import com.google.gerrit.extensions.common.ChangeInfo; 21 : import com.google.gerrit.extensions.common.ChangeInput; 22 : import com.google.gerrit.extensions.restapi.NotImplementedException; 23 : import com.google.gerrit.extensions.restapi.RestApiException; 24 : import java.util.Arrays; 25 : import java.util.EnumSet; 26 : import java.util.List; 27 : import java.util.Set; 28 : 29 : public interface Changes { 30 : /** 31 : * Look up a change by numeric ID. 32 : * 33 : * <p><strong>Note:</strong> This method eagerly reads the change. Methods that mutate the change 34 : * do not necessarily re-read the change. Therefore, calling a getter method on an instance after 35 : * calling a mutation method on that same instance is not guaranteed to reflect the mutation. It 36 : * is not recommended to store references to {@code ChangeApi} instances. 37 : * 38 : * @param id change number. 39 : * @return API for accessing the change. 40 : * @throws RestApiException if an error occurred. 41 : */ 42 : ChangeApi id(int id) throws RestApiException; 43 : 44 : /** 45 : * Look up a change by string ID. 46 : * 47 : * @see #id(int) 48 : * @param id any identifier supported by the REST API, including change number, Change-Id, or 49 : * project~branch~Change-Id triplet. 50 : * @return API for accessing the change. 51 : * @throws RestApiException if an error occurred. 52 : */ 53 : ChangeApi id(String id) throws RestApiException; 54 : 55 : /** 56 : * Look up a change by project, branch, and change ID. 57 : * 58 : * @see #id(int) 59 : */ 60 : ChangeApi id(String project, String branch, String id) throws RestApiException; 61 : 62 : /** 63 : * Look up a change by project and numeric ID. 64 : * 65 : * @param project project name. 66 : * @param id change number. 67 : * @see #id(int) 68 : */ 69 : ChangeApi id(String project, int id) throws RestApiException; 70 : 71 : ChangeApi create(ChangeInput in) throws RestApiException; 72 : 73 : ChangeInfo createAsInfo(ChangeInput in) throws RestApiException; 74 : 75 : QueryRequest query(); 76 : 77 : QueryRequest query(String query); 78 : 79 22 : abstract class QueryRequest { 80 : private String query; 81 : private int limit; 82 : private int start; 83 : private boolean isNoLimit; 84 22 : private Set<ListChangesOption> options = EnumSet.noneOf(ListChangesOption.class); 85 22 : private ListMultimap<String, String> pluginOptions = ArrayListMultimap.create(); 86 : 87 : public abstract List<ChangeInfo> get() throws RestApiException; 88 : 89 : public QueryRequest withQuery(String query) { 90 21 : this.query = query; 91 21 : return this; 92 : } 93 : 94 : public QueryRequest withLimit(int limit) { 95 5 : this.limit = limit; 96 5 : return this; 97 : } 98 : 99 : public QueryRequest withNoLimit() { 100 5 : this.isNoLimit = true; 101 5 : return this; 102 : } 103 : 104 : public QueryRequest withStart(int start) { 105 5 : this.start = start; 106 5 : return this; 107 : } 108 : 109 : /** Set an option on the request, appending to existing options. */ 110 : public QueryRequest withOption(ListChangesOption options) { 111 6 : this.options.add(options); 112 6 : return this; 113 : } 114 : 115 : /** Set options on the request, appending to existing options. */ 116 : public QueryRequest withOptions(ListChangesOption... options) { 117 2 : this.options.addAll(Arrays.asList(options)); 118 2 : return this; 119 : } 120 : 121 : /** Set options on the request, replacing existing options. */ 122 : public QueryRequest withOptions(Set<ListChangesOption> options) { 123 5 : this.options = options; 124 5 : return this; 125 : } 126 : 127 : /** Set a plugin option on the request, appending to existing options. */ 128 : public QueryRequest withPluginOption(String name, String value) { 129 0 : this.pluginOptions.put(name, value); 130 0 : return this; 131 : } 132 : 133 : /** Set a plugin option on the request, replacing existing options. */ 134 : public QueryRequest withPluginOptions(ListMultimap<String, String> options) { 135 1 : this.pluginOptions = ArrayListMultimap.create(options); 136 1 : return this; 137 : } 138 : 139 : public String getQuery() { 140 22 : return query; 141 : } 142 : 143 : public int getLimit() { 144 22 : return limit; 145 : } 146 : 147 : public boolean getNoLimit() { 148 22 : return isNoLimit; 149 : } 150 : 151 : public int getStart() { 152 22 : return start; 153 : } 154 : 155 : public Set<ListChangesOption> getOptions() { 156 22 : return options; 157 : } 158 : 159 : public ListMultimap<String, String> getPluginOptions() { 160 22 : return pluginOptions; 161 : } 162 : 163 : @Override 164 : public String toString() { 165 0 : StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('{').append(query); 166 0 : if (limit != 0) { 167 0 : sb.append(", limit=").append(limit); 168 : } 169 0 : if (start != 0) { 170 0 : sb.append(", start=").append(start); 171 : } 172 0 : if (!options.isEmpty()) { 173 0 : sb.append("options=").append(options); 174 : } 175 0 : sb.append('}'); 176 0 : if (isNoLimit == true) { 177 0 : sb.append(" --no-limit"); 178 : } 179 0 : return sb.toString(); 180 : } 181 : } 182 : 183 : /** 184 : * A default implementation which allows source compatibility when adding new methods to the 185 : * interface. 186 : */ 187 0 : class NotImplemented implements Changes { 188 : @Override 189 : public ChangeApi id(int id) throws RestApiException { 190 0 : throw new NotImplementedException(); 191 : } 192 : 193 : @Override 194 : public ChangeApi id(String triplet) throws RestApiException { 195 0 : throw new NotImplementedException(); 196 : } 197 : 198 : @Override 199 : public ChangeApi id(String project, String branch, String id) throws RestApiException { 200 0 : throw new NotImplementedException(); 201 : } 202 : 203 : @Override 204 : public ChangeApi id(String project, int id) throws RestApiException { 205 0 : throw new NotImplementedException(); 206 : } 207 : 208 : @Override 209 : public ChangeApi create(ChangeInput in) throws RestApiException { 210 0 : throw new NotImplementedException(); 211 : } 212 : 213 : @Override 214 : public ChangeInfo createAsInfo(ChangeInput in) throws RestApiException { 215 0 : throw new NotImplementedException(); 216 : } 217 : 218 : @Override 219 : public QueryRequest query() { 220 0 : throw new NotImplementedException(); 221 : } 222 : 223 : @Override 224 : public QueryRequest query(String query) { 225 0 : throw new NotImplementedException(); 226 : } 227 : } 228 : }