MultInverse implements Scalar<T> { @Override public T value(final Field<T> field) { if (field.equals(this.inverse, new AddIdentity<T>())) { throw new IllegalArgumentException( String.format( "Impossible to inverse addition neutral element: %s", this.inverse ) ); } return field.multiplication().inverse(field.actual(this.inverse)); } MultInverse(final Scalar<T> inv); @Override T value(final Field<T> field);  }
@Test public void multInverseDelegatesToFieldMultiplication() { final MkMultiplication<Object> mult = new MkMultiplication<>(new Object()); final SpyField<Object> field = new SpyField<>( new MkField<Object>(new Object(), mult) ); final Scalar<Object> scalar = new Scalar.Default<>(new Object()); new MultInverse<>(scalar).value(field); MatcherAssert.assertThat( field.calls().multiplicationed(), Matchers.is(true) ); MatcherAssert.assertThat( mult.inverted(), Matchers.is(true) ); }
@Test public void throwsExceptionWhenInvertingAddIdentity() { this.thrown.expect(IllegalArgumentException.class); new MultInverse<Double>(new AddIdentity<>()).value(new Decimal()); }