text:
var ShaderCall = require('../renderer/ShaderCall');
var Matrix3 = require('../math/Matrix3');
var Matrix4 = require('../math/Matrix4');
var World = require('../entities/World');
var RenderQueue = require('../renderer/RenderQueue');
var ObjectUtils = require('../util/ObjectUtils');
var SystemBus = require('../entities/SystemBus');

/* global WebGLRenderingContext */

/**
 * Defines vertex and fragment shader and uniforms to shader callbacks
 * @param {string} name Shader name (mostly for debug/tool use)
 * @param {ShaderDefinition} shaderDefinition Shader data
 *
 *
 * {
 *    vshader: [required] vertex shader source
 *    fshader: [required] fragment shader source
 *    defines : shader definitions (becomes #define)
 *    attributes : attribute bindings
 *       attribute bindings need to map to an attribute in the meshdata being rendered
 *    uniforms : uniform bindings
 *       uniform bindings can be a value (like 2.5 or [1, 2]) or a function
 * }
 *
 */
function Shader(name, shaderDefinition) {
	if (!shaderDefinition.vshader || !shaderDefinition.fshader) {
		throw new Error('Missing shader sources for shader: ' + name);
	}

	this.originalShaderDefinition = shaderDefinition;
	this.shaderDefinition = shaderDefinition;

	this.origVertexSource = shaderDefinition.vshader;
	this.origFragmentSource = shaderDefinition.fshader;

	/** The shader name
	 * @type {string}
	 */
	this.name = name;

	this.shaderProgram = null;
	this.vertexShader = null;
	this.fragmentShader = null;

	/**
	 * The renderer where the program and shaders were allocated.
	 * @type {WebGLRenderingContext}
	 */
	this.renderer = null;

	/**
	 * Attributes detected in the shader source code.
	 * Maps attribute variable's name to {format: string}
	 * @type {Object<string, object>}}
	 */
	this.attributeMapping = {};

	/**
	 * Maps attribute variable's name to attribute location (from getAttribLocation).
	 * @type {Object<string, number>}
	 */
	this.attributeIndexMapping = {};

	/**
	 * Uniforms detected in the shader source code.
	 * Maps variable name to {format: string}.
	 * @type {Object<string, object>}
	 */
	this.uniformMapping = {};

	/**
	 * Maps uniform variable name to ShaderCall object.
	 * @type {Object<string, ShaderCall>}
	 */
	this.uniformCallMapping = {};

	/**
	 * Texture slots detected in the shader source code.
	 * Will be an array of {format: string, name: string}
	 * @type {Array}
	 */
	this.textureSlots = [];
	this.textureSlotsNaming = {};
	this.textureIndex = 0;

	this.currentCallbacks = {};

	this.overridePrecision = shaderDefinition.precision || null;
	this.processors = shaderDefinition.processors;
	this.builder = shaderDefinition.builder;
	this.defines = shaderDefinition.defines || {};
	this.attributes = shaderDefinition.attributes || {};
	this.uniforms = shaderDefinition.uniforms || {};
	this.defineKey = shaderDefinition.defineKey || '';
	this.defineKeyDirty = true;
	this.frameStart = true;
	this.attributeKeys = null;
	this.matchedUniforms = [];

	/** Determines the order in which an object is drawn. There are four pre-defined render queues:
	 *

	 *
RenderQueue.BACKGROUND = Rendered before any other objects. Commonly used for skyboxes and the likes (0-999)
	 *
RenderQueue.OPAQUE = Used for most objects, typically opaque geometry. Rendered front to back (1000-1999)
	 *
RenderQueue.TRANSPARENT = For all alpha-blended objects. Rendered back to front (2000-2999)
	 *
RenderQueue.OVERLAY = For overlay effects like lens-flares etc (3000+)
	 *

	 * By default materials use the render queue of the shader. See {@link RenderQueue} for more info
	 * @type {number}
	 */
	this.renderQueue = RenderQueue.OPAQUE;

	// this._id = Shader.id++;
	if (Shader.cache.has(shaderDefinition)) {
		this._id = Shader.cache.get(shaderDefinition);
	} else {
		this._id = Shader.cache.size;
		Shader.cache.set(shaderDefinition, this._id);
	}
	// console.log('creating shader', this._id, this.name);

	this.errorOnce = false;

	this.vertexSource = typeof this.origVertexSource === 'function' ? this.origVertexSource() : this.origVertexSource;
	this.fragmentSource = typeof this.origFragmentSource === 'function' ? this.origFragmentSource() : this.origFragmentSource;

	// @ifdef DEBUG
	Object.seal(this);
	// @endif
}

// Shader.id = 0;
Shader.cache = new Map();

Shader.prototype.clone = function () {
	return new Shader(this.name, ObjectUtils.deepClone({
		precision: this.precision,
		processors: this.processors,
		builder: this.builder,
		defines: this.defines,
		attributes: this.attributes,
		uniforms: this.uniforms,
		vshader: this.origVertexSource,
		fshader: this.origFragmentSource,
		defineKey: this.defineKey
	}));
};

Shader.prototype.cloneOriginal = function () {
	return new Shader(this.name, this.originalShaderDefinition);
};

/**
 * Compiles a shader and does not apply it
 * @private
 * @param renderer
 */
Shader.prototype.precompile = function (renderer) {
	if (this.shaderProgram === null) {
		this._investigateShaders();
		this.addDefines(this.defines);
		this.addPrecision(this.overridePrecision || renderer.shaderPrecision);
		this.compile(renderer);
	}
};

/*
 * Matches an attribute or uniform variable declaration.
 *
 * Match groups:
 *
 *   1: type (attribute|uniform)
 *   2: format (float|int|bool|vec2|vec3|vec4|mat2|mat3|mat4|sampler2D|sampler3D|samplerCube)
 *   3: variable name
 *   4: if exists, the variable is an array
 */
var regExp = /\b(attribute|uniform)\s+(float|int|bool|vec2|vec3|vec4|mat2|mat3|mat4|sampler2D|sampler3D|samplerCube)\s+(\w+)(\s*\[\s*\w+\s*\])*;/g;

Shader.prototype.compileProgram = function (renderer) {
	if (this.shaderProgram === null) {
		this._investigateShaders();
		this.addDefines(this.defines);
		this.addPrecision(this.overridePrecision || renderer.shaderPrecision);
		this.compile(renderer);
	}
};

Shader.prototype.activateProgram = function (record, context) {
	if (record.usedProgram !== this.shaderProgram) {
		context.useProgram(this.shaderProgram);
		record.usedProgram = this.shaderProgram;
		return true;
	}
};

Shader.prototype.bindAttributeKey = function (record, renderer, attributeMap, key) {
	var attribute = attributeMap[this.attributes[key]];
	if (!attribute) {
		return;
	}

	var attributeIndex = this.attributeIndexMapping[key];
	if (attributeIndex === undefined) {
		// console.warn('Attribute binding [' + name + '] does not exist in the shader.');
		return;
	}

	record.newlyEnabledAttributes[attributeIndex] = true;
	renderer.bindVertexAttribute(attributeIndex, attribute);
};

Shader.prototype.bindAttributes = function (record, renderer, attributeMap) {
	if (this.attributes) {
		for (var i = 0, l = this.attributeKeys.length; i < l; i++) {
			this.bindAttributeKey(record, renderer, attributeMap, this.attributeKeys[i]);
		}
	}
};

Shader.prototype.disableAttributes = function (record, context) {
	for (var i = 0, l = record.enabledAttributes.length; i < l; i++) {
		var enabled = record.enabledAttributes[i];
		var newEnabled = record.newlyEnabledAttributes[i];
		if (!newEnabled && enabled) {
			context.disableVertexAttribArray(i);
			record.enabledAttributes[i] = false;
		}
	}
};

Shader.prototype.enableAttributes = function (record, context) {
	for (var i = 0, l = record.newlyEnabledAttributes.length; i < l; i++) {
		var enabled = record.enabledAttributes[i];
		var newEnabled = record.newlyEnabledAttributes[i];
		if (newEnabled && !enabled) {
			context.enableVertexAttribArray(i);
			record.enabledAttributes[i] = true;
		}
	}
};

Shader.prototype.matchUniforms = function (shaderInfo) {
	var uniforms = this.matchedUniforms;
	if (uniforms) {
		this.textureIndex = 0;

		for (var i = 0, l = uniforms.length; i < l; i++) {
			this._bindUniform(uniforms[i], shaderInfo);
		}
	}
};

Shader.prototype.apply = function (shaderInfo, renderer) {
	var context = renderer.context;
	var record = renderer.rendererRecord;

	this.compileProgram(renderer);
	// Set the ShaderProgram active
	this.activateProgram(record, context);

	record.newlyEnabledAttributes.length = 0;

	// Bind attributes
	this.bindAttributes(record, renderer, shaderInfo.meshData.attributeMap);

	this.disableAttributes(record, context);
	this.enableAttributes(record, context);
	this.matchUniforms(shaderInfo);
};

Shader.prototype.defineValue = function (shaderInfo, name) {
	var defValue = shaderInfo.material.uniforms[name];
	if (defValue === undefined) {
		defValue = this.uniforms[name];
	}
	return defValue;
};

Shader.prototype.mapSlot = function (shaderInfo, mapping, slot) {
	var maps = shaderInfo.material.getTexture(slot.mapping);
	if (maps instanceof Array) {
		this.arrayType(mapping, slot, maps);
	} else {
		slot.index = this.textureIndex;
		mapping.call(this.textureIndex++);
	}
};

Shader.prototype.arrayType = function (mapping, slot, maps) {
	var arr = [];
	slot.index = [];
	for (var i = 0; i < maps.length; i++) {
		slot.index.push(this.textureIndex);
		arr.push(this.textureIndex++);
	}
	mapping.call(arr);
};

Shader.prototype.stringType = function (shaderInfo, name, mapping) {
	var callback = this.currentCallbacks[name];
	if (callback) {
		callback(mapping, shaderInfo);
	} else {
		var slot = this.textureSlotsNaming[name];
		if (slot !== undefined) {
			this.mapSlot(shaderInfo, mapping, slot);
		}
	}
};

Shader.prototype.callMapping = function (shaderInfo, name, mapping) {
	var defValue = this.defineValue(shaderInfo, name);
	var type = typeof defValue;
	if (type === 'string') {
		this.stringType(shaderInfo, name, mapping, defValue);
	} else {
		var value = type === 'function' ? defValue(shaderInfo) : defValue;
		if (value !== undefined) {
			mapping.call(value);
		}
	}
};

Shader.prototype._bindUniform = function (name, shaderInfo) {
	var mapping = this.uniformCallMapping[name];
	if (mapping === undefined) {
		return;
	}
	this.callMapping(shaderInfo, name, mapping);
};

Shader.prototype.setDefine = function (key, value) {
	this.defineKeyDirty = this.defineKeyDirty || this.defines[key] !== value;
	this.defines[key] = value;
};

