Skip to content

Instantly share code, notes, and snippets.

// - Single mesage take
while((e = queue.take)!=null)
{
process(e)
}
/Multiple Message - Batch processing
while(true)
{
e = clqQueue.poll();
if(e==null)
{
// Sleep and then try again
}
else
{
process(e)
FileChannel fc = new RandomAccessFile(fileName, "rw").getChannel();
// Map 8 bytes for long value.
mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, 8);
startAddress = ((DirectBuffer) mem).address();
public boolean increment() {
long orignalValue = readVolatile(startAddress);
long value = convert(orignalValue);
return UnsafeUtils.unsafe.compareAndSwapLong(null,
startAddress,orignalValue, convert(value + 1));
}
public long get() {
long orignalValue = readVolatile(startAddress);
return convert(orignalValue);
private char buffer[] = new char[0]; //char buffer for values
private int[] offset = new int[0]; // stores index of values
private int keyCount = 0; // no of keys
private int bufferIndex = 0; //current buffer index
// add element to list
public void add(CharSequence value) {
expandOffset(keyCount + 1);
expandBuffer(buffer.length + value.length());
public void forEach(ICharProcedure proc) {
for (int i = 0; i < keyCount; i++) {
int startIndex = (i == 0 ? 0 : offset[i - 1]);
if (!proc.execute(i, startIndex, offset[i], this))
break;
}
}
public static Validator newInstance(String validatorType) {
if ("INT".equals(validatorType))
return new IntValidator();
else if ("DATE".equals(validatorType))
return new DateValidator();
else if ("LOOKUPVALUE".equals(validatorType))
return new LookupValueValidator();
else if ("STRINGPATTERN".equals(validatorType))
return new StringPatternValidator();
return null;
public static Validator newInstance(String validatorClass) {
return Class.forName(validatorClass).newInstance();
}
Map<String, Validator> validators = new HashMap<String,Validator>(){
{
put("INT",new IntValidator());
put("LOOKUPVALUE",new LookupValueValidator());
put("DATE",new DateValidator());
put("STRINGPATTERN",new StringPatternValidator());
}
};
public Validator newInstance(String validatorType) {
return validators.get(validatorType);
enum ValidatorType {
INT {
public Validator create() {
return new IntValidator();
}
},
LOOKUPVALUE {
public Validator create() {
return new LookupValueValidator();
}