I am looking for a Java library that allows to match a sequence of objects, potentially mixing with matchers such as those of hamcrest.
Ideally I would like to write a test that can check that an iterable contains a sequence that would look like a regular expression, but for objects rather than character strings:
asser开发者_Go百科tThat(myList).inSequence(oneOrMore(any()),zeroOrMore(equals(MyObject)));
Mockito with verify is close what I would like, but some simple matchers are missing (like zeroOrMore)
Alexandre
Simplest solution i can think of is building a string with a letter for each object, and then use regex as usual.
public boolean matchObjects() {
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
ArrayList<Object> arrayList = new ArrayList<Object>();
arrayList.add(a);
arrayList.add(b);
arrayList.add(c);
arrayList.add(b);
arrayList.add(d);
Iterable<Object> iterable = arrayList;
String result = "";
for (Object object : iterable) {
if (object.equals(a))
result += "a";
else if (object.equals(b))
result += "b";
else if (object.equals(c))
result += "c";
else if (object.equals(d))
result += "d";
else
result += "x";
}
Pattern pattern = Pattern.compile("a.*b");
return pattern.matcher(result).find();
}
Take a look at this google project called ObjRegex. It sounds like what I think you're looking for. I was really interested in your question because I implemented something like this in C#, but it's proprietary and I can't share it.
I think what makes your problem different from a common regular expression matching is that you do not have the language alphabet until you get the pattern. Therefore, you need to read your pattern once to build the language alphabet. Based on your explanation, I guess that a finite set of objects are your language basic elements. Once you have the list of the language symbols you can use your own implementation of regular expression matching or you can convert your stream and pattern to a String (by mapping your objects to characters) and use one of the available regular expression APIs.
精彩评论