Shader.prototype.removeDefine = function (key) {
	this.defineKeyDirty = this.defineKeyDirty || this.defines[key] !== undefined;
	this.defines[key] = undefined;
};

Shader.prototype.hasDefine = function (key) {
	return this.defines[key] !== false && this.defines[key] !== undefined;
};

Shader.prototype.startFrame = function () {
	this.frameStart = true;
};

Shader.prototype.endFrame = function () {
	this.frameStart = false;
};

Shader.prototype.updateProcessors = function (renderInfo) {
	if (this.processors) {
		for (var j = 0; j < this.processors.length; j++) {
			this.processors[j](this, renderInfo);
		}
	}
};

Shader.prototype.getDefineKey = function (definesIndices) {
	if (this.defineKeyDirty) {
		var key = 'Key:' + this.name;
		var defineArray = Object.keys(this.defines);
		for (var i = 0; i < defineArray.length; i++) {
			var defineArrayKey = defineArray[i];
			var defineVal = this.defines[defineArrayKey];
			if (defineVal === undefined || defineVal === false) {
				continue;
			}
			if (definesIndices.indexOf(defineArrayKey) === -1) {
				definesIndices.push(defineArrayKey);
			}
		}
		for (var i = 0, l = definesIndices.length; i < l; i++) {
			var defineArrayKey = definesIndices[i];
			var defineVal = this.defines[defineArrayKey];
			if (defineVal === undefined || defineVal === false) {
				continue;
			}
			key += '_' + i + ':' + defineVal;
		}
		this.defineKey = key;
		this.defineKeyDirty = false;
	}

	return this.defineKey;
};

Shader.prototype.rebuild = function () {
	this.shaderProgram = null;
	this.attributeMapping = {};
	this.attributeIndexMapping = {};
	this.uniformMapping = {};
	this.uniformCallMapping = {};
	this.currentCallbacks = {};
	this.attributeKeys = null;
	this.vertexSource = typeof this.origVertexSource === 'function' ? this.origVertexSource() : this.origVertexSource;
	this.fragmentSource = typeof this.origFragmentSource === 'function' ? this.origFragmentSource() : this.origFragmentSource;
	this.defineKeyDirty = true;
};

Shader.prototype._investigateShaders = function () {
	this.textureSlots = [];
	this.textureSlotsNaming = {};
	Shader.investigateShader(this.vertexSource, this);
	Shader.investigateShader(this.fragmentSource, this);
};

/**
 * Extract shader variable definitions from shader source code.
 * @param {string} source The source code.
 * @param {Object} target
 * @param {Object} target.attributeMapping
 * @param {Object} target.uniformMapping
 * @param {Array} target.textureSlots
 */
Shader.investigateShader = function (source, target) {
	regExp.lastIndex = 0;
	var matcher = regExp.exec(source);

	while (matcher !== null) {
		var definition = {
			// data type: float, int, ...
			format: matcher[2]
		};
		var type = matcher[1];  // "attribute" or "uniform"
		var variableName = matcher[3];
		var arrayDeclaration = matcher[4];
		if (arrayDeclaration) {
			if (definition.format === 'float') {
				definition.format = 'floatarray';
			} else if (definition.format === 'int') {
				definition.format = 'intarray';
			} else if (definition.format.indexOf('sampler') === 0) {
				definition.format = 'samplerArray';
			}
		}

		if (type === 'attribute') {
			target.attributeMapping[variableName] = definition;
		} else {
			if (definition.format.indexOf('sampler') === 0) {
				var textureSlot = {
					format: definition.format,
					name: variableName,
					mapping: target.uniforms[variableName],
					index: target.textureSlots.length
				};
				target.textureSlots.push(textureSlot);
				target.textureSlotsNaming[textureSlot.name] = textureSlot;
			}
			target.uniformMapping[variableName] = definition;
		}

		matcher = regExp.exec(source);
	}
};

Shader.prototype.compile = function (renderer) {
	var context = renderer.context;
	this.renderer = renderer;
	this.vertexShader = this._getShader(context, context.VERTEX_SHADER !== undefined ? context.VERTEX_SHADER : WebGLRenderingContext.VERTEX_SHADER, this.vertexSource);
	this.fragmentShader = this._getShader(context, context.FRAGMENT_SHADER !== undefined ? context.FRAGMENT_SHADER : WebGLRenderingContext.FRAGMENT_SHADER, this.fragmentSource);

	if (this.vertexShader === null || this.fragmentShader === null) {
		console.error('Shader error - no shaders');
	}

	this.shaderProgram = context.createProgram();

	var error = context.getError();
	if (this.shaderProgram === null || error !== 0) {
		console.error('Shader error: ' + error + ' [shader: ' + this.name + ']');
		SystemBus.emit('goo.shader.error');
	}

	context.attachShader(this.shaderProgram, this.vertexShader);
	context.attachShader(this.shaderProgram, this.fragmentShader);

	// Link the Shader Program
	context.linkProgram(this.shaderProgram);
	if (!context.getProgramParameter(this.shaderProgram, (context.LINK_STATUS !== undefined ? context.LINK_STATUS : WebGLRenderingContext.LINK_STATUS))) {
		var errInfo = context.getProgramInfoLog(this.shaderProgram);
		console.error('Could not initialise shaders: ' + errInfo);
		SystemBus.emit('goo.shader.error', errInfo);
	}

	for (var key in this.attributeMapping) {
		var attributeIndex = context.getAttribLocation(this.shaderProgram, key);
		if (attributeIndex === -1) {
			continue;
		}

		this.attributeIndexMapping[key] = attributeIndex;
	}

	for (var key in this.uniformMapping) {
		var uniform = context.getUniformLocation(this.shaderProgram, key);

		if (!uniform) {
			var l = this.textureSlots.length;
			for (var i = 0; i < l; i++) {
				var slot = this.textureSlots[i];
				if (slot.name === key) {
					this.textureSlots.splice(i, 1);
					delete this.textureSlotsNaming[slot.name];
					for (; i < l - 1; i++) {
						this.textureSlots[i].index--;
					}
					break;
				}
			}
			continue;
		}

		this.uniformCallMapping[key] = new ShaderCall(context, uniform, this.uniformMapping[key].format);
	}

	if (this.attributes) {
		this.attributeKeys = Object.keys(this.attributes);
	}

	if (this.uniforms) {
		this.matchedUniforms = [];
		for (var name in this.uniforms) {
			var mapping = this.uniformCallMapping[name];
			if (mapping !== undefined) {
				this.matchedUniforms.push(name);
			}

			var value = this.uniforms[name];
			if (this.defaultCallbacks[value]) {
				this.currentCallbacks[name] = this.defaultCallbacks[value];
			}
		}
	}
};

var errorRegExp = /\b\d+:(\d+):\s(.+)\b/g;
var errorRegExpIE = /\((\d+),\s*\d+\):\s(.+)/g;

Shader.prototype._getShader = function (context, type, source) {
	var shader = context.createShader(type);

	context.shaderSource(shader, source);
	context.compileShader(shader);

	// check if the Shader is successfully compiled
	if (!context.getShaderParameter(shader, context.COMPILE_STATUS !== undefined ? context.COMPILE_STATUS : WebGLRenderingContext.COMPILE_STATUS)) {
		var infoLog = context.getShaderInfoLog(shader);
		var shaderType = type === (context.VERTEX_SHADER !== undefined ? context.VERTEX_SHADER : WebGLRenderingContext.VERTEX_SHADER) ? 'VertexShader' : 'FragmentShader';

		errorRegExp.lastIndex = 0;
		var errorMatcher = errorRegExp.exec(infoLog);
		if (errorMatcher === null) {
			errorMatcher = errorRegExpIE.exec(infoLog);
		}
		if (errorMatcher !== null) {
			while (errorMatcher !== null) {
				var splitSource = source.split('\n');
				var lineNum = errorMatcher[1];
				var errorStr = errorMatcher[2];
				console.error('Error in ' + shaderType + ' - [' + this.name + '][' + this._id + '] at line ' + lineNum + ':');
				console.error('\tError: ' + errorStr);
				console.error('\tSource: ' + splitSource[lineNum - 1]);
				errorMatcher = errorRegExp.exec(infoLog);
			}
		} else {
			console.error('Error in ' + shaderType + ' - [' + this.name + '][' + this._id + '] ' + infoLog);
		}

		return null;
	}

	return shader;
};

var precisionRegExp = /\bprecision\s+(lowp|mediump|highp)\s+(float|int);/g;

Shader.prototype.addPrecision = function (precision) {
	precisionRegExp.lastIndex = 0;
	var vertMatcher = precisionRegExp.exec(this.vertexSource);
	if (vertMatcher === null) {
		this.vertexSource = 'precision ' + precision + ' float;' + '\n' + this.vertexSource;
	}
	precisionRegExp.lastIndex = 0;
	var fragMatcher = precisionRegExp.exec(this.fragmentSource);
	if (fragMatcher === null) {
		this.fragmentSource = 'precision ' + precision + ' float;' + '\n' + this.fragmentSource;
	}
};

Shader.prototype.addDefines = function (defines) {
	if (!defines) {
		return;
	}

	var defineStr = this.generateDefines(defines);

	this.vertexSource = defineStr + '\n' + this.vertexSource;
	this.fragmentSource = defineStr + '\n' + this.fragmentSource;
};

Shader.prototype.generateDefines = function (defines) {
	var chunks = [];
	for (var d in defines) {
		var value = defines[d];
		if (value === false || value === undefined) {
			continue;
		}

		var chunk = '#define ' + d + ' ' + value;
		chunks.push(chunk);
	}

	return chunks.join('\n');
};

function setupDefaultCallbacks(defaultCallbacks) {
	defaultCallbacks[Shader.PROJECTION_MATRIX] = function (uniformCall, shaderInfo) {
		var matrix = shaderInfo.camera.getProjectionMatrix();
		uniformCall.uniformMatrix4fv(matrix);
	};
	defaultCallbacks[Shader.VIEW_MATRIX] = function (uniformCall, shaderInfo) {
		var matrix = shaderInfo.camera.getViewMatrix();
		uniformCall.uniformMatrix4fv(matrix);
	};
	defaultCallbacks[Shader.WORLD_MATRIX] = function (uniformCall, shaderInfo) {
		//! AT: when is this condition ever true?
		var matrix = shaderInfo.transform !== undefined ? shaderInfo.transform.matrix : Matrix4.IDENTITY;
		uniformCall.uniformMatrix4fv(matrix);
	};
	defaultCallbacks[Shader.NORMAL_MATRIX] = function (uniformCall, shaderInfo) {
		//! AT: when is this condition ever true?
		var matrix = shaderInfo.transform !== undefined ? shaderInfo.transform.normalMatrix : Matrix3.IDENTITY;
		uniformCall.uniformMatrix3fv(matrix);
	};

	defaultCallbacks[Shader.VIEW_INVERSE_MATRIX] = function (uniformCall, shaderInfo) {
		var matrix = shaderInfo.camera.getViewInverseMatrix();
		uniformCall.uniformMatrix4fv(matrix);
	};
	defaultCallbacks[Shader.VIEW_PROJECTION_MATRIX] = function (uniformCall, shaderInfo) {
		var matrix = shaderInfo.camera.getViewProjectionMatrix();
		uniformCall.uniformMatrix4fv(matrix);
	};
	defaultCallbacks[Shader.VIEW_PROJECTION_INVERSE_MATRIX] = function (uniformCall, shaderInfo) {
		var matrix = shaderInfo.camera.getViewProjectionInverseMatrix();
		uniformCall.uniformMatrix4fv(matrix);
	};

	for (var i = 0; i < 16; i++) {
		defaultCallbacks[Shader['TEXTURE' + i]] = (function (i) {
			return function (uniformCall) {
				uniformCall.uniform1i(i);
			};
		})(i);
	}

	for (var i = 0; i < 8; i++) {
		defaultCallbacks[Shader['LIGHT' + i]] = (function (i) {
			return function (uniformCall, shaderInfo) {
				var light = shaderInfo.lights[i];
				if (light !== undefined) {
					uniformCall.uniform3f(light.translation.x, light.translation.y, light.translation.z);
				} else {
					uniformCall.uniform3f(-20, 20, 20);
				}
			};
		})(i);
	}
	defaultCallbacks[Shader.LIGHTCOUNT] = function (uniformCall, shaderInfo) {
		uniformCall.uniform1i(shaderInfo.lights.length);
	};

	defaultCallbacks[Shader.CAMERA] = function (uniformCall, shaderInfo) {
		var cameraPosition = shaderInfo.camera.translation;
		uniformCall.uniform3f(cameraPosition.x, cameraPosition.y, cameraPosition.z);
	};
	defaultCallbacks[Shader.NEAR_PLANE] = function (uniformCall, shaderInfo) {
		uniformCall.uniform1f(shaderInfo.camera.near);
	};
	defaultCallbacks[Shader.FAR_PLANE] = function (uniformCall, shaderInfo) {
		uniformCall.uniform1f(shaderInfo.camera.far);
	};
	defaultCallbacks[Shader.MAIN_NEAR_PLANE] = function (uniformCall, shaderInfo) {
		uniformCall.uniform1f(shaderInfo.mainCamera.near);
	};
	defaultCallbacks[Shader.MAIN_FAR_PLANE] = function (uniformCall, shaderInfo) {
		uniformCall.uniform1f(shaderInfo.mainCamera.far);
	};
	defaultCallbacks[Shader.MAIN_DEPTH_SCALE] = function (uniformCall, shaderInfo) {
		uniformCall.uniform1f(1.0 / (shaderInfo.mainCamera.far - shaderInfo.mainCamera.near));
	};

	defaultCallbacks[Shader.AMBIENT] = function (uniformCall, shaderInfo) {
		var materialState = shaderInfo.material.materialState !== undefined ? shaderInfo.material.materialState.ambient : Shader.DEFAULT_AMBIENT;
		uniformCall.uniform4fv(materialState);
	};
	defaultCallbacks[Shader.EMISSIVE] = function (uniformCall, shaderInfo) {
		var materialState = shaderInfo.material.materialState !== undefined ? shaderInfo.material.materialState.emissive : Shader.DEFAULT_EMISSIVE;
		uniformCall.uniform4fv(materialState);
	};
	defaultCallbacks[Shader.DIFFUSE] = function (uniformCall, shaderInfo) {
		var materialState = shaderInfo.material.materialState !== undefined ? shaderInfo.material.materialState.diffuse : Shader.DEFAULT_DIFFUSE;
		uniformCall.uniform4fv(materialState);
	};
	defaultCallbacks[Shader.SPECULAR] = function (uniformCall, shaderInfo) {
		var materialState = Shader.DEFAULT_SPECULAR;
		if (shaderInfo.material.materialState !== undefined) {
			materialState = shaderInfo.material.materialState.specular;
			materialState[3] = Math.max(shaderInfo.material.materialState.shininess, 1);
		}
		uniformCall.uniform4fv(materialState);
	};
	defaultCallbacks[Shader.SPECULAR_POWER] = function (uniformCall, shaderInfo) {
		var shininess = shaderInfo.material.materialState !== undefined ? shaderInfo.material.materialState.shininess : Shader.DEFAULT_SHININESS;
		shininess = Math.max(shininess, 1.0);
		uniformCall.uniform1f(shininess);
	};

	defaultCallbacks[Shader.TIME] = function (uniformCall) {
		uniformCall.uniform1f(World.time);
	};
	defaultCallbacks[Shader.TPF] = function (uniformCall) {
		uniformCall.uniform1f(World.tpf);
	};

	defaultCallbacks[Shader.RESOLUTION] = function (uniformCall, shaderInfo) {
		uniformCall.uniform2f(shaderInfo.renderer.viewportWidth, shaderInfo.renderer.viewportHeight);
	};
}

Shader.prototype.getShaderDefinition = function () {
	return {
		vshader: this.vertexSource,
		fshader: this.fragmentSource,
		defines: this.defines,
		attributes: this.attributes,
		uniforms: this.uniforms
	};
};

Shader.prototype.destroy = function () {
	if (this.shaderProgram) {
		this.renderer.context.deleteProgram(this.shaderProgram);
		this.shaderProgram = null;
	}
	if (this.vertexShader) {
		this.renderer.context.deleteShader(this.vertexShader);
		this.vertexShader = null;
	}
	if (this.fragmentShader) {
		this.renderer.context.deleteShader(this.fragmentShader);
		this.fragmentShader = null;
	}
	this.renderer = null;
};

Shader.prototype.toString = function () {
	return this.name;
};

Shader.PROJECTION_MATRIX = 'PROJECTION_MATRIX';
Shader.VIEW_MATRIX = 'VIEW_MATRIX';
Shader.VIEW_INVERSE_MATRIX = 'VIEW_INVERSE_MATRIX';
Shader.VIEW_PROJECTION_MATRIX = 'VIEW_PROJECTION_MATRIX';
Shader.VIEW_PROJECTION_INVERSE_MATRIX = 'VIEW_PROJECTION_INVERSE_MATRIX';
Shader.WORLD_MATRIX = 'WORLD_MATRIX';
Shader.NORMAL_MATRIX = 'NORMAL_MATRIX';
for (var i = 0; i < 8; i++) {
	Shader['LIGHT' + i] = 'LIGHT' + i;
}
Shader.CAMERA = 'CAMERA';
Shader.AMBIENT = 'AMBIENT';
Shader.EMISSIVE = 'EMISSIVE';
Shader.DIFFUSE = 'DIFFUSE';
Shader.SPECULAR = 'SPECULAR';
Shader.SPECULAR_POWER = 'SPECULAR_POWER';
Shader.NEAR_PLANE = 'NEAR_PLANE';
Shader.FAR_PLANE = 'FAR_PLANE';
Shader.MAIN_NEAR_PLANE = 'MAIN_NEAR_PLANE';
Shader.MAIN_FAR_PLANE = 'MAIN_FAR_PLANE';
Shader.MAIN_DEPTH_SCALE = 'DEPTH_SCALE';
Shader.TIME = 'TIME';
Shader.TPF = 'TPF';
Shader.RESOLUTION = 'RESOLUTION';

Shader.DIFFUSE_MAP = 'DIFFUSE_MAP';
Shader.NORMAL_MAP = 'NORMAL_MAP';
Shader.SPECULAR_MAP = 'SPECULAR_MAP';
Shader.LIGHT_MAP = 'LIGHT_MAP';
Shader.SHADOW_MAP = 'SHADOW_MAP';
Shader.AO_MAP = 'AO_MAP';
Shader.EMISSIVE_MAP = 'EMISSIVE_MAP';
Shader.DEPTH_MAP = 'DEPTH_MAP';

Shader.DEFAULT_AMBIENT = [0.1, 0.1, 0.1, 1.0];
Shader.DEFAULT_EMISSIVE = [0, 0, 0, 0];
Shader.DEFAULT_DIFFUSE = [0.8, 0.8, 0.8, 1.0];
Shader.DEFAULT_SPECULAR = [0.6, 0.6, 0.6, 64.0];
Shader.DEFAULT_SHININESS = 64.0;

Shader.prototype.defaultCallbacks = {};
setupDefaultCallbacks(Shader.prototype.defaultCallbacks);

module.exports = Shader;


text:
package io.bootique.junit5.handler.app;

import io.bootique.junit5.BQApp;
import io.bootique.junit5.handler.HandlerUtil;
import org.junit.jupiter.api.extension.*;

/**
 * Starts runtimes annotated with {@link BQApp}, shuts them down at the end of their declared or implied scope.
 *
 * @since 2.0
 */
public class BQAppHandler implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback, AfterAllCallback {

    private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(BQAppHandler.class);
    private static final String GLOBAL_REGISTRY = "globalRegistry";
    private static final String CLASS_REGISTRY = "classRegistry";
    private static final String METHOD_REGISTRY = "methodRegistry";

    @Override
    public void beforeAll(ExtensionContext context) {
        getOrCreateGlobalAppRegistry(context).beforeContext(context);
        getOrCreateClassAppRegistry(context).beforeContext(context);
    }

    @Override
    public void beforeEach(ExtensionContext context) {
        getOrCreateMethodAppRegistry(context).beforeContext(context);
    }

    @Override
    public void afterEach(ExtensionContext context) {
        getOrCreateMethodAppRegistry(context).afterContext();
    }

    @Override
    public void afterAll(ExtensionContext context) {
        // Only invoking per-class callbacks. Don't bother with GLOBAL callbacks. GLOBALs are shutdown elsewhere by
        // implementing ExtensionContext.Store.CloseableResource
        getOrCreateClassAppRegistry(context).afterContext();
    }

    protected BQAppRegistry getOrCreateGlobalAppRegistry(ExtensionContext context) {
        return (BQAppRegistry) context.getRoot()
                .getStore(NAMESPACE)
                .getOrComputeIfAbsent(GLOBAL_REGISTRY, s -> new GlobalAppRegistry());
    }

    protected BQAppRegistry getOrCreateClassAppRegistry(ExtensionContext context) {
        return (BQAppRegistry) HandlerUtil.getClassContext(context)
                .getStore(NAMESPACE)
                .getOrComputeIfAbsent(CLASS_REGISTRY, s -> new ClassAppRegistry());
    }

    protected BQAppRegistry getOrCreateMethodAppRegistry(ExtensionContext context) {
        return (BQAppRegistry) context
                .getStore(NAMESPACE)
                .getOrComputeIfAbsent(METHOD_REGISTRY, s -> new MethodAppRegistry());
    }
}

redpajama_set_name: RedPajamaGithub


text:
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>ajia</groupId>
		<artifactId>common-aspectj</artifactId>
		<version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>Section7.4.1UtilizingTypedJoinPointContextUsingAnonymusPointcut-noship</artifactId>
    <packaging>jar</packaging>
    <name>Section7.4.1UtilizingTypedJoinPointContextUsingAnonymusPointcut-noship</name>
