Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SimpleResult.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2000-2018 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.Objects;
  18. import java.util.Optional;
  19. import com.vaadin.server.SerializableConsumer;
  20. import com.vaadin.server.SerializableFunction;
  21. /**
  22. * An internal implementation of {@code Result}.
  23. *
  24. * @param <R>
  25. * the result value type
  26. *
  27. * @since 8.0
  28. */
  29. class SimpleResult<R> implements Result<R> {
  30. private final R value;
  31. private final String message;
  32. /**
  33. * Creates a new {@link Result} instance using {@code value} for a non error
  34. * {@link Result} and {@code message} for an error {@link Result}.
  35. * <p>
  36. * If {@code message} is null then {@code value} is ignored and result is an
  37. * error.
  38. *
  39. * @param value
  40. * the value of the result, may be {@code null}
  41. * @param message
  42. * the error message of the result, may be {@code null}
  43. */
  44. SimpleResult(R value, String message) {
  45. // value != null => message == null
  46. assert value == null
  47. || message == null : "Message must be null if value is provided";
  48. this.value = value;
  49. this.message = message;
  50. }
  51. @Override
  52. @SuppressWarnings("unchecked")
  53. public <S> Result<S> flatMap(SerializableFunction<R, Result<S>> mapper) {
  54. Objects.requireNonNull(mapper, "mapper cannot be null");
  55. if (isError()) {
  56. // Safe cast; valueless
  57. return (Result<S>) this;
  58. } else {
  59. return mapper.apply(value);
  60. }
  61. }
  62. @Override
  63. public void handle(SerializableConsumer<R> ifOk,
  64. SerializableConsumer<String> ifError) {
  65. Objects.requireNonNull(ifOk, "ifOk cannot be null");
  66. Objects.requireNonNull(ifError, "ifError cannot be null");
  67. if (isError()) {
  68. ifError.accept(message);
  69. } else {
  70. ifOk.accept(value);
  71. }
  72. }
  73. @Override
  74. public Optional<String> getMessage() {
  75. return Optional.ofNullable(message);
  76. }
  77. @Override
  78. public boolean isError() {
  79. return message != null;
  80. }
  81. @Override
  82. public String toString() {
  83. if (isError()) {
  84. return "error(" + message + ")";
  85. } else {
  86. return "ok(" + value + ")";
  87. }
  88. }
  89. @Override
  90. public <X extends Throwable> R getOrThrow(
  91. SerializableFunction<String, ? extends X> exceptionSupplier)
  92. throws X {
  93. Objects.requireNonNull(exceptionSupplier,
  94. "Exception supplier cannot be null");
  95. if (isError()) {
  96. throw exceptionSupplier.apply(message);
  97. } else {
  98. return value;
  99. }
  100. }
  101. }