001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.component.extension.verifier; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.List; 022import java.util.Optional; 023import java.util.function.Supplier; 024 025import org.apache.camel.component.extension.ComponentVerifierExtension; 026import org.apache.camel.util.function.ThrowingBiConsumer; 027import org.apache.camel.util.function.ThrowingConsumer; 028 029public final class ResultBuilder { 030 private Optional<ComponentVerifierExtension.Scope> scope; 031 private Optional<ComponentVerifierExtension.Result.Status> status; 032 private List<ComponentVerifierExtension.VerificationError> verificationErrors; 033 034 public ResultBuilder() { 035 this.scope = Optional.empty(); 036 this.status = scope.empty(); 037 } 038 039 // ********************************** 040 // Accessors 041 // ********************************** 042 043 public ResultBuilder scope(ComponentVerifierExtension.Scope scope) { 044 this.scope = Optional.of(scope); 045 return this; 046 } 047 048 public ResultBuilder status(ComponentVerifierExtension.Result.Status status) { 049 this.status = Optional.of(status); 050 return this; 051 } 052 053 public ResultBuilder error(ComponentVerifierExtension.VerificationError verificationError) { 054 if (this.verificationErrors == null) { 055 this.verificationErrors = new ArrayList<>(); 056 } 057 058 this.verificationErrors.add(verificationError); 059 this.status = Optional.of(ComponentVerifierExtension.Result.Status.ERROR); 060 061 return this; 062 } 063 064 public ResultBuilder error(Optional<ComponentVerifierExtension.VerificationError> error) { 065 error.ifPresent(e -> error(e)); 066 return this; 067 } 068 069 public ResultBuilder error(Supplier<Optional<ComponentVerifierExtension.VerificationError>> supplier) { 070 return error(supplier.get()); 071 } 072 073 public ResultBuilder error(ThrowingConsumer<ResultBuilder, Exception> consumer) { 074 try { 075 consumer.accept(this); 076 } catch (NoSuchOptionException e) { 077 error(ResultErrorBuilder.withMissingOption(e.getOptionName()).build()); 078 } catch (IllegalOptionException e) { 079 error(ResultErrorBuilder.withIllegalOption(e.getOptionName(), e.getOptionValue()).build()); 080 } catch (Exception e) { 081 error(ResultErrorBuilder.withException(e).build()); 082 } 083 084 return this; 085 } 086 087 public <T> ResultBuilder error(T data, ThrowingBiConsumer<ResultBuilder, T, Exception> consumer) { 088 try { 089 consumer.accept(this, data); 090 } catch (NoSuchOptionException e) { 091 error(ResultErrorBuilder.withMissingOption(e.getOptionName()).build()); 092 } catch (IllegalOptionException e) { 093 error(ResultErrorBuilder.withIllegalOption(e.getOptionName(), e.getOptionValue()).build()); 094 } catch (Exception e) { 095 error(ResultErrorBuilder.withException(e).build()); 096 } 097 098 return this; 099 } 100 101 public ResultBuilder errors(List<ComponentVerifierExtension.VerificationError> verificationErrors) { 102 verificationErrors.forEach(this::error); 103 return this; 104 } 105 106 // ********************************** 107 // Build 108 // ********************************** 109 110 public ComponentVerifierExtension.Result build() { 111 return new DefaultResult( 112 scope.orElse(ComponentVerifierExtension.Scope.PARAMETERS), 113 status.orElse(ComponentVerifierExtension.Result.Status.UNSUPPORTED), 114 verificationErrors != null ? Collections.unmodifiableList(verificationErrors) : Collections.emptyList() 115 ); 116 } 117 118 // ********************************** 119 // Helpers 120 // ********************************** 121 122 public static ResultBuilder withStatus(ComponentVerifierExtension.Result.Status status) { 123 return new ResultBuilder().status(status); 124 } 125 126 public static ResultBuilder withStatusAndScope(ComponentVerifierExtension.Result.Status status, ComponentVerifierExtension.Scope scope) { 127 return new ResultBuilder().status(status).scope(scope); 128 } 129 130 public static ResultBuilder withScope(ComponentVerifierExtension.Scope scope) { 131 return new ResultBuilder().scope(scope); 132 } 133 134 public static ResultBuilder unsupported() { 135 return withStatusAndScope(ComponentVerifierExtension.Result.Status.UNSUPPORTED, ComponentVerifierExtension.Scope.PARAMETERS); 136 } 137 138 public static ResultBuilder unsupportedScope(ComponentVerifierExtension.Scope scope) { 139 return withStatusAndScope(ComponentVerifierExtension.Result.Status.UNSUPPORTED, scope); 140 } 141}