    001    /*  Copyright (c) 2000-2006 hamcrest.org
    002     */
    003    package org.hamcrest.core;
    004
    005    import org.hamcrest.BaseMatcher;
    006    import org.hamcrest.Description;
    007    import org.hamcrest.Factory;
    008    import org.hamcrest.Matcher;
    009
    010
    011    /**
    012     * Is the value the same object as another value?
    013     */
    014    public class IsSame<T> extends BaseMatcher<T> {
    015        private final T object;
    016
    017        public IsSame(T object) {
    018            this.object = object;
    019        }
    020
    021        @Override
    022        public boolean matches(Object arg) {
    023            return arg == object;
    024        }
    025
    026        @Override
    027        public void describeTo(Description description) {
    028            description.appendText("sameInstance(")
    029                    .appendValue(object)
    030                    .appendText(")");
    031        }
    032
    033        /**
    034         * Creates a matcher that matches only when the examined object is the same instance as
    035         * the specified target object.
    036         *
    037         * @param target
    038         *     the target instance against which others should be assessed
    039         */
    040        @Factory
    041        public static <T> Matcher<T> sameInstance(T target) {
    042            return new IsSame<T>(target);
    043        }
    044
    045        /**
    046         * Creates a matcher that matches only when the examined object is the same instance as
    047         * the specified target object.
    048         *
    049         * @param target
    050         *     the target instance against which others should be assessed
    051         */
    052        @Factory
    053        public static <T> Matcher<T> theInstance(T target) {
    054            return new IsSame<T>(target);
    055        }
    056    }