1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6package org.mockito.internal.matchers; 7 8import java.io.Serializable; 9import java.util.regex.Pattern; 10import org.mockito.ArgumentMatcher; 11 12public class Matches implements ArgumentMatcher<Object>, Serializable { 13 14 private final Pattern pattern; 15 16 public Matches(String regex) { 17 this(Pattern.compile(regex)); 18 } 19 20 public Matches(Pattern pattern) { 21 this.pattern = pattern; 22 } 23 24 public boolean matches(Object actual) { 25 return (actual instanceof String) && pattern.matcher((String) actual).matches(); 26 } 27 28 public String toString() { 29 return "matches(\"" + pattern.pattern().replaceAll("\\\\", "\\\\\\\\") + "\")"; 30 } 31} 32