Skip to content

Instantly share code, notes, and snippets.

@hstaudacher
Last active December 19, 2015 02:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hstaudacher/5883964 to your computer and use it in GitHub Desktop.
Save hstaudacher/5883964 to your computer and use it in GitHub Desktop.
Util to throw exceptions in a nicer way then if( .. ) throw...
/*******************************************************************************
* Copyright (c) 2013 EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* EclipseSource - initial API and implementation
******************************************************************************/
public class Clauses {
public static class Clause {
private final boolean condition;
public Clause( boolean condition ) {
this.condition = condition;
}
public void throwIllegalState( String message ) {
if( condition ) {
throw new IllegalStateException( message );
}
}
public void throwIllegalArgument( String message ) {
if( condition ) {
throw new IllegalArgumentException( message );
}
}
}
private static Clause TRUE_CLAUSE = new Clause( true );
private static Clause FALSE_CLAUSE = new Clause( false );
public static Clause when( boolean condition ) {
return condition ? TRUE_CLAUSE : FALSE_CLAUSE;
}
public static Clause whenNot( boolean condition ) {
return when( !condition );
}
public static Clause whenNull( Object object ) {
return when( object == null );
}
private Clauses() {
// prevent instantiation
}
}
/*******************************************************************************
* Copyright (c) 2013 EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* EclipseSource - initial API and implementation
******************************************************************************/
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class ClausesTest {
@Test( expected = IllegalStateException.class )
public void testThrowsISEForTrueCondition() {
Clauses.when( true ).throwIllegalState( "" );
}
@Test( expected = IllegalArgumentException.class )
public void testThrowsIAEForTrueCondition() {
Clauses.when( true ).throwIllegalArgument( "" );
}
@Test( expected = IllegalStateException.class )
public void testThrowsISEForInvertedTrueCondition() {
Clauses.whenNot( false ).throwIllegalState( "" );
}
@Test( expected = IllegalArgumentException.class )
public void testThrowsIAEForInvertedTrueCondition() {
Clauses.whenNot( false ).throwIllegalArgument( "" );
}
@Test( expected = IllegalStateException.class )
public void testThrowsISEForNullCondition() {
Clauses.whenNull( null ).throwIllegalState( "" );
}
@Test( expected = IllegalArgumentException.class )
public void testThrowsIAEForNullCondition() {
Clauses.whenNull( null ).throwIllegalArgument( "" );
}
@Test
public void testThrowsISEWithDelegatedMessage() {
try {
Clauses.when( true ).throwIllegalState( "foo" );
} catch( IllegalStateException ise ) {
assertEquals( "foo", ise.getMessage() );
}
}
@Test( expected = IllegalArgumentException.class )
public void testThrowsIAEWithDelegatedMessage() {
try {
Clauses.when( true ).throwIllegalArgument( "foo" );
} catch( IllegalStateException ise ) {
assertEquals( "foo", ise.getMessage() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment