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