Esempio n. 1
0
        /// <summary>
        /// Combines this instance with another.
        /// <para>
        /// If both instances contain lists of the same type, the combining function will
        /// often be {@code Guavate::concatToList}.
        /// </para>
        /// <para>
        /// It is strongly advised to ensure that the function cannot throw an exception.
        ///
        /// </para>
        /// </summary>
        /// @param <U>  the type of the value in the other instance </param>
        /// @param <R>  the type of the value in the returned result </param>
        /// <param name="other">  the other instance </param>
        /// <param name="combiner">  the function that combines the two values </param>
        /// <returns> the combined instance of value and failures </returns>
        public ValueWithFailures <R> combinedWith <U, R>(ValueWithFailures <U> other, System.Func <T, U, R> combiner)
        {
            R combinedValue = Objects.requireNonNull(combiner(value, other.value));
            ImmutableList <FailureItem> combinedFailures = concatToList(failures, other.failures);

            return(ValueWithFailures.of(combinedValue, combinedFailures));
        }
Esempio n. 2
0
        /// <summary>
        /// Processes the value by applying a function that returns another result.
        /// <para>
        /// This operation allows post-processing of a result value.
        /// This is similar to <seealso cref="#map(Function)"/> but the function returns a {@code ValueWithFailures}.
        /// The result of this method consists of the transformed value, and the combined list of failures.
        /// </para>
        /// <para>
        /// It is strongly advised to ensure that the function cannot throw an exception.
        ///
        /// </para>
        /// </summary>
        /// @param <R>  the type of the value in the returned result </param>
        /// <param name="function">  the function to transform the value with </param>
        /// <returns> the transformed instance of value and failures </returns>
//JAVA TO C# CONVERTER TODO TASK: There is no .NET equivalent to the Java 'super' constraint:
//ORIGINAL LINE: public <R> ValueWithFailures<R> flatMap(java.util.function.Function<? super T, ValueWithFailures<R>> function)
        public ValueWithFailures <R> flatMap <R, T1>(System.Func <T1> function)
        {
            ValueWithFailures <R>       transformedValue = Objects.requireNonNull(function(value));
            ImmutableList <FailureItem> combinedFailures = ImmutableList.builder <FailureItem>().addAll(this.failures).addAll(transformedValue.failures).build();

            return(ValueWithFailures.of(transformedValue.value, combinedFailures));
        }
Esempio n. 3
0
        //-------------------------------------------------------------------------
        public virtual void test_of_array_noFailures()
        {
            ValueWithFailures <string> test = ValueWithFailures.of("success");

            assertEquals(test.hasFailures(), false);
            assertEquals(test.Value, "success");
            assertEquals(test.Failures, ImmutableList.of());
        }
Esempio n. 4
0
        public virtual void test_of_list()
        {
            ValueWithFailures <string> test = ValueWithFailures.of("success", ImmutableList.of(FAILURE1, FAILURE2));

            assertEquals(test.hasFailures(), true);
            assertEquals(test.Value, "success");
            assertEquals(test.Failures, ImmutableList.of(FAILURE1, FAILURE2));
        }
Esempio n. 5
0
        public virtual void test_of_supplier_success()
        {
            ValueWithFailures <string> test = ValueWithFailures.of("", () => "A");

            assertEquals(test.hasFailures(), false);
            assertEquals(test.Value, "A");
            assertEquals(test.Failures, ImmutableList.of());
        }
Esempio n. 6
0
        public virtual void test_combinedWith_differentTypes()
        {
            ValueWithFailures <bool>   @base = ValueWithFailures.of(true, ImmutableList.of(FAILURE1));
            ValueWithFailures <int>    other = ValueWithFailures.of(Convert.ToInt32(1), ImmutableList.of(FAILURE2));
            ValueWithFailures <string> test  = @base.combinedWith(other, (a, b) => a.ToString() + b.ToString());

            assertEquals(test.Value, "true1");
            assertEquals(test.Failures, ImmutableList.of(FAILURE1, FAILURE2));
        }
