Skip to content

Instantly share code, notes, and snippets.

@tomasmalmsten
Created December 10, 2014 15:15
Show Gist options
  • Save tomasmalmsten/2193295def1ac413d030 to your computer and use it in GitHub Desktop.
Save tomasmalmsten/2193295def1ac413d030 to your computer and use it in GitHub Desktop.
A hamcrest matcher verifying that a string matches the pattern of a UUID
package com.tomasmalmsten.matchers
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class StringMatchesUUIDPattern extends TypeSafeMatcher<String> {
private static final String UUID_REGEX = "[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}";
@Override
protected boolean matchesSafely(String s) {
return s.matches(UUID_REGEX);
}
@Override
public void describeTo(Description description) {
description.appendText("a string matching the pattern of a UUID");
}
@Factory
public static Matcher<String> matchesThePatternOfAUUID() {
return new StringMatchesUUIDPattern();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment