/* * Copyright 2000-2016 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin.data; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; import com.vaadin.server.SerializableConsumer; import com.vaadin.server.SerializableFunction; /** * Internal implementation of a {@code Result} that collects all possible * ValidationResults into one list. This class intercepts the normal chaining of * Converters and Validators, catching and collecting results. * * @param * the result data type * * @since 8.2 */ class ValidationResultWrap implements Result { private final List resultList; private final Result wrappedResult; ValidationResultWrap(Result result, List resultList) { this.resultList = resultList; this.wrappedResult = result; } ValidationResultWrap(R value, ValidationResult result) { if (result.isError()) { wrappedResult = new SimpleResult<>(null, result.getErrorMessage()); } else { wrappedResult = new SimpleResult<>(value, null); } this.resultList = new ArrayList<>(); this.resultList.add(result); } List getValidationResults() { return Collections.unmodifiableList(resultList); } Result getWrappedResult() { return wrappedResult; } @Override public Result flatMap(SerializableFunction> mapper) { Result result = wrappedResult.flatMap(mapper); if (!(result instanceof ValidationResultWrap)) { return new ValidationResultWrap(result, resultList); } List currentResults = new ArrayList<>(resultList); ValidationResultWrap resultWrap = (ValidationResultWrap) result; currentResults.addAll(resultWrap.getValidationResults()); return new ValidationResultWrap<>(resultWrap.getWrappedResult(), currentResults); } @Override public void handle(SerializableConsumer ifOk, SerializableConsumer ifError) { wrappedResult.handle(ifOk, ifError); } @Override public boolean isError() { return wrappedResult.isError(); } @Override public Optional getMessage() { return wrappedResult.getMessage(); } @Override public R getOrThrow( SerializableFunction exceptionProvider) throws X { return wrappedResult.getOrThrow(exceptionProvider); } }