Esempio n. 7
0
        // -------------------------------------------------------------------------
        public virtual void test_combinedWith()
        {
            ValueWithFailures <IList <string> > @base = ValueWithFailures.of(ImmutableList.of("a"), ImmutableList.of(FAILURE1));
            ValueWithFailures <IList <string> > other = ValueWithFailures.of(ImmutableList.of("b", "c"), ImmutableList.of(FAILURE2));
            ValueWithFailures <IList <string> > test  = @base.combinedWith(other, Guavate.concatToList);

            assertEquals(test.Value, ImmutableList.of("a", "b", "c"));
            assertEquals(test.Failures, ImmutableList.of(FAILURE1, FAILURE2));
        }
Esempio n. 8
0
        //-------------------------------------------------------------------------
        public virtual void test_map()
        {
            ValueWithFailures <IList <string> > @base = ValueWithFailures.of(ImmutableList.of("1", "2"), ImmutableList.of(FAILURE1));
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            ValueWithFailures <IList <int> > test = @base.map(list => list.Select(s => Convert.ToInt32(s)).collect(toImmutableList()));

            assertEquals(test.Value, ImmutableList.of(Convert.ToInt32(1), Convert.ToInt32(2)));
            assertEquals(test.Failures, ImmutableList.of(FAILURE1));
        }
Esempio n. 9
0
        //-------------------------------------------------------------------------
        public virtual void coverage()
        {
            ValueWithFailures <string> test = ValueWithFailures.of("success", FAILURE1, FAILURE2);

            coverImmutableBean(test);
            ValueWithFailures <string> test2 = ValueWithFailures.of("test");

            coverBeanEquals(test, test2);
        }
Esempio n. 10
0
        public virtual void test_flatMap()
        {
            ValueWithFailures <IList <string> > @base = ValueWithFailures.of(ImmutableList.of("1", "a", "2"), ImmutableList.of(FAILURE1));
            ValueWithFailures <IList <int> >    test  = @base.flatMap(this.flatMapFunction);

            assertEquals(test.Value, ImmutableList.of(Convert.ToInt32(1), Convert.ToInt32(2)));
            assertEquals(test.Failures.size(), 2);
            assertEquals(test.Failures.get(0), FAILURE1);
            assertEquals(test.Failures.get(1).Reason, FailureReason.INVALID);
        }
Esempio n. 11
0
 /// <summary>
 /// Creates an instance using a supplier.
 /// <para>
 /// If the supplier succeeds normally, the supplied value will be returned.
 /// If the supplier fails, the empty value will be returned along with a failure.
 ///
 /// </para>
 /// </summary>
 /// @param <T> the type of the value </param>
 /// <param name="emptyValue">  the empty value </param>
 /// <param name="supplier">  supplier of the result value </param>
 /// <returns> an instance containing the supplied value, or a failure if an exception is thrown </returns>
 public static ValueWithFailures <T> of <T>(T emptyValue, System.Func <T> supplier)
 {
     try
     {
         return(of(supplier()));
     }
     catch (Exception ex)
     {
         return(ValueWithFailures.of(emptyValue, FailureItem.of(FailureReason.ERROR, ex)));
     }
 }
Esempio n. 12
0
        public virtual void test_of_supplier_failure()
        {
            ValueWithFailures <string> test = ValueWithFailures.of("", () =>
            {
                throw new System.ArgumentException();
            });

            assertEquals(test.hasFailures(), true);
            assertEquals(test.Value, "");
            assertEquals(test.Failures.size(), 1);
            assertEquals(test.Failures.get(0).Reason, FailureReason.ERROR);
        }
Esempio n. 13
0
        //-------------------------------------------------------------------------
        public virtual void test_toValueWithFailures()
        {
            IList <double> testList = ImmutableList.of(5d, 6d, 7d);
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            ValueWithFailures <double> result = testList.Select(value => mockCalc(value)).collect(ValueWithFailures.toValueWithFailures(1d, (val1, val2) => val1 * val2));

            assertEquals(result.Value, 210d);     //5 * 6 * 7 = 210
            IList <FailureItem> failures = result.Failures;

            assertEquals(failures.Count, 3);     //One failure item for each element in testList.
            assertEquals(failures[0].Message, Messages.format("Error calculating result for input value {}", 5d));
            assertEquals(failures[1].Message, Messages.format("Error calculating result for input value {}", 6d));
            assertEquals(failures[2].Message, Messages.format("Error calculating result for input value {}", 7d));
        }
