1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.authz.layer;
\r
25 import java.util.Collection;
\r
26 import java.util.List;
\r
27 import java.util.Set;
\r
31 * It would be nice if Java Enums were extensible, but they're not.
\r
35 public class Result<RV> {
\r
36 private static final String SUCCESS = "Success";
\r
37 public static final String[] EMPTY_VARS = new String[0];
\r
39 public final static int OK=0,
\r
44 ERR_NotImplemented = 5,
\r
46 ERR_ConflictAlreadyExists = 7,
\r
47 ERR_ActionNotCompleted = 8,
\r
51 public final RV value;
\r
52 public final int status;
\r
53 public final String details;
\r
54 public final String[] variables;
\r
56 protected Result(RV value, int status, String details, String[] variables) {
\r
59 specialCondition|=EMPTY_LIST;
\r
61 this.status = status;
\r
62 this.details = details;
\r
63 if(variables==null) {
\r
64 this.variables = EMPTY_VARS;
\r
66 this.variables=variables;
\r
71 * Create a Result class with "OK" status and "Success" for details
\r
73 * This is the easiest to use
\r
79 public static<R> Result<R> ok(R value) {
\r
80 return new Result<R>(value,OK,SUCCESS,null);
\r
84 * Accept Arrays and mark as empty or not
\r
88 public static<R> Result<R[]> ok(R value[]) {
\r
89 return new Result<R[]>(value,OK,SUCCESS,null).emptyList(value.length==0);
\r
93 * Accept Sets and mark as empty or not
\r
97 public static<R> Result<Set<R>> ok(Set<R> value) {
\r
98 return new Result<Set<R>>(value,OK,SUCCESS,null).emptyList(value.size()==0);
\r
102 * Accept Lists and mark as empty or not
\r
106 public static<R> Result<List<R>> ok(List<R> value) {
\r
107 return new Result<List<R>>(value,OK,SUCCESS,null).emptyList(value.size()==0);
\r
111 * Accept Collections and mark as empty or not
\r
115 public static<R> Result<Collection<R>> ok(Collection<R> value) {
\r
116 return new Result<Collection<R>>(value,OK,SUCCESS,null).emptyList(value.size()==0);
\r
121 * Special Case for Void Type
\r
124 public static Result<Void> ok() {
\r
125 return new Result<Void>(null,OK,SUCCESS,null);
\r
129 * Create a Status (usually non OK, with a details statement
\r
135 // public static<R> Result<R> err(int status, String details) {
\r
136 // return new Result<R>(null,status,details,null);
\r
140 * Create a Status (usually non OK, with a details statement and variables supported
\r
146 public static<R> Result<R> err(int status, String details, String ... variables) {
\r
147 return new Result<R>(null,status,details,variables);
\r
151 * Create Error from status and Details of previous Result (and not data)
\r
155 public static<R> Result<R> err(Result<?> pdr) {
\r
156 return new Result<R>(null,pdr.status,pdr.details,pdr.variables);
\r
160 * Create General Error from Exception
\r
164 public static<R> Result<R> err(Exception e) {
\r
165 return new Result<R>(null,ERR_General,e.getMessage(),EMPTY_VARS);
\r
169 * Create a Status (usually non OK, with a details statement
\r
175 public static<R> Result<R> create(R value, int status, String details, String ... vars) {
\r
176 return new Result<R>(value,status,details,vars);
\r
180 * Create a Status from a previous status' result/details
\r
186 public static<R> Result<R> create(R value, Result<?> result) {
\r
187 return new Result<R>(value,result.status,result.details,result.variables);
\r
190 private static final int PARTIAL_CONTENT = 0x001;
\r
191 private static final int EMPTY_LIST = 0x002;
\r
194 * AAF Specific problems, etc
\r
200 * specialCondition is a bit field to enable multiple conditions, e.g. PARTIAL_CONTENT
\r
202 private int specialCondition = 0;
\r
206 * Is result set only partial results, i.e. the DAO clipped the real result set to a smaller number.
\r
207 * @return true iff result returned PARTIAL_CONTENT
\r
209 public boolean partialContent() {
\r
210 return (specialCondition & PARTIAL_CONTENT) == PARTIAL_CONTENT;
\r
214 * Set fact that result set only returned partial results, i.e. the DAO clipped the real result set to a smaller number.
\r
215 * @param hasPartialContent set true iff result returned PARTIAL_CONTENT
\r
216 * @return this Result object, so you can chain calls, in builder style
\r
218 public Result<RV> partialContent(boolean hasPartialContent) {
\r
219 if (hasPartialContent) {
\r
220 specialCondition |= PARTIAL_CONTENT;
\r
222 specialCondition &= (~PARTIAL_CONTENT);
\r
228 * When Result is a List, you can check here to see if it's empty instead of looping
\r
232 public boolean isEmpty() {
\r
233 return (specialCondition & EMPTY_LIST) == EMPTY_LIST;
\r
237 * A common occurrence is that data comes back, but list is empty. If set, you can skip looking
\r
238 * at list at the outset.
\r
243 public Result<RV> emptyList(boolean emptyList) {
\r
245 specialCondition |= EMPTY_LIST;
\r
247 specialCondition &= (~EMPTY_LIST);
\r
254 * Convenience function. Checks OK, and also if List is not Empty
\r
255 * Not valid if Data is not a List
\r
258 public boolean isOK() {
\r
259 return status == OK;
\r
263 * Convenience function. Checks OK, and also if List is not Empty
\r
264 * Not valid if Data is not a List
\r
267 public boolean notOK() {
\r
268 return status != OK;
\r
272 * Convenience function. Checks OK, and also if List is not Empty
\r
273 * Not valid if Data is not a List
\r
276 public boolean isOKhasData() {
\r
277 return status == OK && (specialCondition & EMPTY_LIST) != EMPTY_LIST;
\r
282 * Convenience function. Checks OK, and also if List is not Empty
\r
283 * Not valid if Data is not a List
\r
286 public boolean notOKorIsEmpty() {
\r
287 return status != OK || (specialCondition & EMPTY_LIST) == EMPTY_LIST;
\r
291 public String toString() {
\r
295 StringBuilder sb = new StringBuilder();
\r
298 sb.append(String.format(details,((Object[])variables)));
\r
300 sb.append("{empty}");
\r
303 sb.append(value.toString());
\r
304 return sb.toString();
\r
308 public String errorString() {
\r
309 StringBuilder sb = new StringBuilder();
\r
311 case 1: sb.append("Security"); break;
\r
312 case 2: sb.append("Denied"); break;
\r
313 case 3: sb.append("Policy"); break;
\r
314 case 4: sb.append("BadData"); break;
\r
315 case 5: sb.append("NotImplemented"); break;
\r
316 case 6: sb.append("NotFound"); break;
\r
317 case 7: sb.append("AlreadyExists"); break;
\r
318 case 8: sb.append("ActionNotComplete"); break;
\r
319 default: sb.append("Error");
\r
322 sb.append(String.format(details, (Object[])variables));
\r
323 return sb.toString();
\r