You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidationResultWrap.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.Optional;
  21. import com.vaadin.server.SerializableConsumer;
  22. import com.vaadin.server.SerializableFunction;
  23. /**
  24. * Internal implementation of a {@code Result} that collects all possible
  25. * ValidationResults into one list. This class intercepts the normal chaining of
  26. * Converters and Validators, catching and collecting results.
  27. *
  28. * @param <R>
  29. * the result data type
  30. *
  31. * @since 8.2
  32. */
  33. class ValidationResultWrap<R> implements Result<R> {
  34. private final List<ValidationResult> resultList;
  35. private final Result<R> wrappedResult;
  36. ValidationResultWrap(Result<R> result, List<ValidationResult> resultList) {
  37. this.resultList = resultList;
  38. this.wrappedResult = result;
  39. }
  40. ValidationResultWrap(R value, ValidationResult result) {
  41. if (result.isError()) {
  42. wrappedResult = new SimpleResult<>(null, result.getErrorMessage());
  43. } else {
  44. wrappedResult = new SimpleResult<>(value, null);
  45. }
  46. this.resultList = new ArrayList<>();
  47. this.resultList.add(result);
  48. }
  49. List<ValidationResult> getValidationResults() {
  50. return Collections.unmodifiableList(resultList);
  51. }
  52. Result<R> getWrappedResult() {
  53. return wrappedResult;
  54. }
  55. @Override
  56. public <S> Result<S> flatMap(SerializableFunction<R, Result<S>> mapper) {
  57. Result<S> result = wrappedResult.flatMap(mapper);
  58. if (!(result instanceof ValidationResultWrap)) {
  59. return new ValidationResultWrap<S>(result, resultList);
  60. }
  61. List<ValidationResult> currentResults = new ArrayList<>(resultList);
  62. ValidationResultWrap<S> resultWrap = (ValidationResultWrap<S>) result;
  63. currentResults.addAll(resultWrap.getValidationResults());
  64. return new ValidationResultWrap<>(resultWrap.getWrappedResult(),
  65. currentResults);
  66. }
  67. @Override
  68. public void handle(SerializableConsumer<R> ifOk,
  69. SerializableConsumer<String> ifError) {
  70. wrappedResult.handle(ifOk, ifError);
  71. }
  72. @Override
  73. public boolean isError() {
  74. return wrappedResult.isError();
  75. }
  76. @Override
  77. public Optional<String> getMessage() {
  78. return wrappedResult.getMessage();
  79. }
  80. @Override
  81. public <X extends Throwable> R getOrThrow(
  82. SerializableFunction<String, ? extends X> exceptionProvider)
  83. throws X {
  84. return wrappedResult.getOrThrow(exceptionProvider);
  85. }
  86. }