</project>


text:
package org.kiji.schema.shell.input

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader

import org.kiji.annotations.ApiAudience

/**
 * Reads user input from an InputStream full of commands.
 *
 *
The close() method of this InputSource will close the underlying stream.

 */
@ApiAudience.Private
final class StreamInputSource(stream: InputStream) extends InputSource {
  private val bufferedStream = new BufferedReader(new InputStreamReader(stream, "UTF-8"))

  /** Read a line of input from the underlying source and return it. */
  def readLine(prompt: String): Option[String] = {
    return Option(bufferedStream.readLine())
  }

  def close(): Unit = {
    bufferedStream.close()
  }
}


text:
Infrastructure Errors
=====================

Since the infrastructure is the highest level of exception catchers these errors aren't placed in the commons, as no one else is expected to catch them.

Although they are actually exceptions, they are called errors to avoid conflicts with python's built-in exceptions.

ConfigurationError
------------------

A ConfigurationError is raised if there is an error in the configuration file

OperatorError
-------------

An OperatorError is raised by an Operator if the operation has failed.


text:
namespace AsciiUml.Commands {
	public interface ICommand {
		State Execute(State state);
	}
}


text:
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        dummy = ListNode(0)
        dummy.next = head
        first, second = dummy, dummy
        for i in xrange(n):
            first = first.next
        while first.next:
            first = first.next
            second = second.next
        second.next = second.next.next
        return dummy.next


text:
/**
 * Shared code for providers that use LLDP.
 */
package org.onosproject.provider.lldpcommon;


text:
// ========================================================================
// Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================

package org.eclipse.jetty.server;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.Exchanger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import junit.framework.Assert;

import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StdErrLog;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;

import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;

/**
 *
 */
public abstract class HttpServerTestBase extends HttpServerTestFixture
{
    /** The request. */
    private static final String REQUEST1_HEADER="POST / HTTP/1.0\n"+"Host: localhost\n"+"Content-Type: text/xml; charset=utf-8\n"+"Connection: close\n"+"Content-Length: ";
    private static final String REQUEST1_CONTENT="<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
            +"<nimbus xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+"        xsi:noNamespaceSchemaLocation=\"nimbus.xsd\" version=\"1.0\">\n"
            +"</nimbus>";
    private static final String REQUEST1=REQUEST1_HEADER+REQUEST1_CONTENT.getBytes().length+"\n\n"+REQUEST1_CONTENT;

    /** The expected response. */
    private static final String RESPONSE1="HTTP/1.1 200 OK\n"+"Content-Length: 13\n"+"Server: Jetty("+Server.getVersion()+")\n"+"\n"+"Hello world\n";

    // Break the request up into three pieces, splitting the header.
    private static final String FRAGMENT1=REQUEST1.substring(0,16);
    private static final String FRAGMENT2=REQUEST1.substring(16,34);
    private static final String FRAGMENT3=REQUEST1.substring(34);

    /** Second test request. */
    protected static final String REQUEST2_HEADER=
        "POST / HTTP/1.0\n"+
        "Host: localhost\n"+
        "Content-Type: text/xml;charset=ISO-8859-1\n"+
        "Content-Length: ";
    protected static final String REQUEST2_CONTENT=
        "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+
        "<nimbus xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+
        "        xsi:noNamespaceSchemaLocation=\"nimbus.xsd\" version=\"1.0\">\n"+
        "    <request requestId=\"1\">\n"+
        "        <getJobDetails>\n"+
        "            <jobId>73</jobId>\n"+
        "        </getJobDetails>\n"+
        "    </request>\n"+
        "</nimbus>";
    protected static final String REQUEST2=REQUEST2_HEADER+REQUEST2_CONTENT.getBytes().length+"\n\n"+REQUEST2_CONTENT;

    /** The second expected response. */
    protected static final String RESPONSE2_CONTENT=
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+
            "<nimbus xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"+
            "        xsi:noNamespaceSchemaLocation=\"nimbus.xsd\" version=\"1.0\">\n"+
            "    <request requestId=\"1\">\n"+
            "        <getJobDetails>\n"+
            "            <jobId>73</jobId>\n"+
            "        </getJobDetails>\n"+
            "    </request>\n"
            +"</nimbus>\n";
    protected static final String RESPONSE2=
        "HTTP/1.1 200 OK\n"+
        "Content-Type: text/xml;charset=ISO-8859-1\n"+
        "Content-Length: "+RESPONSE2_CONTENT.getBytes().length+"\n"+
        "Server: Jetty("+Server.getVersion()+")\n"+
        "\n"+
        RESPONSE2_CONTENT;

