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.impl.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.ComponentVerifier; 026import org.apache.camel.IllegalOptionException; 027import org.apache.camel.NoSuchOptionException; 028import org.apache.camel.util.function.ThrowingBiConsumer; 029import org.apache.camel.util.function.ThrowingConsumer; 030 031public final class ResultBuilder { 032 private Optional<ComponentVerifier.Scope> scope; 033 private Optional<ComponentVerifier.Result.Status> status; 034 private List<ComponentVerifier.VerificationError> verificationErrors; 035 036 public ResultBuilder() { 037 this.scope = Optional.empty(); 038 this.status = scope.empty(); 039 } 040 041 // ********************************** 042 // Accessors 043 // ********************************** 044 045 public ResultBuilder scope(ComponentVerifier.Scope scope) { 046 this.scope = Optional.of(scope); 047 return this; 048 } 049 050 public ResultBuilder status(ComponentVerifier.Result.Status status) { 051 this.status = Optional.of(status); 052 return this; 053 } 054 055 public ResultBuilder error(ComponentVerifier.VerificationError verificationError) { 056 if (this.verificationErrors == null) { 057 this.verificationErrors = new ArrayList<>(); 058 } 059 060 this.verificationErrors.add(verificationError); 061 this.status = Optional.of(ComponentVerifier.Result.Status.ERROR); 062 063 return this; 064 } 065 066 public ResultBuilder error(Optional<ComponentVerifier.VerificationError> error) { 067 error.ifPresent(e -> error(e)); 068 return this; 069 } 070 071 public ResultBuilder error(Supplier<Optional<ComponentVerifier.VerificationError>> supplier) { 072 return error(supplier.get()); 073 } 074 075 public ResultBuilder error(ThrowingConsumer<ResultBuilder, Exception> consumer) { 076 try { 077 consumer.accept(this); 078 } catch (NoSuchOptionException e) { 079 error(ResultErrorBuilder.withMissingOption(e.getOptionName()).build()); 080 } catch (IllegalOptionException e) { 081 error(ResultErrorBuilder.withIllegalOption(e.getOptionName(), e.getOptionValue()).build()); 082 } catch (Exception e) { 083 error(ResultErrorBuilder.withException(e).build()); 084 } 085 086 return this; 087 } 088 089 public <T> ResultBuilder error(T data, ThrowingBiConsumer<ResultBuilder, T, Exception> consumer) { 090 try { 091 consumer.accept(this, data); 092 } catch (NoSuchOptionException e) { 093 error(ResultErrorBuilder.withMissingOption(e.getOptionName()).build()); 094 } catch (IllegalOptionException e) { 095 error(ResultErrorBuilder.withIllegalOption(e.getOptionName(), e.getOptionValue()).build()); 096 } catch (Exception e) { 097 error(ResultErrorBuilder.withException(e).build()); 098 } 099 100 return this; 101 } 102 103 public ResultBuilder errors(List<ComponentVerifier.VerificationError> verificationErrors) { 104 verificationErrors.forEach(this::error); 105 return this; 106 } 107 108 // ********************************** 109 // Build 110 // ********************************** 111 112 public ComponentVerifier.Result build() { 113 return new DefaultResult( 114 scope.orElse(ComponentVerifier.Scope.PARAMETERS), 115 status.orElse(ComponentVerifier.Result.Status.UNSUPPORTED), 116 verificationErrors != null ? Collections.unmodifiableList(verificationErrors) : Collections.emptyList() 117 ); 118 } 119 120 // ********************************** 121 // Helpers 122 // ********************************** 123 124 public static ResultBuilder withStatus(ComponentVerifier.Result.Status status) { 125 return new ResultBuilder().status(status); 126 } 127 128 public static ResultBuilder withStatusAndScope(ComponentVerifier.Result.Status status, ComponentVerifier.Scope scope) { 129 return new ResultBuilder().status(status).scope(scope); 130 } 131 132 public static ResultBuilder withScope(ComponentVerifier.Scope scope) { 133 return new ResultBuilder().scope(scope); 134 } 135 136 public static ResultBuilder unsupported() { 137 return withStatusAndScope(ComponentVerifier.Result.Status.UNSUPPORTED, ComponentVerifier.Scope.PARAMETERS); 138 } 139 140 public static ResultBuilder unsupportedScope(ComponentVerifier.Scope scope) { 141 return withStatusAndScope(ComponentVerifier.Result.Status.UNSUPPORTED, scope); 142 } 143}