001/**
002 * Powerunit - A JDK1.8 test framework
003 * Copyright (C) 2014 Mathieu Boretti.
004 *
005 * This file is part of Powerunit
006 *
007 * Powerunit is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * Powerunit is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
019 */
020package ch.powerunit;
021
022import java.util.function.Function;
023
024/**
025 * This is an intermediate and optional step on assertion to cast an object into
026 * a sub-object.
027 * 
028 * @author borettim
029 * @since 0.3.0
030 * @param <T>
031 *            the object type.
032 */
033public interface AssertThatCastableObject<T> extends AssertThatObject<T> {
034        /**
035         * Cast the received value into a sub-class of the receiver, before applying
036         * the matcher.
037         * <p>
038         * For example, we have <code>Object myNumber =...;</code> which is known to
039         * be an <code>Integer</code>. It is possible to convert it by using :
040         * 
041         * <pre>
042         * assertThat(myNumber).as(Integer.class).is(10);
043         * </pre>
044         * 
045         * @param clazz
046         *            the target class
047         * @return {@link AssertThatObject the assertion on the result of the
048         *         conversion}.
049         * @param <P>
050         *            The subtype for the conversion
051         */
052        <P extends T> AssertThatObject<P> as(Class<P> clazz);
053
054        /**
055         * Convert a received object into another one, based on a passed converter.
056         * <p>
057         * For example, we have <code>String myString =...;</code> which is known to
058         * be a <code>String</code> representing an <code>Integer</code>. It is
059         * possible to convert it by using:
060         * 
061         * <pre>
062         * assertThat(myString).as(Integer.class, Integer::valueOf).is(1);
063         * </pre>
064         * 
065         * @param targetClass
066         *            he target class
067         * @param converter
068         *            the converter to be used to converter the object to the target
069         *            class.
070         * @return {@link AssertThatCastableObject the assertion on the result of
071         *         the conversion}.
072         * @param <P>
073         *            the target type
074         * @since 0.4.0
075         */
076        <P> AssertThatCastableObject<P> as(Class<P> targetClass,
077                        Function<T, P> converter);
078}