001 /* 002 * Copyright (c) 2007-2015 Concurrent, Inc. All Rights Reserved. 003 * 004 * Project and contact information: http://www.cascading.org/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021 package cascading.operation.assertion; 022 023 import java.beans.ConstructorProperties; 024 025 import cascading.operation.AssertionException; 026 import cascading.operation.AssertionLevel; 027 import cascading.operation.BaseOperation; 028 import cascading.operation.PlannedOperation; 029 import cascading.operation.PlannerLevel; 030 import cascading.util.TraceUtil; 031 032 /** 033 * Class BaseAssertion is a convenience class for {@link cascading.operation.Assertion} implementations. Subclassing 034 * this class is not required, but does provide some convenience functions for signaling assertion failures. 035 * 036 * @see cascading.operation.Assertion 037 * @see cascading.operation.GroupAssertion 038 * @see cascading.operation.ValueAssertion 039 */ 040 public abstract class BaseAssertion<C> extends BaseOperation<C> implements PlannedOperation<C> 041 { 042 /** Field message */ 043 private String message; 044 045 protected BaseAssertion() 046 { 047 } 048 049 @ConstructorProperties( {"message"} ) 050 protected BaseAssertion( String message ) 051 { 052 this.message = message; 053 } 054 055 @ConstructorProperties( {"numArgs"} ) 056 protected BaseAssertion( int numArgs ) 057 { 058 super( numArgs ); 059 } 060 061 @ConstructorProperties( {"numArgs", "message"} ) 062 protected BaseAssertion( int numArgs, String message ) 063 { 064 super( numArgs ); 065 this.message = message; 066 } 067 068 public String getMessage() 069 { 070 return message; 071 } 072 073 @Override 074 public boolean supportsPlannerLevel( PlannerLevel plannerLevel ) 075 { 076 return plannerLevel instanceof AssertionLevel; 077 } 078 079 protected void fail() 080 { 081 throwFail( TraceUtil.formatTrace( this, getMessage() ) ); 082 } 083 084 protected void fail( Object... args ) 085 { 086 throwFail( TraceUtil.formatTrace( this, getMessage() ), args ); 087 } 088 089 /** 090 * Static method throwFail should be used to throw an {@link AssertionException}. 091 * 092 * @param message of type String, the message to be thrown 093 */ 094 public static void throwFail( String message ) 095 { 096 throw new AssertionException( message ); 097 } 098 099 /** 100 * Static method throwFail should be used to throw an {@link AssertionException}. 101 * 102 * @param message of type String, the message to be thrown as a format string 103 * @param args of type Object[], the values to be passed into the message format string 104 */ 105 public static void throwFail( String message, Object... args ) 106 { 107 throw new AssertionException( String.format( message, args ) ); 108 } 109 110 @Override 111 public boolean equals( Object object ) 112 { 113 if( this == object ) 114 return true; 115 if( !( object instanceof BaseAssertion ) ) 116 return false; 117 if( !super.equals( object ) ) 118 return false; 119 120 BaseAssertion that = (BaseAssertion) object; 121 122 if( message != null ? !message.equals( that.message ) : that.message != null ) 123 return false; 124 125 return true; 126 } 127 128 @Override 129 public int hashCode() 130 { 131 int result = super.hashCode(); 132 result = 31 * result + ( message != null ? message.hashCode() : 0 ); 133 return result; 134 } 135 }