Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2012 the original author or authors.
*
* 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.
*/
package com.bloidonia.groovy.extensions
/**
* @author Tim Yates
*/
class ObjectExtensionMethods {
/**
* Will execute closure passing 'self' as a parameter to the closure.
*
* When the closure has run, self will be flattened (if it is a list)
* and close will be called on every item in it (if it responds to a
* zero-arg version of 'close').
*
*
* // A class that responds to close()
* class A {
* boolean isClosed = false
* def close() {
* isClosed = true
* }
* }
*
* // Make an instance and test
* def obj = new A()
* def result = obj.withClosable {
* 'cool'
* }
*
* // Check the result
* assert result == 'cool'
* // Assert obj is closed
* assert obj.isClosed
*
* def list = [ new A(), 'tim', new A() ]
* result = list.withClosable {
* 'also cool'
* }
*
* // Check the result
* assert result == 'also cool'
* // Assert all A instances are closed
* assert list.grep( A )*.isClosed.every()
*
*
* @param self the object to call 'close' on after the closure has run
* @param c the closure to run
* @return the result of calling the closure
*/
static Object withClosable( Object self, Closure c ) {
try {
c( self )
}
finally {
[ self ].flatten().each {
if( it.respondsTo( 'close' )?.paramsCount.contains( 0 ) ) {
it.close()
}
}
}
}
/**
* Taken from Ruby, the tap method executes the closure
* using the object as the delegate – internally, it just calls
* self.with c and then it returns the object it was
* called on.
*
* This allows you to tap into a method chain
*
*
*
* @since 0.5
* @param self the object to call the delegate closure on
* @param c the closure to run
* @return the the object this method was called on
*/
static Object tap( Object self, Closure c ) {
self.with c
self
}
}