    /*
     * Feed the server the entire request at once.
     */
    @Test
    public void testRequest1() throws Exception
    {
        configureServer(new HelloWorldHandler());

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();

            os.write(REQUEST1.getBytes());
            os.flush();

            // Read the response.
            String response=readResponse(client);

            // Check the response
            assertEquals("response",RESPONSE1,response);
        }
        finally
        {
            client.close();
        }
    }

    @Test
    public void testFragmentedChunk() throws Exception
    {
        configureServer(new EchoHandler());

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();

            os.write(("GET /R2 HTTP/1.1\015\012"+
                    "Host: localhost\015\012"+
                    "Transfer-Encoding: chunked\015\012"+
                    "Content-Type: text/plain\015\012"+
                    "Connection: close\015\012"+
                    "\015\012").getBytes());
            os.flush();
            Thread.sleep(PAUSE);
            os.write(("5\015\012").getBytes());
            os.flush();
            Thread.sleep(PAUSE);
            os.write(("ABCDE\015\012"+
                      "0;\015\012\015\012").getBytes());
            os.flush();

            // Read the response.
            String response=readResponse(client);
            assertTrue (response.indexOf("200")>0);
        }
        finally
        {
            client.close();
        }
    }

    /*
     * Feed the server fragmentary headers and see how it copes with it.
     */
    @Test
    public void testRequest1Fragments() throws Exception, InterruptedException
    {
        configureServer(new HelloWorldHandler());

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();

            // Write a fragment, flush, sleep, write the next fragment, etc.
            os.write(FRAGMENT1.getBytes());
            os.flush();
            Thread.sleep(PAUSE);
            os.write(FRAGMENT2.getBytes());
            os.flush();
            Thread.sleep(PAUSE);
            os.write(FRAGMENT3.getBytes());
            os.flush();

            // Read the response
            String response = readResponse(client);

            // Check the response
            assertEquals("response",RESPONSE1,response);
        }
        finally
        {
            client.close();
        }

    }

    @Test
    public void testRequest2() throws Exception
    {
        configureServer(new EchoHandler());

        byte[] bytes=REQUEST2.getBytes();
        for (int i=0; i<LOOPS; i++)
        {
            Socket client=newSocket(HOST,_connector.getLocalPort());
            try
            {
                OutputStream os=client.getOutputStream();

                os.write(bytes);
                os.flush();

                // Read the response
                String response=readResponse(client);

                // Check the response
                assertEquals("response "+i,RESPONSE2,response);
            }
            catch(IOException e)
            {
                e.printStackTrace();
                _server.dumpStdErr();
                throw e;
            }
            finally
            {
                client.close();
            }
        }
    }

    @Test
    public void testRequest2Fragments() throws Exception
    {
        configureServer(new EchoHandler());

        byte[] bytes=REQUEST2.getBytes();
        final int pointCount=2;
        Random random=new Random(System.currentTimeMillis());
        for (int i=0; i<LOOPS; i++)
        {
            int[] points=new int[pointCount];
            StringBuilder message=new StringBuilder();

            message.append("iteration #").append(i + 1);

            // Pick fragment points at random
            for (int j=0; j<points.length; ++j)
            {
                points[j]=random.nextInt(bytes.length);
            }

            // Sort the list
            Arrays.sort(points);

            Socket client=newSocket(HOST,_connector.getLocalPort());
            try
            {
                OutputStream os=client.getOutputStream();

                writeFragments(bytes,points,message,os);

                // Read the response
                String response=readResponse(client);

                // Check the response
                assertEquals("response for "+i+" "+message.toString(),RESPONSE2,response);
            }
            finally
            {
                client.close();
            }
        }
    }

    @Test
    public void testRequest2Iterate() throws Exception
    {
        configureServer(new EchoHandler());

        byte[] bytes=REQUEST2.getBytes();
        for (int i=0; i<bytes.length; i+=3)
        {
            int[] points=new int[] { i };
            StringBuilder message=new StringBuilder();

            message.append("iteration #").append(i + 1);

            // Sort the list
            Arrays.sort(points);

            Socket client=newSocket(HOST,_connector.getLocalPort());
            try
            {
                OutputStream os=client.getOutputStream();

                writeFragments(bytes,points,message,os);

                // Read the response
                String response=readResponse(client);

                // Check the response
                assertEquals("response for "+i+" "+message.toString(),RESPONSE2,response);
            }
            finally
            {
                client.close();
            }
        }
    }

    /*
     * After several iterations, I generated some known bad fragment points.
     */
    @Test
    public void testRequest2KnownBad() throws Exception
    {
        configureServer(new EchoHandler());

        byte[] bytes=REQUEST2.getBytes();
        int[][] badPoints=new int[][]
        {
                { 70 }, // beginning here, drops last line of request
                { 71 }, // no response at all
                { 72 }, // again starts drops last line of request
                { 74 }, // again, no response at all
        };
        for (int i=0; i<badPoints.length; ++i)
        {
            Socket client=newSocket(HOST,_connector.getLocalPort());
            try
            {
                OutputStream os=client.getOutputStream();
                StringBuilder message=new StringBuilder();

                message.append("iteration #").append(i + 1);
                writeFragments(bytes,badPoints[i],message,os);

                // Read the response
                String response=readResponse(client);

                // Check the response
                // TODO - change to equals when code gets fixed
                assertNotSame("response for "+message.toString(),RESPONSE2,response);
            }
            finally
            {
                client.close();
            }
        }
    }

    @Test
    public void testFlush() throws Exception
    {
        configureServer(new DataHandler());

        String[] encoding = {"NONE","UTF-8","ISO-8859-1","ISO-8859-2"};
        for (int e =0; e<encoding.length;e++)
        {
            for (int b=1;b<=128;b=b==1?2:b==2?32:b==32?128:129)
            {
                for (int w=41;w<42;w+=4096)
                {
                    for (int c=0;c<1;c++)
                    {
                        String test=encoding[e]+"x"+b+"x"+w+"x"+c;
                        try
                        {
                            URL url=new URL(_scheme+"://"+HOST+":"+_connector.getLocalPort()+"/?writes="+w+"&block="+b+ (e==0?"":("&encoding="+encoding[e]))+(c==0?"&chars=true":""));

                            InputStream in = (InputStream)url.getContent();
                            String response=IO.toString(in,e==0?null:encoding[e]);

                            assertEquals(test,b*w,response.length());
                        }
                        catch(Exception x)
                        {
                            System.err.println(test);
                            x.printStackTrace();
                            throw x;
                        }
                    }
                }
            }
        }
    }

    @Test
    public void testBlockingWhileReadingRequestContent() throws Exception
    {
        configureServer(new DataHandler());

        long start=System.currentTimeMillis();
        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            os.write((
                    "GET /data?writes=1024&block=256 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "connection: close\r\n"+
                    "content-type: unknown\r\n"+
                    "content-length: 30\r\n"+
                    "\r\n"
            ).getBytes());
            os.flush();
            Thread.sleep(200);
            os.write((
                    "\r\n23456890"
            ).getBytes());
            os.flush();
            Thread.sleep(1000);
            os.write((
                    "abcdefghij"
            ).getBytes());
            os.flush();
            Thread.sleep(1000);
            os.write((
                    "0987654321\r\n"
            ).getBytes());
            os.flush();

            int total=0;
            int len=0;
            byte[] buf=new byte[1024*64];

            while(len>=0)
            {
                Thread.sleep(100);
                len=is.read(buf);
                if (len>0)
                    total+=len;
            }

            assertTrue(total>(1024*256));
            assertTrue(30000L>(System.currentTimeMillis()-start));
        }
        finally
        {
            client.close();
        }
    }

    @Test
    public void testBlockingWhileWritingResponseContent() throws Exception
    {
        configureServer(new DataHandler());

        long start=System.currentTimeMillis();
        Socket client=newSocket(HOST,_connector.getLocalPort());
        int total=0;
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            os.write((
                    "GET /data?writes=512&block=1024 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "connection: close\r\n"+
                    "content-type: unknown\r\n"+
                    "\r\n"
            ).getBytes());
            os.flush();

            int len=0;
            byte[] buf=new byte[1024*32];

            int i=0;
            while(len>=0)
            {
                if (i++%10==0)
                    Thread.sleep(1000);
                len=is.read(buf);
                if (len>0)
                    total+=len;
            }

            assertTrue(total>(512*1024));
            assertTrue(30000L>(System.currentTimeMillis()-start));
        }
        finally
        {
            //System.err.println("Got "+total+" of "+(512*1024));
            client.close();
        }
    }

    @Test
    public void testBigBlocks() throws Exception
    {
        configureServer(new BigBlockHandler());

        Socket client=newSocket(HOST,_connector.getLocalPort());
        client.setSoTimeout(20000);
        try
        {
            OutputStream os=client.getOutputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

            os.write((
                    "GET /r1 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "\r\n"+
                    "GET /r2 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "connection: close\r\n"+
                    "\r\n"
            ).getBytes());
            os.flush();

            // read the chunked response header
            boolean chunked=false;
            boolean closed=false;
            while(true)
            {
                String line=in.readLine();
                if (line==null || line.length()==0)
                    break;

                chunked|="Transfer-Encoding: chunked".equals(line);
                closed|="Connection: close".equals(line);
            }
            Assert.assertTrue(chunked);
            Assert.assertFalse(closed);

            // Read the chunks
            int max=Integer.MIN_VALUE;
            while(true)
            {
                String chunk=in.readLine();
                String line=in.readLine();
                if (line.length()==0)
                    break;
                int len=line.length();
                Assert.assertEquals(Integer.valueOf(chunk,16).intValue(),len);
                if (max<len)
                    max=len;
            }

            // Check that a direct content buffer was used as a chunk
            Assert.assertEquals(128*1024,max);

            // read and check the times are < 999ms
            String[] times=in.readLine().split(",");
            for (String t: times)
               Assert.assertTrue(Integer.valueOf(t).intValue()<999);

            // read the EOF chunk
            String end=in.readLine();
            Assert.assertEquals("0",end);
            end=in.readLine();
            Assert.assertEquals(0,end.length());

            // read the non-chunked response header
            chunked=false;
            closed=false;
            while(true)
            {
                String line=in.readLine();
                if (line==null || line.length()==0)
                    break;

                chunked|="Transfer-Encoding: chunked".equals(line);
                closed|="Connection: close".equals(line);
            }
            Assert.assertFalse(chunked);
            Assert.assertTrue(closed);

            String bigline = in.readLine();
            Assert.assertEquals(10*128*1024,bigline.length());

            // read and check the times are < 999ms
            times=in.readLine().split(",");
            for (String t: times)
                Assert.assertTrue(t,Integer.valueOf(t).intValue()<999);

            // check close
            Assert.assertTrue(in.readLine()==null);
        }
        finally
        {
            client.close();
        }
    }

    // Handler that sends big blocks of data in each of 10 writes, and then sends the time it took for each big block.
    protected static class BigBlockHandler extends AbstractHandler
    {
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        {
            byte[] buf = new byte[128*1024];
            for (int i=0;i<buf.length;i++)
                buf[i]=(byte)("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_".charAt(i%63));

            baseRequest.setHandled(true);
            response.setStatus(200);
            response.setContentType("text/plain");
            ServletOutputStream out=response.getOutputStream();
            long[] times=new long[10];
            for (int i=0;i<times.length;i++)
            {
                // System.err.println("\nBLOCK "+request.getRequestURI()+" "+i);
                long start=System.currentTimeMillis();
                out.write(buf);
                long end=System.currentTimeMillis();
                times[i]=end-start;
                // System.err.println("Block "+request.getRequestURI()+" "+i+" "+times[i]);
            }
            out.println();
            for (long t : times)
            {
                out.print(t);
                out.print(",");
            }
            out.close();
        }
    }

    @Test
    public void testPipeline() throws Exception
    {
        configureServer(new HelloWorldHandler());

        //for (int pipeline=1;pipeline<32;pipeline++)
        for (int pipeline=1;pipeline<32;pipeline++)
        {
            Socket client=newSocket(HOST,_connector.getLocalPort());
            try
            {
                client.setSoTimeout(5000);
                OutputStream os=client.getOutputStream();

                String request="";

                for (int i=1;i<pipeline;i++)
                    request+=
                        "GET /data?writes=1&block=16&id="+i+" HTTP/1.1\r\n"+
                        "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                        "user-agent: testharness/1.0 (blah foo/bar)\r\n"+
                        "accept-encoding: nothing\r\n"+
                        "cookie: aaa=1234567890\r\n"+
                        "\r\n";

                request+=
                    "GET /data?writes=1&block=16 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "user-agent: testharness/1.0 (blah foo/bar)\r\n"+
                    "accept-encoding: nothing\r\n"+
                    "cookie: aaa=bbbbbb\r\n"+
                    "Connection: close\r\n"+
                    "\r\n";

                os.write(request.getBytes());
                os.flush();

                LineNumberReader in = new LineNumberReader(new InputStreamReader(client.getInputStream()));

                String line = in.readLine();
                int count=0;
                while (line!=null)
                {
                    if ("HTTP/1.1 200 OK".equals(line))
                        count++;
                    line = in.readLine();
                }
                assertEquals(pipeline,count);
            }
            finally
            {
                client.close();
            }
        }
    }

    @Test
    public void testRecycledWriters() throws Exception
    {
        configureServer(new EchoHandler());

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            os.write((
                    "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "content-type: text/plain; charset=utf-8\r\n"+
                    "content-length: 10\r\n"+
                    "\r\n").getBytes("iso-8859-1"));

            os.write((
                    "123456789\n"
            ).getBytes("utf-8"));

            os.write((
                    "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "content-type: text/plain; charset=utf-8\r\n"+
                    "content-length: 10\r\n"+
                    "\r\n"
            ).getBytes("iso-8859-1"));

            os.write((
                    "abcdefghZ\n"
            ).getBytes("utf-8"));

            String content="Wibble";
            byte[] contentB=content.getBytes("utf-8");
            os.write((
                    "POST /echo?charset=utf-16 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "content-type: text/plain; charset=utf-8\r\n"+
                    "content-length: "+contentB.length+"\r\n"+
                    "connection: close\r\n"+
                    "\r\n"
            ).getBytes("iso-8859-1"));
            os.write(contentB);

            os.flush();

            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            IO.copy(is,bout);
            byte[] b=bout.toByteArray();

            //System.err.println("OUTPUT: "+new String(b));
            int i=0;
            while (b[i]!='Z')
                i++;
            int state=0;
            while(state!=4)
            {
                switch(b[i++])
                {
                    case '\r':
                        if (state==0||state==2)
                            state++;
                        continue;
                    case '\n':
                        if (state==1||state==3)
                            state++;
                        continue;

                    default:
                        state=0;
                }
            }

            String in = new String(b,0,i,"utf-8");
            assertTrue(in.indexOf("123456789")>=0);
            assertTrue(in.indexOf("abcdefghZ")>=0);
            assertTrue(in.indexOf("Wibble")<0);

            in = new String(b,i,b.length-i,"utf-16");
            assertEquals("Wibble\n",in);
        }
        finally
        {
            client.close();
        }
    }

    @Test
    public void testHead() throws Exception
    {
        configureServer(new EchoHandler(false));

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            os.write((
                "POST /R1 HTTP/1.1\015\012"+
                "Host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                "content-type: text/plain; charset=utf-8\r\n"+
                "content-length: 10\r\n"+
                "\015\012"+
                "123456789\n" +

                "HEAD /R1 HTTP/1.1\015\012"+
                "Host: "+HOST+":"+_connector.getLocalPort()+"\015\012"+
                "content-type: text/plain; charset=utf-8\r\n"+
                "content-length: 10\r\n"+
                "\015\012"+
                "123456789\n"+

                "POST /R1 HTTP/1.1\015\012"+
                "Host: "+HOST+":"+_connector.getLocalPort()+"\015\012"+
                "content-type: text/plain; charset=utf-8\r\n"+
                "content-length: 10\r\n"+
                "Connection: close\015\012"+
                "\015\012"+
                "123456789\n"

                ).getBytes("iso-8859-1"));

            String in = IO.toString(is);

            int index=in.indexOf("123456789");
            assertTrue(index>0);
            index=in.indexOf("123456789",index+1);
            assertTrue(index>0);
            index=in.indexOf("123456789",index+1);
            assertTrue(index==-1);

        }
        finally
        {
            client.close();
        }
    }

    @Test
    public void testRecycledReaders() throws Exception
    {
        configureServer(new EchoHandler());

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            os.write((
                    "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "content-type: text/plain; charset=utf-8\r\n"+
                    "content-length: 10\r\n"+
                    "\r\n").getBytes("iso-8859-1"));

            os.write((
                    "123456789\n"
            ).getBytes("utf-8"));

            os.write((
                    "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "content-type: text/plain; charset=utf-8\r\n"+
                    "content-length: 10\r\n"+
                    "\r\n"
            ).getBytes("iso-8859-1"));

            os.write((
                    "abcdefghi\n"
            ).getBytes("utf-8"));

            String content="Wibble";
            byte[] contentB=content.getBytes("utf-16");
            os.write((
                    "POST /echo?charset=utf-8 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "content-type: text/plain; charset=utf-16\r\n"+
                    "content-length: "+contentB.length+"\r\n"+
                    "connection: close\r\n"+
                    "\r\n"
            ).getBytes("iso-8859-1"));
            os.write(contentB);

            os.flush();

            String in = IO.toString(is);
            assertTrue(in.indexOf("123456789")>=0);
            assertTrue(in.indexOf("abcdefghi")>=0);
            assertTrue(in.indexOf("Wibble")>=0);
        }
        finally
        {
            client.close();
        }
    }

    @Test
    public void testBlockedClient() throws Exception
    {
        configureServer(new HelloWorldHandler());

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            // Send a request with chunked input and expect 100
            os.write((
                    "GET / HTTP/1.1\r\n"+
                    "Host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "Transfer-Encoding: chunked\r\n"+
                    "Expect: 100-continue\r\n"+
                    "Connection: Keep-Alive\r\n"+
                    "\r\n"
            ).getBytes());

            // Never send a body.
            // HelloWorldHandler does not read content, so 100 is not sent.
            // So close will have to happen anyway, without reset!

            os.flush();

            client.setSoTimeout(2000);
            long start=System.currentTimeMillis();
            String in = IO.toString(is);
            assertTrue(System.currentTimeMillis()-start<1000);
            assertTrue(in.indexOf("Connection: close")>0);
            assertTrue(in.indexOf("Hello world")>0);

        }
        finally
        {
            client.close();
        }
    }

    @Test
    public void testCommittedError() throws Exception
    {
        CommittedErrorHandler handler =new CommittedErrorHandler();
        configureServer(handler);

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            ((StdErrLog)Log.getLogger(AbstractHttpConnection.class)).setHideStacks(true);
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            // Send a request
            os.write((
                    "GET / HTTP/1.1\r\n"+
                    "Host: "+HOST+":"+_connector.getLocalPort()+"\r\n" +
                    "\r\n"
            ).getBytes());
            os.flush();

            client.setSoTimeout(2000);
            String in = IO.toString(is);

            assertEquals(-1,is.read()); // Closed by error!

            assertTrue(in.indexOf("HTTP/1.1 200 OK")>=0);
            assertTrue(in.indexOf("Transfer-Encoding: chunked")>0);
            assertTrue(in.indexOf("Now is the time for all good men to come to the aid of the party")>0);
            assertTrue(in.indexOf("\r\n0\r\n")==-1); // chunking is interrupted by error close

            client.close();
            Thread.sleep(100);
            assertTrue(!handler._endp.isOpen());
        }
        finally
        {
            ((StdErrLog)Log.getLogger(AbstractHttpConnection.class)).setHideStacks(false);

            if (!client.isClosed())
                client.close();
        }
    }

    protected static class CommittedErrorHandler extends AbstractHandler
    {
        public EndPoint _endp;

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        {
            _endp=baseRequest.getConnection().getEndPoint();
            response.setHeader("test","value");
            response.setStatus(200);
            response.setContentType("text/plain");
            response.getWriter().println("Now is the time for all good men to come to the aid of the party");
            response.getWriter().flush();
            response.flushBuffer();

            throw new ServletException(new Exception("exception after commit"));
        }
    }

    protected static class AvailableHandler extends AbstractHandler
    {
        public Exchanger<Object> _ex = new Exchanger<Object>();

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        {
            baseRequest.setHandled(true);
            response.setStatus(200);
            response.setContentType("text/plain");
            InputStream in = request.getInputStream();
            ServletOutputStream out=response.getOutputStream();

            // should always be some input available, because of deferred dispatch.
            int avail=in.available();
            out.println(avail);

            String buf="";
            for (int i=0;i<avail;i++)
                buf+=(char)in.read();

            avail=in.available();
            out.println(avail);

            try
            {
                _ex.exchange(null);
                _ex.exchange(null);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }

            avail=in.available();

            if (avail==0)
            {
                // handle blocking channel connectors
                buf+=(char)in.read();
                avail=in.available();
                out.println(avail+1);
            }
            else if (avail==1)
            {
                // handle blocking socket connectors
                buf+=(char)in.read();
                avail=in.available();
                out.println(avail+1);
            }
            else
                out.println(avail);

            while (avail>0)
            {
                buf+=(char)in.read();
                avail=in.available();
            }

            out.println(avail);
            out.println(buf);
            out.close();
        }
    }

    @Test
    public void testAvailable() throws Exception
    {
        AvailableHandler ah=new AvailableHandler();
        configureServer(ah);

        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            os.write((
                    "GET /data?writes=1024&block=256 HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "connection: close\r\n"+
                    "content-type: unknown\r\n"+
                    "content-length: 30\r\n"+
                    "\r\n"
            ).getBytes());
            os.flush();
            Thread.sleep(500);
            os.write((
                    "1234567890"
            ).getBytes());
            os.flush();

            ah._ex.exchange(null);

            os.write((
                    "abcdefghijklmnopqrst"
            ).getBytes());
            os.flush();
            Thread.sleep(500);
            ah._ex.exchange(null);

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            // skip header
            while(reader.readLine().length()>0);
            assertThat(Integer.parseInt(reader.readLine()),Matchers.greaterThan(0));
            assertEquals(0,Integer.parseInt(reader.readLine()));
            assertThat(Integer.parseInt(reader.readLine()),Matchers.greaterThan(0));
            assertEquals(0,Integer.parseInt(reader.readLine()));
            assertEquals("1234567890abcdefghijklmnopqrst",reader.readLine());

        }
        finally
        {
            client.close();
        }
    }

    @Test
    public void testDualRequest1() throws Exception
    {
        configureServer(new HelloWorldHandler());

        Socket client1=newSocket(HOST,_connector.getLocalPort());
        Socket client2=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os1=client1.getOutputStream();
            OutputStream os2=client2.getOutputStream();

            os1.write(REQUEST1.getBytes());
            os2.write(REQUEST1.getBytes());
            os1.flush();
            os2.flush();

            // Read the response.
            String response1=readResponse(client1);
            String response2=readResponse(client2);

            // Check the response
            assertEquals("client1",RESPONSE1,response1);
            assertEquals("client2",RESPONSE1,response2);
        }
        finally
        {
            client1.close();
            client2.close();
        }
    }

    /**
     * Read entire response from the client. Close the output.
     *
     * @param client Open client socket.
     * @return The response string.
     * @throws IOException in case of I/O problems
     */
    protected static String readResponse(Socket client) throws IOException
    {
        BufferedReader br=null;

        StringBuilder sb=new StringBuilder();
        try
        {
            br=new BufferedReader(new InputStreamReader(client.getInputStream()));

            String line;

            while ((line=br.readLine())!=null)
            {
                sb.append(line);
                sb.append('\n');
            }

            return sb.toString();
        }
        catch(IOException e)
        {
            System.err.println(e+" while reading '"+sb+"'");
            throw e;
        }
        finally
        {
            if (br!=null)
            {
                br.close();
            }
        }
    }

    private void writeFragments(byte[] bytes, int[] points, StringBuilder message, OutputStream os) throws IOException, InterruptedException
    {
        int last=0;

        // Write out the fragments
        for (int j=0; j<points.length; ++j)
        {
            int point=points[j];

            os.write(bytes,last,point-last);
            last=point;
            os.flush();
            Thread.sleep(PAUSE);

            // Update the log message
            message.append(" point #").append(j + 1).append(": ").append(point);
        }

        // Write the last fragment
        os.write(bytes,last,bytes.length-last);
        os.flush();
        Thread.sleep(PAUSE);
    }

    @Test
    public void testUnreadInput () throws Exception
    {
        configureServer(new NoopHandler());
        final int REQS=5;
        String content="This is a loooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "ooooooooooooooooooooooooooooooooooooooooooooo"+
        "oooooooooooonnnnnnnnnnnnnnnnggggggggg content"+
        new String(new char[65*1024]);
        final byte[] bytes = content.getBytes();

        Socket client=newSocket(HOST,_connector.getLocalPort());
        final OutputStream out=client.getOutputStream();

        new Thread()
        {
            public void run()
            {
                try
                {
                    for (int i=0; i<REQS; i++)
                    {
                        out.write("GET / HTTP/1.1\r\nHost: localhost\r\n".getBytes(StringUtil.__ISO_8859_1));
                        out.write(("Content-Length: "+bytes.length+"\r\n" + "\r\n").getBytes(StringUtil.__ISO_8859_1));
                        out.write(bytes,0,bytes.length);
                    }
                    out.write("GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".getBytes(StringUtil.__ISO_8859_1));
                    out.flush();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }.start();

        String resps = readResponse(client);

        int offset=0;
        for (int i=0;i<(REQS+1);i++)
        {
            int ok=resps.indexOf("HTTP/1.1 200 OK",offset);
            assertThat("resp"+i,ok,greaterThanOrEqualTo(offset));
            offset=ok+15;
        }
    }

    public class NoopHandler extends AbstractHandler
    {
        public void handle(String target, Request baseRequest,
                HttpServletRequest request, HttpServletResponse response) throws IOException,
                ServletException
        {
           //don't read the input, just send something back
            ((Request)request).setHandled(true);
            response.setStatus(200);
        }
    }

    @Test
    public void testSuspendedPipeline() throws Exception
    {
        SuspendHandler suspend = new SuspendHandler();
        suspend.setSuspendFor(30000);
        suspend.setResumeAfter(1000);
        configureServer(suspend);

        long start=System.currentTimeMillis();
        Socket client=newSocket(HOST,_connector.getLocalPort());
        try
        {
            OutputStream os=client.getOutputStream();
            InputStream is=client.getInputStream();

            // write an initial request
            os.write((
                    "GET / HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "\r\n"
            ).getBytes());
            os.flush();

            Thread.sleep(200);

            // write an pipelined request
            os.write((
                    "GET / HTTP/1.1\r\n"+
                    "host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
                    "connection: close\r\n"+
                    "\r\n"
            ).getBytes());
            os.flush();

            String response=readResponse(client);
            assertThat(response,JUnitMatchers.containsString("RESUMEDHTTP/1.1 200 OK"));
            assertThat((System.currentTimeMillis()-start),greaterThanOrEqualTo(1999L));

            // TODO This test should also check that that the CPU did not spin during the suspend.
        }
        finally
        {
            client.close();
        }
    }
}


text:
class Butler {
  constructor() {
    this.middlewares = [];
  }

  use(middleware) {
    this.middlewares.push(middleware);
  }

  react(event,value) {

  }
}

module.exports = Butler;


text:
package org.apache.hyracks.storage.am.lsm.common.impls;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.hyracks.api.exceptions.HyracksDataException;
import org.apache.hyracks.storage.am.lsm.common.api.ILSMIOOperation;
import org.apache.hyracks.storage.am.lsm.common.api.ILSMIOOperation.LSMIOOpertionType;
import org.apache.hyracks.storage.am.lsm.common.api.ILSMIOOperationScheduler;

public class AsynchronousScheduler implements ILSMIOOperationScheduler {
    // Since this is a asynchronous scheduler, we make sure that flush operations coming from the same lsm index
    // will be executed serially in same order of scheduling the operations. Look at asterix issue 630.

    public final static AsynchronousScheduler INSTANCE = new AsynchronousScheduler();
    private ExecutorService executor;
    private final Map<String, ILSMIOOperation> runningFlushOperations = new HashMap<String, ILSMIOOperation>();
    private final Map<String, PriorityQueue<ILSMIOOperation>> waitingFlushOperations = new HashMap<String, PriorityQueue<ILSMIOOperation>>();

    public void init(ThreadFactory threadFactory) {
        // Creating an executor with the same configuration of Executors.newCachedThreadPool.
        executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>(), threadFactory) {

            @Override
            protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
                return new LSMIOOperationTask<T>(callable);
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                super.afterExecute(r, t);
                LSMIOOperationTask<Boolean> task = (LSMIOOperationTask<Boolean>) r;
                ILSMIOOperation executedOp = task.getOperation();
                String id = executedOp.getIndexUniqueIdentifier();
                synchronized (this) {
                    runningFlushOperations.remove(id);
                    if (waitingFlushOperations.containsKey(id)) {
                        try {
                            ILSMIOOperation op = waitingFlushOperations.get(id).poll();
                            if (op != null) {
                                scheduleOperation(op);
                            } else {
                                waitingFlushOperations.remove(id);
                            }
                        } catch (HyracksDataException e) {
                            t = e.getCause();
                        }
                    }
                }
            }
        };
    }

    @Override
    public void scheduleOperation(ILSMIOOperation operation) throws HyracksDataException {
        if (operation.getIOOpertionType() == LSMIOOpertionType.MERGE) {
            executor.submit(operation);
        } else {
            String id = operation.getIndexUniqueIdentifier();
            synchronized (executor) {
                if (runningFlushOperations.containsKey(id)) {
                    if (waitingFlushOperations.containsKey(id)) {
                        waitingFlushOperations.get(id).offer(operation);
                    } else {
                        PriorityQueue<ILSMIOOperation> q = new PriorityQueue<ILSMIOOperation>();
                        q.offer(operation);
                        waitingFlushOperations.put(id, q);
                    }
                } else {
                    runningFlushOperations.put(id, operation);
                    executor.submit(operation);
                }
            }
        }
    }
}


