Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2015, nwillc@gmail.com
   *
   * Permission to use, copy, modify, and/or distribute this software for any
   * purpose with or without fee is hereby granted, provided that the above
   * copyright notice and this permission notice appear in all copies.
   *
   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
 package com.github.nwillc.contracts;
 
 import org.junit.Test;
 
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;

This contract checks:
  • compare with argument that can't be cast to correct type throws ClassCastException
  • returns 0 on equality
  • returns less then zero on less then
  • returns greater then zero on greater
  • Null values throw a NullPointerException (see below)
The jdk contract does not specify how nulls are handled. The possibilities are nulls constitute an error, they are ordered first, or ordered last. This contract defaults to the error case, but the behavior can be controlled by calling setNulls() with the appropriate value.

Since:
1.6.5
 
 public abstract class ComparatorContract<T> {

Since:
1.6.6
 
 	protected enum Nulls {
Nulls throw NullPointerException.
 
 		ERROR,
Nulls are considered to be ordered first.
 
 		NULLS_FIRST,
Nulls are considered to be ordered last.
 
 		NULLS_LAST
 	}
 
 	private Comparator<T> comparator;
 	private T value;
 	private T lesserValue;
 	private Nulls nulls = .;
 
 	protected abstract Comparator<T> getComparator();
 	protected abstract T getValue();
 	protected abstract T getLesserValue();

Parameters:
nulls How nulls should be treated.
Since:
1.6.6
 
 	public void setNulls(Nulls nulls) {
 		this. = nulls;
 	}
 
 	public void contractSetup() throws Exception {
 		assertThat().describedAs("getComparator must return non null value").isNotNull();
 		 = getValue();
 	}
 
 	@SuppressWarnings("unchecked")
 	@Test
 	public void shouldThrowExceptionForBadCast() throws Exception {
 		try {
 			.compare(, (T)this);
 		} catch (ClassCastException e) {}
 	}
 
 	@Test
 	public void shouldReturnZeroOnEquality() throws Exception {
 	}
 
 	@Test
 	public void shouldReturnNegativeOnLessThan() throws Exception {
 	}
 
 	@Test
 	public void shouldReturnPositiveOnGreaterThan() throws Exception {
	}

Since:
1.6.6
	public void shouldThrowExceptionIfNullsError() throws Exception {
		if ( != .) {
			return;
		}
		try {
			.compare(nullnull);
catch (NullPointerException e) {}
	}
New to GrepCode? Check out our FAQ X