1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5package org.mockito.internal.matchers; 6 7import java.io.Serializable; 8 9import org.hamcrest.*; 10import org.mockito.internal.debugging.LocationImpl; 11import org.mockito.invocation.Location; 12 13@SuppressWarnings("unchecked") 14public class LocalizedMatcher implements Matcher, ContainsExtraTypeInformation, CapturesArguments, MatcherDecorator, Serializable { 15 16 private static final long serialVersionUID = 6748641229659825725L; 17 private final Matcher actualMatcher; 18 private Location location; 19 20 public LocalizedMatcher(Matcher actualMatcher) { 21 this.actualMatcher = actualMatcher; 22 this.location = new LocationImpl(); 23 } 24 25 public void _dont_implement_Matcher___instead_extend_BaseMatcher_() { 26 // yeah right 27 } 28 29 public boolean matches(Object item) { 30 return actualMatcher.matches(item); 31 } 32 33 public void describeTo(Description description) { 34 actualMatcher.describeTo(description); 35 } 36 37 public Location getLocation() { 38 return location; 39 } 40 41 @Override 42 public String toString() { 43 return "Localized: " + this.actualMatcher; 44 } 45 46 public SelfDescribing withExtraTypeInfo() { 47 if (actualMatcher instanceof ContainsExtraTypeInformation) { 48 return ((ContainsExtraTypeInformation) actualMatcher).withExtraTypeInfo(); 49 } else { 50 return this; 51 } 52 } 53 54 public boolean typeMatches(Object object) { 55 return actualMatcher instanceof ContainsExtraTypeInformation 56 && ((ContainsExtraTypeInformation) actualMatcher).typeMatches(object); 57 } 58 59 public void captureFrom(Object argument) { 60 if (actualMatcher instanceof CapturesArguments) { 61 ((CapturesArguments) actualMatcher).captureFrom(argument); 62 } 63 } 64 65 //TODO: refactor other 'delegated interfaces' to use the MatcherDecorator feature 66 public Matcher getActualMatcher() { 67 return actualMatcher; 68 } 69}