text:
package org.locationtech.geowave.core.ingest.kafka;

import org.apache.avro.specific.SpecificRecordBase;
import org.locationtech.geowave.core.ingest.avro.GenericAvroSerializer;
import kafka.serializer.Encoder;
import kafka.utils.VerifiableProperties;

/**
 * Default encoder used by Kafka to serialize Avro generated Java object to binary. This class is
 * specified as a property in the Kafka config setup.
 *
 * <p> Key: serializer.class
 *
 * <p> Value: org.locationtech.geowave.core.ingest.kafka.AvroKafkaEncoder
 *
 * @param <T> - Base Avro class extended by all generated class files
 */
public class KafkaAvroEncoder<T extends SpecificRecordBase> implements Encoder<T> {
  private final GenericAvroSerializer<T> serializer = new GenericAvroSerializer<>();

  public KafkaAvroEncoder(final VerifiableProperties verifiableProperties) {
    // This constructor must be present to avoid runtime errors
  }

  @Override
  public byte[] toBytes(final T object) {
    return GenericAvroSerializer.serialize(object, object.getSchema());
  }
}


text:
"use strict";

describe("Home module", function () {

    beforeEach(module("MyApp.Home"));

    describe("HomeCtrl", function() {
        var scope, ctrl;

        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl  = $controller("HomeCtrl", {$scope: scope});
        }));

        it('ensure the title is set', function() {
            expect(ctrl.title).toBe("This is the home view!");
        });
    });
});


text:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference
        android:defaultValue="@string/pref_serverHostname_default"
        android:inputType="textUri"
        android:key="@string/pref_serverHostname_key"
        android:summary="@string/pref_serverHostname_summ"
        android:title="@string/pref_serverHostname_title" />
</PreferenceScreen>


text:
var application_root = __dirname,
    express = require("express"),
    path = require("path"),
    mongoose = require('mongoose');

var app = express.createServer();

// database

mongoose.connect('mongodb://localhost/ecomm_database');

// config

app.configure(function () {
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(application_root, "public")));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

var Schema = mongoose.Schema; //Schema.ObjectId

// Schemas

var Sizes = new Schema({
    size: { type: String, required: true },
    available: { type: Number, required: true, min: 0, max: 1000 },
    sku: {
        type: String,
        required: true,
        validate: [/[a-zA-Z0-9]/, 'Product sku should only have letters and numbers']
    },
    price: { type: Number, required: true, min: 0 }
});

var Images = new Schema({
    kind: {
        type: String,
        enum: ['thumbnail', 'catalog', 'detail', 'zoom'],
        required: true
    },
    url: { type: String, required: true }
});

var Variants = new Schema({
    color: String,
    images: [Images],
    sizes: [Sizes]
});

var Categories = new Schema({
    name: String
});

var Catalogs = new Schema({
    name: String
});

// Product Model

var Product = new Schema({
    title: { type: String, required: true },
    description: { type: String, required: true },
    style: { type: String, unique: true },
    images: [Images],
    categories: [Categories],
    catalogs: [Catalogs],
    variants: [Variants],
    modified: { type: Date, default: Date.now }
});

// validation

Product.path('title').validate(function (v) {
    console.log("validate title");
    console.log(v);
    return v.length > 10 && v.length < 70;
});

Product.path('style').validate(function (v) {
    console.log("validate style");
    console.log(v);
    return v.length < 40;
}, 'Product style attribute is should be less than 40 characters');

Product.path('description').validate(function (v) {
    console.log("validate description");
    console.log(v);
    return v.length > 10;
}, 'Product description should be more than 10 characters');

var ProductModel = mongoose.model('Product', Product);

/* Product Document
[
{
  "title": "My Awesome T-shirt",
  "description": "All about the details. Of course it's black.",
  "images": [
    {
      "kind": "thumbnail",
      "url": "images/products/1234/main.jpg"
    }
  ],
  "categories": [
      { "name": "Clothes" },
      { "name": "Shirts" }
  ],
  "style": "1234",
  "variants": [
    {
      "color": "Black",
      "images": [
        {
          "kind": "thumbnail",
          "url": "images/products/1234/thumbnail.jpg"
        },
        {
          "kind": "catalog",
          "url": "images/products/1234/black.jpg"
        }
      ],
      "sizes": [
        {
          "size": "S",
          "available": 10,
          "sku": "CAT-1234-Blk-S",
          "price": 99.99
        },
        {
          "size": "M",
          "available": 7,
          "sku": "CAT-1234-Blk-M",
          "price": 109.99
        }
      ]
    }
  ],
  "catalogs": [
      { "name": "Apparel" }
  ]
}
]
*/

// REST api

app.get('/api', function (req, res) {
  res.send('Ecomm API is running');
});

// POST to CREATE
app.post('/api/products', function (req, res) {
  var product;
  console.log("POST: ");
  console.log(req.body);
  product = new ProductModel({
    title: req.body.title,
    description: req.body.description,
    style: req.body.style,
    images: req.body.images,
    categories: req.body.categories,
    catalogs: req.body.catalogs,
    variants: req.body.variants
  });
  product.save(function (err) {
    if (!err) {
      return console.log("created");
    } else {
      return console.log(err);
    }
  });
  return res.send(product);
});

// PUT to UPDATE

// Bulk update
app.put('/api/products', function (req, res) {
    var i, len = 0;
    console.log("is Array req.body.products");
    console.log(Array.isArray(req.body.products));
    console.log("PUT: (products)");
    console.log(req.body.products);
    if (Array.isArray(req.body.products)) {
        len = req.body.products.length;
    }
    for (i = 0; i < len; i++) {
        console.log("UPDATE product by id:");
        for (var id in req.body.products[i]) {
            console.log(id);
        }
        ProductModel.update({ "_id": id }, req.body.products[i][id], function (err, numAffected) {
            if (err) {
                console.log("Error on update");
                console.log(err);
            } else {
                console.log("updated num: " + numAffected);
            }
        });
    }
    return res.send(req.body.products);
});

// Single update
app.put('/api/products/:id', function (req, res) {
  return ProductModel.findById(req.params.id, function (err, product) {
    product.title = req.body.title;
    product.description = req.body.description;
    product.style = req.body.style;
    product.images = req.body.images;
    product.categories = req.body.categories;
    product.catalogs = req.body.catalogs;
    product.variants = req.body.variants;
    return product.save(function (err) {
      if (!err) {
        console.log("updated");
      } else {
        console.log(err);
      }
      return res.send(product);
    });
  });
});

// GET to READ

// List products
app.get('/api/products', function (req, res) {
  return ProductModel.find(function (err, products) {
    if (!err) {
      return res.send(products);
    } else {
      return console.log(err);
    }
  });
});

// Single product
app.get('/api/products/:id', function (req, res) {
  return ProductModel.findById(req.params.id, function (err, product) {
    if (!err) {
      return res.send(product);
    } else {
      return console.log(err);
    }
  });
});

// DELETE to DESTROY

// Bulk destroy all products
app.delete('/api/products', function (req, res) {
  ProductModel.remove(function (err) {
    if (!err) {
      console.log("removed");
      return res.send('');
    } else {
      console.log(err);
    }
  });
});

// remove a single product
app.delete('/api/products/:id', function (req, res) {
  return ProductModel.findById(req.params.id, function (err, product) {
    return product.remove(function (err) {
      if (!err) {
        console.log("removed");
        return res.send('');
      } else {
        console.log(err);
      }
    });
  });
});

// launch server

app.listen(4242);


text:
package counters

import (
	"bufio"
	"bytes"
)

// Bytes represent writer for counting bytes.
type Bytes int

func (b *Bytes) Write(p []byte) (int, error) {
	*b += Bytes(len(p))
	return len(p), nil
}

// Words represent writer for counting words.
type Words int

func (w *Words) Write(p []byte) (int, error) {
	var wc int
	input := bufio.NewScanner(bytes.NewBuffer(p))
	input.Split(bufio.ScanWords)
	for input.Scan() {
		wc++
	}
	if input.Err() != nil {
		return 0, input.Err()
	}
	*w += Words(wc)
	return wc, nil
}

// Lines represent writer for counting lines.
type Lines int

func (l *Lines) Write(p []byte) (int, error) {
	var wc int
	input := bufio.NewScanner(bytes.NewBuffer(p))
	input.Split(bufio.ScanLines)
	for input.Scan() {
		wc++
	}
	if input.Err() != nil {
		return 0, input.Err()
	}
	*l += Lines(wc)
	return wc, nil
}


text:
require_relative 'load_config'
require_relative 'server'
require_relative 'mail_sender'

