Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created December 24, 2011 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/1516728 to your computer and use it in GitHub Desktop.
Save mike-neck/1516728 to your computer and use it in GitHub Desktop.
hamcrest extension for comparing values.
package org.hamcrest.core;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
/**
* Copyright (C) 2011 mike_neck
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
public class LessThan<T extends Comparable<T>> extends BaseMatcher<T> {
private final T matcher;
private final Class<T> klass;
public LessThan(T matcher) {
this.matcher = matcher;
this.klass = (Class<T>) matcher.getClass();
}
@Override
public boolean matches(Object o) {
if (o.getClass() == klass) {
T object = klass.cast(o);
int result = matcher.compareTo(object);
return result > 0;
} else {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("is less than ").appendValue(matcher);
}
@Factory
public static <T extends Comparable<T>> Matcher<T> lessThan(T value) {
return new LessThan<T>(value);
}
}
package org.hamcrest.core;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
/**
* Copyright (C) 2011 mike_neck
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
public class LessThanEqual<T extends Comparable<T>> extends BaseMatcher<T> {
private final T matcher;
private final Class<T> klass;
public LessThanEqual(T matcher) {
this.matcher = matcher;
this.klass = (Class<T>) matcher.getClass();
}
@Override
public boolean matches(Object o) {
if (o.getClass() == klass) {
T object = klass.cast(o);
int result = matcher.compareTo(object);
return result >= 0;
} else {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("is less than or equals ").appendValue(matcher);
}
@Factory
public static <T extends Comparable<T>> Matcher<T> lessThanEqual(T matcher) {
return new LessThanEqual<T>(matcher);
}
}
package org.hamcrest.core;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
/**
* Copyright (C) 2011 mike_neck
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
public class MoreThan<T extends Comparable<T>> extends BaseMatcher<T> {
private final T matcher;
private final Class<T> klass;
public MoreThan(T matcher) {
this.matcher = matcher;
this.klass = (Class<T>) matcher.getClass();
}
@Override
public boolean matches(Object o) {
if(o.getClass() == klass) {
T object = klass.cast(o);
int result = matcher.compareTo(object);
return result < 0;
} else {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("more than ").appendValue(matcher);
}
@Factory
public static <T extends Comparable<T>> Matcher<T> moreThan(T value) {
return new MoreThan<T>(value);
}
}
package org.hamcrest.core;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
/**
* Copyright (C) 2011 mike_neck
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
public class MoreThanEqual<T extends Comparable<T>> extends BaseMatcher<T> {
private final T matcher;
private final Class<T> klass;
public MoreThanEqual(T matcher) {
this.matcher = matcher;
this.klass = (Class<T>) matcher.getClass();
}
@Override
public boolean matches(Object o) {
if (o.getClass() == klass) {
T object = klass.cast(o);
int result = matcher.compareTo(object);
return result <= 0;
} else {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("is more than or equals ").appendValue(matcher);
}
@Factory
public static <T extends Comparable<T>> MoreThanEqual<T> moreThanEqual(T value) {
return new MoreThanEqual<T>(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment