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.

SimpleResult.java 3.2KB

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