|
|
|
Thursday, 06 October 2011 03:25 |
An aggregate extension method that stops as soon as the accumulated result reaches some stop value. Useful for implementing the "are all satisifed" and "is any satisfied" scenarios when testing predicates.
See http://programmaticallyspeaking.com/?p=153 for more info.
using System;
using System.Collections.Generic;
namespace ProgrammaticallySpeaking {
public static class ShortCircuit {
public static TAccumulate AggregateUntil(this IEnumerable source, TAccumulate seed, TAccumulate stop, Func func) {
var result = seed;
foreach (var element in source) {
result = func(result, element);
if (result.Equals(stop))
break;
}
return result;
}
}
}
Validation of arguments should be added, of course... Read more: |
|
|
Wednesday, 24 August 2011 18:43 |
// underscore.js mixin to find list class, not relevant to finding the index in jquery.tmpl, but useful otherwise.
_.mixin({
indexClass: function(index, length) {
var result;
result = index % 2 === 0 ? "even" : "odd";
if (index === 0) {
result += " first";
} else if (index === length - 1) {
result += " last";
}
return result;
}
});
columns = []
columns.push({title: "First Column", position: 0, terms: [{key: "One", value: 1, position: 0}, {key: "Two", value: 2, position: 1}]});
columns.push({title: "Middle Column", position: 1, terms: [{key: "A", value: "a", position: 0}, {key: "B", value: "b", position: 1}]});
columns.push({title: "Last Column", position: 2, terms: [{key: "X", value: "x", position: 0}, {key: "Y", value: "y", position: 1}]});
$("#section-template").tmpl({title: "Managing Lists in jQuery.tmpl" columns:columns}).appendTo("body")
 Read more: |
|
Tuesday, 01 March 2011 05:42 |
// #project-specific
// since implementing push is not an option
//
// in our controller, we start an async TaskInstance
// We need to wait untill the TaskInstance#getStatus != OPEN
// This usually takes 1 to 2 seconds
//
// we may not return and need to block response for a few seconds
import org.junit.Test;
import org.junit.runner.RunWith;
import org.unitils.UnitilsJUnit4TestClassRunner;
import java.util.concurrent.*;
@RunWith(UnitilsJUnit4TestClassRunner.class)
public class DeleteThisAdHocTest {
ExecutorService executor = Executors.newFixedThreadPool(1);
class MyFakeTask {
private int nrOfRetriesBeforeTaskSuccesfullyReturns;
private int counter = 0;
public MyFakeTask(int nrOfRetriesBeforeTaskSuccesfullyReturns) {
this.nrOfRetriesBeforeTaskSuccesfullyReturns = nrOfRetriesBeforeTaskSuccesfullyReturns;
}
public boolean isFinished() {
counter++;
return(counter > nrOfRetriesBeforeTaskSuccesfullyReturns);
}
}
@Test
public void testFuture_timeout(){
final MyFakeTask fakeTask = new MyFakeTask(3);
FutureTask future = new FutureTask(
new Callable() {
public String call() throws InterruptedException {
while(!fakeTask.isFinished()) {
System.out.println("Task isn't finished yet");
Thread.sleep(1000);
}
return "Aloha!";
}
});
executor.execute(future);
String result;
try{
result = future.get(5, TimeUnit.SECONDS);
} catch (ExecutionException ex){
result = "error during execution";
} catch (InterruptedException ie) {
result = "interrupted";
} catch (TimeoutException te){
result = "timeout";
} finally {
executor.shutdown();
}
System.out.println(result);
}
}
When running the code above, this results in the following output:
Task isn't finished yet
Task isn't finished yet
Task isn't finished yet
Aloha!
 Read more: |
|
|
Thursday, 06 January 2011 06:24 |
Java:
// the key must be a String
Map result = new HashMap();
Flex:
// injected
public var countryLabelRegexes:Object;
// example key: NATIONAL-BE
var key:String = label + REGEX_KEY_DELIMITER + (legalAddress.country as Country).isoCountryCode;
// check if the dynamic object has a property NATIONAL-BE (equivalent to java map.hasKey("NATIONAL-BE");
if(countryLabelRegexes.hasOwnProperty(key)) {
// retrieve the value
result = countryLabelRegexes[key];
}
 Read more: |
|
|
|
|
|
|
|