Esempio n. 14
0
        //-----------------------------------------------------------------------
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }
            if (obj != null && obj.GetType() == this.GetType())
            {
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: ValueWithFailures<?> other = (ValueWithFailures<?>) obj;
                ValueWithFailures <object> other = (ValueWithFailures <object>)obj;
                return(JodaBeanUtils.equal(value, other.value) && JodaBeanUtils.equal(failures, other.failures));
            }
            return(false);
        }
Esempio n. 15
0
        private ValueWithFailures <IList <int> > flatMapFunction(IList <string> input)
        {
            IList <int>         integers = new List <int>();
            IList <FailureItem> failures = new List <FailureItem>();

            foreach (string str in input)
            {
                try
                {
                    integers.Add(Convert.ToInt32(str));
                }
                catch (System.FormatException ex)
                {
                    failures.Add(FailureItem.of(FailureReason.INVALID, ex));
                }
            }
            return(ValueWithFailures.of(integers, failures));
        }
Esempio n. 16
0
        /// <summary>
        /// Processes the value by applying a function that alters the value.
        /// <para>
        /// This operation allows post-processing of a result value.
        /// The specified function represents a conversion to be performed on the value.
        /// </para>
        /// <para>
        /// It is strongly advised to ensure that the function cannot throw an exception.
        ///
        /// </para>
        /// </summary>
        /// @param <R>  the type of the value in the returned result </param>
        /// <param name="function">  the function to transform the value with </param>
        /// <returns> the transformed instance of value and failures </returns>
//JAVA TO C# CONVERTER TODO TASK: There is no .NET equivalent to the Java 'super' constraint:
//ORIGINAL LINE: public <R> ValueWithFailures<R> map(java.util.function.Function<? super T, ? extends R> function)
        public ValueWithFailures <R> map <R, T1>(System.Func <T1> function) where T1 : R
        {
            R transformedValue = Objects.requireNonNull(function(value));

            return(ValueWithFailures.of(transformedValue, this.failures));
        }
Esempio n. 17
0
        /// <summary>
        /// Returns a collector that can be used to create a ValueWithFailure instance from a stream of ValueWithFailure
        /// instances.
        /// <para>
        /// The <seealso cref="Collector"/> returned performs a reduction of its <seealso cref="ValueWithFailures"/> input elements under a
        /// specified <seealso cref="BinaryOperator"/> using the provided identity.
        ///
        /// </para>
        /// </summary>
        /// @param <T>  the type of the success value in the <seealso cref="ValueWithFailures"/> </param>
        /// <param name="identityValue">  the identity value </param>
        /// <param name="operator">  the operator used for the reduction. </param>
        /// <returns> a <seealso cref="Collector"/> </returns>
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: public static <T> java.util.stream.Collector<ValueWithFailures<T>, ?, ValueWithFailures<T>> toValueWithFailures(T identityValue, java.util.function.BinaryOperator<T> operator)
        public static Collector <ValueWithFailures <T>, ?, ValueWithFailures <T> > toValueWithFailures <T>(T identityValue, System.Func <T, T, T> @operator)
        {
            System.Func <ValueWithFailures <T>, ValueWithFailures <T>, ValueWithFailures <T> > reduceFunction = (result1, result2) => result1.combinedWith(result2, @operator);

            return(Collectors.reducing(ValueWithFailures.of(identityValue), reduceFunction));
        }
Esempio n. 18
0
        private static ValueWithFailures <double> mockCalc(double value)
        {
            FailureItem failure = FailureItem.of(FailureReason.CALCULATION_FAILED, "Error calculating result for input value {}", value);

            return(ValueWithFailures.of(value, ImmutableList.of(failure)));
        }
Esempio n. 19
0
        public virtual void test_serialization()
        {
            ValueWithFailures <string> test = ValueWithFailures.of("success", FAILURE1, FAILURE2);

            assertSerialization(test);
        }