module PaypalIpnForwarder
  class ServerIpnReceptionChecker

    PROCESS_ID_IPN_CHECKER = '.process_id_for_ipn_checker'

    def initialize(server, paypal_id, is_test_mode=false)
      @content = LoadConfig.new(is_test_mode)
      @server = server
      @paypal_id = paypal_id
      @time_test_started = Time.now
      @time_before_email = @content.no_ipn_time_before_notification
      @ipn_received = false

    end

    def check_ipn_received
      @process_id = fork do

        Signal.trap('HUP') do
          @ipn_received = true
        end

        verify_ipn_received

      end
      Process.detach(@process_id)
      File.write(PROCESS_ID_IPN_CHECKER + '_' + @paypal_id, @process_id, nil, nil)
    end

    def verify_ipn_received(time=1.0)
      loop do
        if Time.now >= @time_test_started + @time_before_email
          send_email_that_no_ipn_received
          break
        end
        break if @ipn_received
        sleep time
      end
    end

    def send_email_that_no_ipn_received
      email_map = @server.email_map
      to = email_map[@paypal_id]
      subject = "No IPNs are being received from paypal sandbox ##{@paypal_id} on the paypal IPN forwarder"
      body = "Test mode has been turned on for sandbox with id: #{@paypal_id} but no IPN has been received for it in an 9 minutes.
There most likely is an issue with the paypal sandbox."
      mailsender = MailSender.new
      mailsender.send_mail(to, subject, body)
    end

    def ipn_received
      @ipn_received = true
    end

    def ipn_received?
      @ipn_received
    end
  end
end


text:
<?php

/* Testing Error Conditions */
echo "*** Testing Error Conditions ***\n";

/* zero argument */
var_dump( fprintf() );

/* scalar argument */
var_dump( fprintf(3) );

/* NULL argument */
var_dump( fprintf(NULL) );

echo "Done\n";
?>


text:
package com.kissthinker.proxy;

/**
 * @author David Ainslie
 *
 */
public interface Proxy
{
    /**
     * Create (instantiate) an object for the given class which is wrapped within a {@link ClassInfo}.
     * Instead of simply passing in one argument of {@link Class} to be used as the blueprint for creating a proxy object (which was actually the way it was initially coded),
     * a wrapper is used for any future additions/enhancements to {@link ClassInfo} allowing client code to access future "extras" but without changing the method contract.
     * @param classInfo Class info
     * @param <O> Object type
     * @param <P> Parent type
     * @return O a proxy for the given (wrapped) class
     */
    <O, P> O create(ClassInfo<O, P> classInfo);

    /**
     *
     * @author David Ainslie
     *
     */
    public static class Null implements Proxy
    {
        /**
         *
         * @see com.kissthinker.proxy.Proxy#create(ClassInfo)
         */
        @Override
        public <O, P> O create(ClassInfo<O, P> classInfo)
        {
            return null;
        }
    }

    /**
     *
     * @author David Ainslie
     *
     * @param <O> Object type
     * @param <P> Parent type
     */
    public static class ClassInfo<O, P>
    {
        /** */
        private final Class<O> proxyClass;

        /** */
        private Class<P> parentClass;

        /** */
        private P parent;

        /**
         * @param proxyClass Proxy class
         */
        public ClassInfo(Class<O> proxyClass)
        {
            super();
            this.proxyClass = proxyClass;
        }

        /**
         * Getter
         * @return the proxyClass
         */
        public Class<O> proxyClass()
        {
            return proxyClass;
        }

        /**
         * Getter
         * @return the parentClass
         */
        public Class<P> parentClass()
        {
            return parentClass;
        }

        /**
         * Setter
         * @param parentClass the parentClass to set
         * @return ClassInfo
         */
        public ClassInfo<O, P> parentClass(Class<P> parentClass)
        {
            this.parentClass = parentClass;
            return this;
        }

        /**
         * Getter
         * @return the parent
         */
        public P parent()
        {
            return parent;
        }

        /**
         * Setter
         * @param parent the parent to set
         * @return ClassInfo
         */
        public ClassInfo<O, P> parent(P parent)
        {
            this.parent = parent;
            return this;
        }
    }
}


text:
<?php

class NavActions extends Actions
{
    protected static $navUri;

    public static function setNavUri($uri)
    {
        static::$navUri = $uri;
    }

    public static function getNavUri()
    {
        return static::$navUri ?: Request::getRelativeUri();
    }

    public static function prepareFooterPartial(array $vars)
    {
        return $vars + [
      'isDark' => false,
      'showLearnFooter' => false,
    ];
    }

    public static function prepareGlobalItemsPartial(array $vars)
    {
        return $vars += [
      'selectedItem' => static::getNavUri(),
      'selectedCulture' => i18n::getLanguage() . '_' . i18n::getCountry(),
      'cultures' => i18n::getAllCultures()
    ];
    }

    public static function execute400(array $vars)
    {
        Response::setStatus(400);
        return ['page/400', ['error' => $vars['error'] ?? null]];
    }

    public static function execute404()
    {
//    $uri = Request::getRelativeUri();
//    Controller::queueToRunAfterResponse(function() use($uri) {
//      Slack::sendErrorIfProd('404 for url ' . $uri, false);
//    });
        Response::setStatus(404);
        return ['page/404'];
    }
}


text:
package org.ovirt.engine.ui.common.widget.dialog;

import org.ovirt.engine.ui.common.widget.IsProgressContentWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.VerticalAlign;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;

public class ProgressPopupContent extends Composite implements IsProgressContentWidget {

    interface WidgetUiBinder extends UiBinder<Widget, ProgressPopupContent> {
        WidgetUiBinder uiBinder = GWT.create(WidgetUiBinder.class);
    }

    @UiField
    Image progressImage;

    @UiField
    InlineLabel progressLabel;

    public ProgressPopupContent() {
        initWidget(WidgetUiBinder.uiBinder.createAndBindUi(this));
        progressImage.getElement().getStyle().setVerticalAlign(VerticalAlign.MIDDLE);
    }

    @Override
    public void setProgressMessage(String text) {
        progressLabel.setText(text);
        progressLabel.setVisible(text != null);
    }

}


text:
package sgl
package awt
package util

import sgl.util._

/*
 * These are default loggers that make sense in the JVM/Desktop
 * environment. They are not necessarly only for AWT, but until
 * we get more platforms they should at least be independant from
 * the core library since they assume the existence of a terminal.
 * They also use some color code to display nice color messages.
 */

trait TerminalLoggingProvider extends LoggingProvider {
  import Logger._

  private val ErrorPrefix = "[ Error ] "
  private val WarningPrefix = "[Warning] "
  private val InfoPrefix = "[ Info ] "
  private val DebugPrefix = "[ Debug ] "
  private val TracePrefix = "[ Trace ] "

  abstract class TerminalLogger extends Logger {
    def output(msg: String): Unit

    protected override def log(level: LogLevel, tag: Tag, msg: String): Unit = level match {
      case NoLogging => ()
      case Error => output(reline(ErrorPrefix, tag, msg))
      case Warning => output(reline(WarningPrefix, tag, msg))
      case Info => output(reline(InfoPrefix, tag, msg))
      case Debug => output(reline(DebugPrefix, tag, msg))
      case Trace => output(reline(TracePrefix, tag, msg))
    }

    private def reline(prefix: String, tag: Tag, msg: String): String = {
      val colorPrefix =
        if(prefix == ErrorPrefix)
          Console.RED
        else if(prefix == WarningPrefix)
          Console.YELLOW
        else if(prefix == DebugPrefix)
          Console.MAGENTA
        else if(prefix == TracePrefix)
          Console.GREEN
        else //for INFO
          Console.BLUE
      val colorTag = Console.CYAN
      "[" + colorPrefix + prefix.substring(1, prefix.length-2) + Console.RESET + "] " +
      "[ " + colorTag + tag.name + Console.RESET + " ] " +
      msg.trim.replaceAll("\n", "\n" + (" " * (prefix.size)))
    }
  }

}
trait StdErrLoggingProvider extends TerminalLoggingProvider {
  abstract class StdErrLogger extends TerminalLogger {
    override def output(msg: String): Unit = {
      Console.err.println(msg)
    }
  }
}

trait DefaultStdErrLoggingProvider extends StdErrLoggingProvider {
  val logger = DefaultStdErrLogger
  object DefaultStdErrLogger extends StdErrLogger {
    override val logLevel: Logger.LogLevel = Logger.Warning
  }
}

trait VerboseStdErrLoggingProvider extends StdErrLoggingProvider {
  val logger = VerboseStdErrLogger
  object VerboseStdErrLogger extends StdErrLogger {
    import Logger._
    override val logLevel: LogLevel = Debug
  }
}

trait TraceStdErrLoggingProvider extends StdErrLoggingProvider {
  val logger = TraceStdErrLogger
  object TraceStdErrLogger extends StdErrLogger {
    import Logger._
    override val logLevel: LogLevel = Trace
  }
}

trait StdOutLoggingProvider extends TerminalLoggingProvider {
  abstract class StdOutLogger extends TerminalLogger {
    override def output(msg: String): Unit = {
      Console.out.println(msg)
    }
  }
}

trait DefaultStdOutLoggingProvider extends StdOutLoggingProvider {
  val logger = DefaultStdOutLogger
  object DefaultStdOutLogger extends StdOutLogger {
    override val logLevel: Logger.LogLevel = Logger.Warning
  }
}

trait VerboseStdOutLoggingProvider extends StdOutLoggingProvider {
  val logger = VerboseStdOutLogger
  object VerboseStdOutLogger extends StdOutLogger {
    override val logLevel: Logger.LogLevel = Logger.Debug
  }
}

trait TraceStdOutLoggingProvider extends StdOutLoggingProvider {
  val logger = TraceStdOutLogger
  object TraceStdOutLogger extends StdOutLogger {
    override val logLevel: Logger.LogLevel = Logger.Trace
  }
}

// vim: set ts=4 sw=4 et:


text:
document.querySelector("#build-control").addEventListener("click", function() {
   var builder = new AnimationBuilder();
   builder.setType(document.querySelector("#type-select").value);
   builder.setBorderSize(document.querySelector("#border-size-select").value);
   builder.setBorderColor(document.querySelector("#border-color-select").value);
   builder.setBorderType(document.querySelector("#border-type-select").value);
   builder.setBackgroundColor(document.querySelector("#background-color-select").value);
   builder.setText(document.querySelector("#text-select").value);
   builder.setTextColor(document.querySelector("#text-color-select").value);
   builder.setTextSize(document.querySelector("#text-size-select").value);
   builder.setAnimation(document.querySelector("#animation-select").value);
   document.querySelector("#result-field").appendChild(builder.getResult().getContent());
});


text:
package org.innovateuk.ifs.management.publiccontent.viewmodel.section;

import org.innovateuk.ifs.management.publiccontent.viewmodel.AbstractPublicContentViewModel;

/**
 * View model for the search and supporting information section.
 */
public class SearchInformationViewModel extends AbstractPublicContentViewModel {
}