text:
/*

# Standard call

     Ruby.uniq([1, 1]) -> [1]
     Ruby.ord("ab")    -> 97

# Direct call

     Ruby[2].Array.prototype.uniq.call([1, 1]) -> [1]
     Ruby[2].String.prototype.ord.call("ab")   -> 97

# OOP Extended

     Ruby(2);

     [1, 1].uniq() -> [1]
     "ab".ord()    -> 97

 */

(function(global) {

// --- dependency modules ----------------------------------
//var Extend = global["Extend"];

// --- define / local variables ----------------------------
//var _runOnNode = "process" in global;
//var _runOnWorker = "WorkerLocation" in global;
//var _runOnBrowser = "document" in global;

// --- class / interfaces ----------------------------------
function Ruby(version) { // @arg Number = 2.0 - OOP Extended Ruby version number.
                         // @desc Extend functionality to the native objects.
    switch (version || 2.0) {
    case 2.0:
        Extend_tree(global, Ruby["2"]);
    }
}

//{@dev
Ruby["repository"] = "https://github.com/uupaa/Ruby.js";
//}@dev

Ruby["2"] = {
    "Array": {
        "prototype": {
            "times":    Array_times,        // Array#times(times):Array
            "all":      Array.prototype.every,
            "any":      Array.prototype.some,
            "at":       Array_at,           // Array#at(nth):Any
            "assoc":    Array_assoc,        // Array#assoc(find:Any):Array/undefined
            "clear":    Array_clear,        // Array#clear():Array
            "clone":    Array_clone,        // Array#clone():Array
            "collect":  Array.prototype.map,
            "compact":  Array_compact,      // Array#compact():Array
            "cycle":    Array_cycle,        // Array#cycle(num:Integer, callback:Function = null):Array
            "detect":   Array_find,
          //"delete":   Array_delete,       // Array#delete(value:Any, callback:Function = null):Array
            "delete_at":Array_delete_at,    // Array#delete_at(index:Integer):Any
            "delete_if":Array_delete_if,    // Array#delete_if(callback:Function):Array
            "dup":      Array_clone,        // Array#dup():Array
            "each":     Array.prototype.forEach,
            "each_index":Array.prototype.forEach,
            "each_with_index":Array.prototype.forEach,
            "empty":    Array_empty,        // Array#empty():Boolean
            "eql":      Array_eql,          // Array#eql(target:Array):Boolean
            "find":     Array_find,         // Array#find(callback, ifnone = null):
            "find_index":Array_index,
            "index":    Array_index,        // Array#index(any:Any):Number/undefined
          //"fetch":
          //"fill":
          //"flatten":
            "first":    Array_first,        // Array#first():Any/undefined
            "include":  Array_include,      // Array#include(any:Any):Boolean
          //"insert":
            "to_s":     Array.prototype.toString,
          //"inspect":
          //"join":
          //"keep_if":
            "last":     Array_last,         // Array#last():Any/undefined
            "map":      Array.prototype.map,
          //"size":
          //"permutation":
          //"pop":
          //"product":
          //"push":
          //"rassoc":
          //"replace":
          //"reverse":
          //"reverse_each":
          //"rindex":
          //"rotate":
          //"sample":
            "select":   Array.prototype.filter,
          //"shift":
          //"shuffle":
          //"slice":
          //"sort":
            "to_a":     Array.prototype.valudOf,
          //"to_ary":
          //"transpose":
            "uniq":     Array_uniq          // Array#uniq():DenseArray
          //"unshift":
          //"values_at":
          //"zip":
        }
    },
    "String": {
        "prototype": {
            "times":    String_repeat,
            "ascii_only": String_ascii_only,// String#ascii_only():Boolean
            "capitalize": String_capitalize,// String#capitalize():String
            "center":   String_center,      // String#center(width:Number, padding:String = " "):String
            "chars":    String_chars,       // String#chars():String
            "chomp":    String_chomp,       // String#chomp(separator = "\n"):String
            "chop":     String_chop,        // String#chop():String
            "chr":      String_chr,         // String#chr():String
            "codepoints":String_codepoints, // String#codepoints():String
            "count":    String_count,       // String#count(...:TRString, ...):Integer
            "delete":   String_delete,      // String#delete(...:String, ...):String
            "downcase": String.prototype.toLowerCase,
            "each_char":String_each_char,   // String#each_char(callback:Function = null):String/Array
            "each_codepoint":String_each_codepoint,   // String#each_codepoint(callback:Function = null):String/Array
            "each_line":String_each_line,   // String#each_line(callback:Function = null, rs = "\n"):String/Array
            "end_with": String_end_with,    // String#end_with(...:String, ...):Boolean
            "eql":      String_eql,         // String#eql(target:String):Boolean
            "gsub":     String_gsub,        // String#gsub(pattern:RegExp, replace:String/Function):String
            "hex":      String_hex,         // String#hex():Integer
            "include":  String_include,     // String#include(target:String):Boolean
            "index":    String.prototype.indexOf,
            "insert":   String_insert,      // String#insert(pos:Integer, target:String):String
            "size":     String_size,        // String#size():Integer
            "lines":    String_each_line,   // String#lines(callback:Function = null, rs = "\n"):String/Array
            "ljust":    String_ljust,       // String#ljust(width:Number, padding:String = " "):String
            "lstrip":   String_lstrip,      // String#lstrip():String
//          "match":    String_match,       // String#match(pattern:String, pos:Integer = 0):MatchedArray
//          "succ":     String_succ,        // String#succ():String
//          "next":     String_next,        // String#next():String
            "ord":      String_ord,         // String#ord():Integer
            "partition":String_partition,   // String#partition(separator:String):StringArray
//          "prepend":  String_prepend,     // String#prepend(target:String):String
            "reverse":  String_reverse,     // String#reverse():String;
            "rindex":   String.prototype.lastIndexOf,
            "rjust":    String_rjust,       // String#rjust(width:Number, padding:String = " "):String
            "rpartition":String_rpartition, // String#rpartition(separator:String):StringArray
            "rstrip":   String_rstrip,      // String#rstrip():String
            "scan":     String_scan,        // String#scan(pattern:RegExp/String):StringArray
            "squeeze":  String_squeeze,     // String#squeeze(...:String, ...):String
            "start_with":String_start_with, // String#start_with(...:String, ...):Boolean
            "strip":    String_strip,       // String#strip():String
            "swapcase": String_swapcase,    // String#swapcase():String
            "to_f":     String_to_f,        // String#to_f():String
            "to_i":     String_to_i,        // String#to_i():String
            "to_s":     String.prototype.toString,
            "to_str":   String.prototype.toString,
            "tr":       Stirng_tr,          // String#tr(pattern:TRString, replace:TRString):String
            "tr_s":     Stirng_tr_s,        // String#tr_s(pattern:TRString, replace:TRString):String
            "upcase":   String.prototype.toUpperCase
        }
    },
    "Number": {
        "prototype": {
            "to_s":     Number_to_s,        // Number#to_s(radix:Integer = 10):String
            "upto":     Number_upto,        // Number#upto(max:Number, callback:Function)
            "downto":   Number_upto,        // Number#downto(min:Number, callback:Function)
            "step":     Number_step         // Number#step(limit:Number, step:Number, callback:Function)
        }
    }
};

// --- implements ------------------------------------------
Array_uniq.call([].concat(Object.keys(Ruby["2"]["Array"]["prototype"]),
                          Object.keys(Ruby["2"]["String"]["prototype"]),
                          Object.keys(Ruby["2"]["Number"]["prototype"]))).forEach(function(name) {

    Ruby[name] = function(that) {
        var klass = typeof that === "string" ? "String"
                  : typeof that === "number" ? "Number"
                  : Array.isArray(that) ? "Array" : "";

        return Ruby["2"][klass]["prototype"][name].call(that, [].slice.call(arguments, 1));
    };
});

// --- Array -----------------------------------------------
function Array_times(times) { // @arg Integer
                              // @ret Array
    var rv = [];

    for (var i = 0, iz = times; i < iz; ++i) {
        rv.push(this);
    }
    return [].concat(rv);
}

function Array_at(nth) { // @arg Integer
                         // @ret Any
    if (nth < 0) {
        return this[this.length + nth] || undefined;
    }
    return this[nth] || undefined;
}

function Array_assoc(find) { // @arg Any - find value
                             // @ret Array|undefined:
    function _find(v) {
        return v[0] === find;
    }

    for (var i = 0, iz = this.length; i < iz; ++i) {
        if ( i in this && _find(this[i]) ) {
            return this[i];
        }
    }
    return;
}

function Array_clear() { // @ret Array - empry array.
    this.length = 0;
    return this;
}

function Array_clone() { // @ret Array - cloned array.
    var rv = [];
    for (var i = 0, iz = this.length; i < iz; ++i) {
        if (i in this) {
            rv[i] = this[i];
        }
    }
    return rv;
}

function Array_compact() { // @ret Array
    var rv = [];

    for (var i = 0, iz = this.length; i < iz; ++i) {
        if (i in this) {
            var value = this[i];

            if (value === value && value !== undefined && value !== null) {
                rv.push(value);
            }
        }
    }
    return rv;
}

function Array_cycle(num,        // @arg Integer
                     callback) { // @arg Function = null
                                 // @ret Array
//{@dev
    $valid($type(num, "Integer") && num >= 0, Array_cycle, "num");
    $valid($type(callback, "Function|omit"), Array_cycle, "callback");
//}@dev

    var rv = [];
    var index = 0;

    for (var i = 0; i < num; ++i) {
        for (var j = 0, jz = this.length; j < jz; ++j) {
            if (j in this) {
                var value = this[j];

                if (callback) {
                    callback(value, index++);
                }
                rv.push(value);
            }
        }
    }
    return rv;
}

/*
function Array_delete(value,      // @arg Any
                      callback) { // @arg Function = null
                                  // @ret Any
//{@dev
    $valid($type(callback, "Function|omit"), Array_delete, "callback");
//}@dev

    var rv = [];
    var lastMatchedValue = undefined;

    for (var i = 0, iz = this.length; i < iz; ++i) {
        if (i in this) {
            if (value === this[i]) {
                lastMatchedValue = this[i];
            } else {
                rv.push(this[i]);
            }
        }
    }
    if (lastMatchedValue === undefined) {
        if (callback) {
            for (var i = 0, iz = rv.length; i < iz; ++i) {
                rv[i] = callback(rv[i], i);
            }
            return rv;
        }
    }
    return lastMatchedValue;
}
 */

function Array_delete_at(index) { // @arg Integer
                                  // @ret Any
//{@dev
    $valid($type(index, "Integer"), Array_delete_at, "index");
//}@dev

    return this.splice(index, 1);
}

function Array_delete_if(callback) { // @arg Function
                                     // @ret Array
//{@dev
    $valid($type(callback, "Function"), Array_delete_if, "callback");
//}@dev

    var result = this.reduce(function(result, value, index) {
        if (!callback(value, index)) {
            result.push(value);
        }
        return result;
    }, []);

    this.length = 0;
    Array.prototype.push.apply(this, result);

    return this;
}

function Array_empty() { // @ret Boolean
    return !this.length;
}

function Array_eql(target) { // @arg Array
                             // @ret Boolean
    if (this.length !== target.length) {
        return false;
    }
    for (var i = 0, iz = this.length; i < iz; ++i) {
        if (this[i] !== target[i]) {
            return false;
        }
    }
    return true;
}

function Array_index(any) { // @arg Any
                            // @ret Number|undefined
    var rv = this.indexOf(any);

    return rv < 0 ? void 0 : rv;
}

function Array_first() { // @ret Any
//{@dev
    $valid(!arguments.length, Array_first, "num");
//}@dev

    return this.length ? this[0] : undefined;
}

function Array_find(callback, // @arg Function
                    ifnone) { // @arg Function = null
                              // @ret Any|undefined
    for (var i = 0, iz = this.length; i < iz; ++i) {
        var result = callback(this[i], i, this); // callback(value, index, array):Any

        if (result) {
            return this[i];
        }
    }
    return ifnone ? ifnone(this)
                  : undefined;
}

function Array_uniq() { // @ret DenseArray - new Array has unique value(s)
                        // @desc make array from unique values (avoid NaN, null and undefined values)
    var rv = [];

    for (var i = 0, iz = this.length; i < iz; ++i) {
        var value = this[i];

        if (value === value && value !== null && value !== undefined) { // avoid NaN, null, undefined.
            if (rv.indexOf(value) < 0) {
                rv.push(value);
            }
        }
    }
    return rv;
}

function Array_include(any) { // @arg Any
                              // @ret Boolean
    return this.indexOf(any) >= 0;
}

function Array_last() { // @ret Any
//{@dev
    $valid(!arguments.length, Array_last, "num");
//}@dev

    return this.length ? this[this.length - 1] : undefined;
}

// --- String ----------------------------------------------
function String_repeat(count) {
    count = count | 0;
    return (this.length && count > 0) ? Array(count + 1).join(this) : "";
}

function String_ascii_only() { // @ret Boolean
    for (var i = 0, iz = this.length; i < iz; ++i) {
        if (this.charCodeAt(i) > 0x7f) {
            return false;
        }
    }
    return true;
}

function String_capitalize() { // @ret String
    return this.slice(0, 1).toUpperCase() + this.slice(1).toLowerCase();
}

function String_center(width,     // @arg Integer
                       padding) { // @arg String = " "
//{@dev
    $valid($type(width, "Integer"),       String_center, "width");
    $valid($type(padding, "String|omit"), String_center, "padding");
//}@dev

    padding = padding || " ";

    if (width <= this.length) {
        return this.valueOf();
    }
    var num = width - this.length;
    var lpad = (num / 2) | 0;
    var rpad = (num / 2 + 0.5) | 0;
    var filler = String_repeat.call(padding, width).slice(0, width);
    var rv = filler.slice(0, lpad) + this + filler.slice(-rpad);

    return rv;
}

function String_chars() { // @ret String
    return this.split("");
}

function String_chomp(separator) { // @arg String = "\n" - trim char.
                                   // @ret String
    separator = separator === undefined ? "\n" : separator;

    switch (separator) {
    case null: return this.valueOf();
    case "\n": return this.replace(/(\r\n|\r|\n)$/,  "");
    case "":   return this.replace(/(\r\n|\r|\n)+$/, "");
    }
    return this.replace(RegExp(separator), "");
}

function String_chop() { // @ret String
    var iz = this.length;

    if (iz >= 2 && this[iz - 2] === "\r" && this[iz - 1] === "\n") {
        return this.slice(0, -2);
    }
    return this.slice(0, -1);
}

function String_chr() { // @ret String
    return this[0];
}

function String_codepoints() { // @ret UTF16IntegrArray
    var result = [];

    for (var i = 0, iz = this.length; i < iz; ++i) {
        result.push( this.charCodeAt(i) );
    }
    return result;
}

function String_count(/* ... */) { // @var_args TRString - "a-c", "^0-9"
                                   // @ret Integer
    var rex = new CharSet().set([].slice.call(arguments)).get().liner;
    var rv = this.match(RegExp("[" + rex + "]", "g"));

    return rv.length;
}

function String_delete(/* ... */) { // @var_args String - eg: "a-c", "^0-9"
                                    // @ret String
    var rex = new CharSet().set([].slice.call(arguments)).get().liner;
    var rv = this.replace(RegExp("[" + rex + "]", "g"), "");

    return rv;
}

function String_each_char(callback) { // @arg Function = null - callback(value:String, index:Integer)
                                      // @ret String|Array
    var array = this.split("");

    if (typeof callback === "function") {
        array.forEach(callback);
        return this;
    }
    return array;
}

function String_each_codepoint(callback) { // @arg Function = null - callback(value:Integer, index:Integer)
                                           // @ret String
    var array = String_codepoints.call(this);

    if (typeof callback === "function") {
        array.forEach(callback);
        return this;
    }
    return array;
}

function String_each_line(callback, // @arg Function = null - callback(value:Integer, index:Integer)
                          rs) {     // @arg String = "\n"
                                    // @ret String
    rs = rs === undefined ? "\n" : rs;
    var array = null;

    if (rs === "") { // paragraph mode
        array = this.chomp(rs).split("\r\n\r\n");
    } else if (rs === null || rs === undefined) {
        array = [this.valueOf()];
    } else {
        array = this.chomp(rs).split(rs);
    }
    if (typeof callback === "function") {
        array.forEach(callback);
        return this;
    }
    return array;
}

function String_end_with(/* ... */) { // @var_args String
                                      // @ret Boolean
    var source = this.valueOf();

    return [].slice.call(arguments).some(function(pattern) {
        var pos = source.length - pattern.length;

        if (source.lastIndexOf(pattern, pos) === pos) {
            return true;
        }
        return false;
    });
}

function String_eql(target) { // @arg String
                              // @ret Boolean
    return this.valueOf() === target;
}

function String_gsub(pattern,   // @arg RegExp
                     replace) { // @arg String|Function
                                // @ret String
//{@dev
    $valid($type(pattern, "RegExp"),          String_gsub, "pattern");
    $valid($type(replace, "String|Function"), String_gsub, "replace");
//}@dev

    if (replace.indexOf("\\&") >= 0) {
        throw new Error("unsupported replace character");
    }
    var rex = _createRegExp(pattern, ["g"]);

    //replace = replace.replace(/\\(\d)/g, "\$$1");
    replace = replace.replace(/\\(\d)/g, "$$1");

    return this.replace(rex, replace);
}

function String_hex() { // @ret Integer
    return parseInt(this.replace(/_/g, ""), 16) || 0;
}

function String_include(target) { // @arg String
                                  // @ret Boolean
    return this.indexOf(target) >= 0;
}

function String_insert(pos,      // @arg Integer
                       target) { // @arg String
                                 // @ret String
    return this.slice(0, pos) + target + this.slice(pos);
}

function String_size() { // @ret Integer
    return this.length;
}

function String_ljust(width,     // @arg Integer
                      padding) { // @arg String = " "
                                 // @ret String
//{@dev
    $valid($type(width, "Integer"),       String_ljust, "width");
    $valid($type(padding, "String|omit"), String_ljust, "padding");
//}@dev

    if (width <= this.length) {
        return this.valueOf();
    }
    return ( this + String_repeat.call(padding || " ", width) ).slice(0, width);
}

function String_lstrip() { // @ret String
    return this.replace(/^[ \t\r\n\f\v\0]+/, "");
}

function String_ord() { // @ret Integer
    if (!this.length) {
        throw new Error("String#ord() empty string");
    }
    return this.charCodeAt(0);
}

function String_partition(separator) { // @arg String
                                       // @ret StringArray - [left-side, separator, right-side]
    var pos = this.indexOf(separator);

    if (pos >= 0) {
        return [this.slice(0, pos), separator, this.slice(pos + separator.length)];
    }
    return [this.valueOf(), "", ""];
}

function String_reverse() { // @ret String
    return this.split("").reverse().join("");
}

function String_rjust(width,     // @arg Integer
                      padding) { // @arg String = " "
//{@dev
    $valid($type(width, "Integer"),       String_rjust, "width");
    $valid($type(padding, "String|omit"), String_rjust, "padding");
//}@dev

    if (width <= this.length) {
        return this.valueOf();
    }
    return ( String_repeat.call(padding || " ", width) + this ).slice(-width);
}

function String_rpartition(separator) { // @arg String
                                        // @ret StringArray - [left-side, separator, right-side]
    var pos = this.lastIndexOf(separator);

    if (pos >= 0) {
        return [this.slice(0, pos), separator, this.slice(pos + separator.length)];
    }
    return ["", "", this.valueOf()];
}

function String_rstrip() { // @ret String
    return this.replace(/[ \t\r\n\f\v\0]+$/, "");
}

/*
function String_sub(pattern,   // @arg RegExp
                    replace) { // @arg String|Function
                               // @ret String
//{@dev
    $valid($type(pattern, "RegExp"),          String_sub, "pattern");
    $valid($type(replace, "String|Function"), String_sub, "replace");
//}@dev

    return this.replace(pattern, replace);
}
 */

function String_scan(pattern,    // @arg RegExp|String
                     callback) { // @arg Function = null - callback(value:String/StringArray, index:Integer):void
                                 // @ret StringArray - [matchedString, ...] or [ [capturedString, ...], ... ]
//{@dev
    $valid($type(pattern, "RegExp|String"),  String_scan, "pattern");
    $valid($type(callback, "Function|omit"), String_scan, "callback");
//}@dev

    var rex = _createRegExp(pattern, ["g"]);
    var source = this.valueOf();
    var result = [];
    var match = null;
    var callbackIndex = 0;

    while ((match = rex.exec(source)) !== null) {
        var hasCapture = match.length > 1;

        if (hasCapture) {
            // RegExp and Capture pattern.
            // /(\d)/g.exec("123") -> result.push(["1", "1"])
            //                        callback(["1", "1"], callbackIndex)
            Array.prototype.push.apply( result, [match.slice(1)] );
            if (callback) {
                callback(match.slice(1), callbackIndex++);
            }
        } else {
            // /\d/g.exec("123") -> result.push("1")
            //                      callback("1", callbackIndex)
            result.push( match[0] );
            if (callback) {
                callback(match[0], callbackIndex++);
            }
        }
    }
    return result;
}

function String_squeeze(/* ... */) { // @var_args String
                                     // @ret String
    var targetString = "";
    var cs = new CharSet();

    if (arguments.length) {
        cs.set([].slice.call(arguments));
    }
    targetString = cs.get().liner;

    var result = [];
    var source = this.valueOf();
    var lastChar = "";

    for (var i = 0, iz = source.length; i < iz; ++i) {
        var curtChar = source[i];

        if (!targetString || targetString.indexOf(curtChar) >= 0) {
            if (lastChar === curtChar) {
                continue; // skip
            }
        }
        if (lastChar !== curtChar) {
            lastChar = curtChar;
        }
        result.push(curtChar);
    }

    return result.join("");
}

function String_start_with(/* ... */) { // @var_args String
                                        // @ret Boolean
    var source = this.valueOf();

    return [].slice.call(arguments).some(function(pattern) {
        if (source.indexOf(pattern) === 0) {
            return true;
        }
        return false;
    });
}

function String_strip() { // @ret String
    return this.replace(/^[ \t\r\n\f\v]+/, "").
                replace(/[ \t\r\n\f\v\0]+$/, "");
}

function String_swapcase() { // @ret String:
    return this.replace(/(?:([A-Z])|([a-z]))/g, function(_, up, low) {
        return up ? up.toLowerCase() : low.toUpperCase();
    });
}

function String_to_f() { // @ret Number - to float value
    return parseFloat(this.replace(/_/g, "")) || 0.0;
}

function String_to_i(radix) { // @arg Integer = 10 - radix, from 2 to 36
                              // @ret Number       - to integer value
    var value = this.valueOf();

    if (radix === undefined) {
        // nop
    } else if (radix === 0) {
        switch (value.slice(0, 2)) {
        case "0b": radix = 2;  break;
        case "0o": radix = 8;  break;
        case "0d": radix = 10; break;
        case "0x": radix = 16;
        }
        if (radix) {
            value = value.slice(2);
        }
        if (!radix && value[0] === "0") {
            radix = 8;
            value = value.slice(1);
        }
    } else {
        value = value.replace(/^(0b|0o|0d|0x)/, "");
    }
    return parseInt(value, radix || 10) || 0;
}

function Stirng_tr(pattern,   // @arg TRString
                   replace) { // @arg TRString
                              // @ret String
    var pat = new CharSet(pattern).get(); // { chars, liner }
    var rep = new CharSet(replace).get();

    var result = [];
    var chars = this.valueOf().split(""); // ["a", "b", ...]

    for (var i = 0, iz = chars.length; i < iz; ++i) {
        var c = chars[i];

        if (pat.chars[c]) { // matched
            var pos = pat.liner.indexOf(c);
            var r = rep.liner[pos] || rep.liner[rep.liner.length - 1] || "";

            result.push(r);
        } else {
            result.push(c);
        }
    }
    return result.join("");
}

function Stirng_tr_s(pattern,   // @arg TRString
                     replace) { // @arg TRString
                                // @ret String
    var pat = new CharSet(pattern).get(); // { chars, liner }
    var rep = new CharSet(replace).get();

    var result = [];
    var buffer = [];
    var chars = this.valueOf().split(""); // ["a", "b", ...]

    for (var i = 0, iz = chars.length; i < iz; ++i) {
        var c = chars[i];

        if (pat.chars[c]) { // matched
            var pos = pat.liner.indexOf(c);
            var r = rep.liner[pos] || rep.liner[rep.liner.length - 1] || "";

            buffer.push(r);
        } else {
            if (buffer.length) {
                result.push( String_squeeze.call( buffer.join("") ) );
                buffer.length = 0;
            }
            result.push(c);
        }
    }
    if (buffer.length) {
        result.push( String_squeeze.call( buffer.join("") ) );
    }
    return result.join("");
}

function _createRegExp(source,  // @arg RegExp|String
                       flags) { // @arg StringArray - ["g"]
    flags = flags || [];

    if (typeof source === "string") {
        return RegExp(source, flags.join(""));
    }
    if (source.global) {
        flags.push("g");
    }
    if (source.multiline) {
        flags.push("m");
    }
    if (source.ignoreCase) {
        flags.push("i");
    }
    return RegExp(source["source"], flags.join(""));
}

function CharSet(pattern) { // @arg String = ""
    this._chars = {};
    this._liner = [];

    this.set(pattern);
}
CharSet.prototype = {
    constructor:    CharSet,
    get:            CharSet_get,
    set:            CharSet_set,
    add:            CharSet_add,
    addRange:       CharSet_addRange,
    remove:         CharSet_remove,
    removeRange:    CharSet_removeRange
};

function CharSet_get() { // @ret Object
    return { chars: this._chars, liner: this._liner.join("") };
}

function CharSet_set(patterns) { // @arg StringArray|String
    function _set(that, pattern) {
        if (pattern) {
            if (pattern[0] === "^") {
                pattern = pattern.slice(1);

                if (/\w-\w/.test(pattern)) {
                    that.removeRange(pattern);
                } else {
                    that.remove(pattern);
                }
            } else {
                if (/\w-\w/.test(pattern)) {
                    that.addRange(pattern);
                } else {
                    that.add(pattern);
                }
            }
        }
    }

    var that = this;

    if (Array.isArray(patterns)) {
        patterns.forEach(function(value) {
            _set(that, value);
        });
    } else if (typeof patterns === "string") {
        _set(this, patterns);
    }
    return this;
}

function CharSet_add(chars) { // @arg String - "12345"
    var ary = chars.split("");

    for (var i = 0, iz = ary.length; i < iz; ++i) {
        var c = ary[i];

        this._chars[c] = 1;
        if (this._liner.indexOf(c) < 0) {
            this._liner.push(c);
        }
    }
    return this;
}

function CharSet_addRange(range) { // @arg String - "0-9"
    var first = range.charCodeAt(0);
    var last  = range.charCodeAt(2);

    for (; first <= last; ++first) {
        var c = String.fromCharCode(first);

        this._chars[c] = 1;
        if (this._liner.indexOf(c) < 0) {
            this._liner.push(c);
        }
    }
    return this;
}

function CharSet_remove(chars) { // @arg String - "12345"
    var ary = chars.split("");

    for (var i = 0, iz = ary.length; i < iz; ++i) {
        var c = ary[i];

        delete this._chars[c];

        var pos = this._liner.indexOf(c);
        if (pos >= 0) {
            this._liner.splice(pos, 1);
        }
    }
    return this;
}

function CharSet_removeRange(range) { // @arg String - "0-9"
    var first = range.charCodeAt(0);
    var last  = range.charCodeAt(2);

    for (; first <= last; ++first) {
        var c = String.fromCharCode(first);

        delete this._chars[c];

        var pos = this._liner.indexOf(c);
        if (pos >= 0) {
            this._liner.splice(pos, 1);
        }
    }
    return this;
}

// --- Number ----------------------------------------------
function Number_upto(max,        // @arg Integer
                     callback) { // @arg Function - callback(num:Integer, index:Integer array:Array)
//{@dev
    $valid($type(max, "Integer"),       Number_upto, "max");
    $valid($type(callback, "Function"), Number_upto, "callback");
//}@dev

  //this.to(max).forEach(callback);
    _Number_to(this, max).forEach(callback);
}

function Number_step(limit,      // @arg Integer
                     step,       // @arg Integer
                     callback) { // @arg Function - callback(num:Integer, index:Integer, array:Array)
//{@dev
    $valid($type(limit, "Integer"),     Number_step, "limit");
    $valid($type(step, "Integer"),      Number_step, "step");
    $valid($type(callback, "Function"), Number_step, "callback");
//}@dev

//  +this > limit ? this.to(limit, -step).each(callback)  // Ruby spec
//                : this.to(limit,  step).each(callback);

    if (+this > limit) {
        _Number_to(this, limit, -step).forEach(callback); // Ruby spec
    } else {
        _Number_to(this, limit,  step).forEach(callback);
    }
}

function _Number_to(that,           // @arg this
                    end,            // @arg Integer              - end number
                    filterOrStep) { // @arg Function|Integer = 1 - filter function or step number
                                    // @ret Array                - [begin, ... end]
                                    // @this begin number
                                    // @desc create number array
                                    // @throw Error("BAD_ARG")
    var rv = [], begin = that | 0, reverse = begin > end,
        i  = (reverse ? end : begin) | 0,
        iz = (reverse ? begin : end) | 0,
        type = typeof filterOrStep,
        step = 1;

    if (type === "function") {
        for (; i <= iz; ++i) {
            if (filterOrStep(i)) {
                rv.push(i);
            }
        }
    } else {
        if (type === "number") {
            step = filterOrStep | 0;
        }
        if (step < 1 || end >= 0x7FFFFFFF) {
            throw new Error("invalid Number#to(,step)");
        }
        for (; i <= iz; i += step) {
            rv.push(i);
        }
    }
    return reverse ? rv.reverse() : rv;
}

function Number_to_s(radix) { // @arg Integer = 10 - 2 - 36
//{@dev
    $valid($type(radix, "Integer|omit"), Number_to_s, "radix");
//}@dev

    return this.toString(radix || 10);
}

// --- copy from uupaa.extend.js ---
function Extend_tree(base, tree) {
    for (var klass in tree) { // "Object", "Array", "String", ...
        if ( !(klass in base) ) {
            base[klass] = {};
        }
        _extend(base[klass], tree[klass]);
    }
    function _extend(base, object) {
        for (var key in object) { // "isArray", "prototype", ...
            if (key === "prototype") {
                if ( !(key in base) ) { // base["prototype"] = {}
                    base[key] = {};
                }
                for (var method in object[key]) {
                    _define(base[key], method, object[key][method]);
                }
            } else {
                _define(base, key, object[key]);
            }
        }
    }
    function _define(base, key, value) {
        var defineProperty = Object["defineProperty"] || polyfill_defineProperty;

        if ( !(key in base) ) {
            defineProperty(base, key, {
                "configurable": true, // false is immutable
                "enumerable": false,  // false is invisible(for in loop)
                "writable": true,     // false is read-only
                "value": value
            });
        }
    }
    //{@polyfill_defineProperty
    function polyfill_defineProperty(object, prop, descriptor) {
        if (object["nodeType"]) {
            if (Object["defineProperty"] && !Object["defineProperties"]) { // [IE8]
                return Object["defineProperty"](object, prop, descriptor); // call native api
            }
        }

        // data descriptor.
        if ("value" in descriptor) {
            object[prop] = descriptor["value"];
        }

        // accessor descriptor.
        if (descriptor["get"] && object["__defineGetter__"]) {
            object["__defineGetter__"](prop, descriptor["get"]);
        }
        if (descriptor["set"] && object["__defineSetter__"]) {
            object["__defineSetter__"](prop, descriptor["set"]);
        }
        return object;
    }
    //}@polyfill_defineProperty
}

// --- validate / assertions -------------------------------
//{@dev
function $valid(val, fn, hint) { if (global["$valid"]) { global["$valid"](val, fn, hint); } }
function $type(obj, type) { return global["$valid"] ? global["$valid"].type(obj, type) : true; }
//function $keys(obj, str) { return global["$valid"] ? global["$valid"].keys(obj, str) : true; }
//function $some(val, str, ignore) { return global["$valid"] ? global["$valid"].some(val, str, ignore) : true; }
//function $args(fn, args) { if (global["$valid"]) { global["$valid"].args(fn, args); } }
//}@dev

// --- exports ---------------------------------------------
if ("process" in global) {
    module["exports"] = Ruby;
}
global["Ruby" in global ? "Ruby_" : "Ruby"] = Ruby; // switch module. http://git.io/Minify

})((this || 0).self || global); // WebModule idiom. http://git.io/WebModule


text:
package com.technumen.models;

import com.technumen.constants.TimesheetConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Data
@Entity
@ToString(exclude = {"empPassword", "empPassword2"})
@NoArgsConstructor
@AllArgsConstructor
public class Employee implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "employee_id", unique = true, nullable = false)
    private long employeeId; //Unique Id and the primary key for Employee table.
    @Column(unique = true, nullable = false)
    private String employeeEmailId; //Primary EmailId/User Name of the user.
    @Column(nullable = false)
    private String empPassword; //This will be stored as a Hash value.
    @Transient
    private String empPassword2; //Just a confirmation field used for validation. Not saved/updated.
    private String assignedEmployeeId; //TechNumen assigned Identifier.
    @Column(nullable = false)
    private String employeeFirstName;
    @Column(nullable = false)
    private String employeeLastName;
    @Transient
    private String employeeFullName;
    private String employeeMiddleName;
    private String employeeTitle; //Description of the Employee Title.
    private int employeeRoleId; //Foreign Reference for Employee_Roles table.
    private String employeeRoleDesc;
    private String employeePhone;
    private String employeePhoneExt;
    private String clientName; //Name of the Client.
    private String clientAddress;//Address of the Client.
    private String clientCity;
    private String clientState;
    private String clientZip;
    private long departmentId;//Primary key of Department table.
    private String departmentName;
    @DateTimeFormat(pattern = "MM/dd/yyyy")
    @Temporal(TemporalType.DATE)
    private Date employeeStartDate; //Account Created Date.
    private String accountStatusFlag; //Flag to check if the account is ACTIVE or INACTIVE.
    @DateTimeFormat(pattern = "MM/dd/yyyy")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateInactivated;
    private String nameUserInactivated; // Who inactivated this user
    @DateTimeFormat(pattern = "MM/dd/yyyy")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreated;
    private String nameUserCreated;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
    private Set<Timesheet> timesheetRecords = new HashSet<>(0);

    public String getEmployeeFullName() {
        return (StringUtils.isNotBlank(this.employeeFirstName) ? this.employeeFirstName : "") +
                " " + (StringUtils.isNotBlank(this.employeeLastName) ? this.employeeLastName : "");
    }

    public boolean isEmployeeRole() {
        return this.employeeRoleId == TimesheetConstants.USER_ROLE_EMPLOYEE_ID;
    }

    public boolean isSupervisorRole() {
        return this.employeeRoleId == TimesheetConstants.USER_ROLE_SUPERVISOR_ID;
    }

    public boolean isAdminRole() {
        return this.employeeRoleId == TimesheetConstants.USER_ROLE_ADMIN_ID;
    }

}

redpajama_set_name: RedPajamaGithub


text:
package com.ibm.streamsx.topology.generator.spl;

import static com.ibm.streamsx.topology.builder.BVirtualMarker.END_LOW_LATENCY;
import static com.ibm.streamsx.topology.builder.BVirtualMarker.ISOLATE;
import static com.ibm.streamsx.topology.builder.BVirtualMarker.LOW_LATENCY;
import static com.ibm.streamsx.topology.generator.operator.OpProperties.CONFIG;
import static com.ibm.streamsx.topology.generator.operator.OpProperties.PLACEMENT;
import static com.ibm.streamsx.topology.generator.operator.OpProperties.PLACEMENT_COLOCATE_KEY;
import static com.ibm.streamsx.topology.generator.operator.OpProperties.PLACEMENT_COLOCATE_TAGS;
import static com.ibm.streamsx.topology.generator.operator.OpProperties.PLACEMENT_ISOLATE_REGION_ID;
import static com.ibm.streamsx.topology.generator.operator.OpProperties.addColocationTag;
import static com.ibm.streamsx.topology.generator.spl.GraphUtilities.findOperatorByKind;
import static com.ibm.streamsx.topology.generator.spl.GraphUtilities.getDownstream;
import static com.ibm.streamsx.topology.generator.spl.GraphUtilities.getUpstream;
import static com.ibm.streamsx.topology.generator.spl.GraphUtilities.kind;
import static com.ibm.streamsx.topology.generator.spl.GraphUtilities.operators;
import static com.ibm.streamsx.topology.internal.graph.GraphKeys.CFG_COLOCATE_IDS;
import static com.ibm.streamsx.topology.internal.graph.GraphKeys.CFG_COLOCATE_TAG_MAPPING;
import static com.ibm.streamsx.topology.internal.graph.GraphKeys.CFG_HAS_ISOLATE;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.jstring;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.object;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.objectCreate;

import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.ibm.streamsx.topology.builder.BVirtualMarker;
import com.ibm.streamsx.topology.function.Consumer;
import com.ibm.streamsx.topology.generator.spl.GraphUtilities.Direction;
import com.ibm.streamsx.topology.generator.spl.GraphUtilities.VisitController;
import com.ibm.streamsx.topology.internal.gson.GsonUtilities;

class PEPlacement {

    private final SPLGenerator generator;
    private final JsonObject graph;

    private int isolateRegionCount;
    private int lowLatencyRegionCount;

    PEPlacement(SPLGenerator generator, JsonObject graph) {
        this.generator = generator;
        this.graph = graph;
    }

    private void setIsolateRegionId(JsonObject op, String isolationRegionId) {

        JsonObject placement = objectCreate(op, CONFIG, PLACEMENT);

        // As we create regions up and downstream of isolate
        // operators the same region can be seen twice,
        // e.g x -> isolate1 -> y -> isolate2 -> z
        // y is seen twice, once from isolate1 and once from isolate2
        if (jstring(placement, PLACEMENT_ISOLATE_REGION_ID) != null)
                return;

        placement.addProperty(PLACEMENT_ISOLATE_REGION_ID, isolationRegionId);
    }

    /**
     * Assign a region isolate identifier to all operators
     * in an isolate region. From the starts (which are immediately
     * up/downstream of an isolate set the isolate region id for
     * all reachable operators until another isolate or the edge is hit.
     *
     * @param starts Set of operators upstream or downstream of an isolate marker.
     */
    private void assignIsolateRegionIds(Set<JsonObject> starts) {

        final String isolationRegionId = newIsolateRegionId();

        Set<BVirtualMarker> boundaries = EnumSet.of(BVirtualMarker.ISOLATE);

        GraphUtilities.visitOnce(starts, boundaries, graph,
                op -> setIsolateRegionId(op, isolationRegionId));
    }

    /**
     * Determine whether any isolated region is ever joined with its parent.
     * I.E:
     *
     *

     *
     *       |---$Isolate---|
     *   ----|              |----
     *       |--------------|
     *
     *

     *
     * @param isolate
     *            An $Isolate$ operator in the graph
     * @return a boolean which is false if the the Isolated region is later
     *         merged with its parent.
     */
    @SuppressWarnings("serial")
    private void checkValidColocationRegion(JsonObject isolate) {
        final Set<JsonObject> isolateChildren = getDownstream(isolate, graph);
        Set<JsonObject> isoParents = getUpstream(isolate, graph);

        assertNotIsolated(isoParents);

        Set<BVirtualMarker> boundaries = EnumSet.of(BVirtualMarker.ISOLATE);

        GraphUtilities.visitOnce(isoParents, boundaries, graph,
                new Consumer<JsonObject>() {
                    @Override
                    public void accept(JsonObject op) {
                        if (isolateChildren.contains(op)) {
                            throw new IllegalStateException(
                                    "Invalid isolation "
                                            + "configuration. An isolated region is joined with a non-"
                                            + "isolated region.");
                        }
                    }
                });
    }

    void tagIsolationRegions() {
        // Check whether graph is valid for colocations
        List<JsonObject> isolateOperators = findOperatorByKind(ISOLATE, graph);

        if (!isolateOperators.isEmpty())
            graph.getAsJsonObject("config").addProperty(CFG_HAS_ISOLATE, true);

        for (JsonObject jso : isolateOperators) {
            checkValidColocationRegion(jso);
        }

        // Assign isolation regions their partition colocations
        // by working upstream from the the isolate marker
        // and then downstream to separate the regions with
        // different isolate region identifiers.
        for (JsonObject isolate : isolateOperators) {
            assignIsolateRegionIds(getUpstream(isolate, graph));
            assignIsolateRegionIds(getDownstream(isolate, graph));
        }

        GraphUtilities.removeOperators(isolateOperators, graph);
    }

    private String newIsolateRegionId() {
        return "__spl_isolateRegionId" + isolateRegionCount++;
    }

    private static void assertNotIsolated(Collection<JsonObject> jsos) {
        for (JsonObject jso : jsos) {
            if (BVirtualMarker.ISOLATE.isThis(jstring(jso, "kind"))) {
                throw new IllegalStateException(
                        "Cannot put \"isolate\" regions immediately"
                                + " adjacent to each other. E.g -- .isolate().isolate()");
            }
        }
    }

    /**
     * We colocate between upon LOW_LATENCY and END_LOW_LATENCY markers.
     * Given they can be nested we take the simple approch
     * of work down from LOW_LATENCY and then up from LOW_LATENCY.
     *
     * In addition assignLowLatency colocates operators immediately upstream
     * of LOW_LATENCY as defined by the lowLatency() methods.
     */
    void tagLowLatencyRegions() {
        List<JsonObject> lowLatencyStartOperators = GraphUtilities
                .findOperatorByKind(LOW_LATENCY, graph);

        if (lowLatencyStartOperators.isEmpty())
            return;

        // Assign isolation regions a colocation tag
        for (JsonObject llStart : lowLatencyStartOperators) {
            assignLowLatency(llStart, Direction.DOWNSTREAM);
        }

        List<JsonObject> lowLatencyEndOperators = GraphUtilities
                .findOperatorByKind(END_LOW_LATENCY, graph);

        if (lowLatencyEndOperators.isEmpty())
            return;

        for (JsonObject llStart : lowLatencyEndOperators)
            assignLowLatency(llStart, Direction.UPSTREAM);
    }

    private void assignLowLatency(JsonObject llStart, Direction direction) {

        final JsonPrimitive lowLatencyTag =
            new JsonPrimitive("__spl_lowLatency$" + lowLatencyRegionCount++);

        Set<JsonObject> llStartChildren;
        Set<BVirtualMarker> boundaries;

        if (direction == Direction.DOWNSTREAM) {
            llStartChildren = getDownstream(llStart, graph);
            boundaries = EnumSet.of(END_LOW_LATENCY);
        } else {
            llStartChildren = getUpstream(llStart, graph);
            boundaries = EnumSet.of(LOW_LATENCY);
        }

        GraphUtilities.visitOnce(
                new VisitController(direction, boundaries),
                llStartChildren, graph,
                op -> addColocationTag(op, lowLatencyTag));

        if (direction == Direction.DOWNSTREAM) {
            // Low latency merges with upstream.
            for (JsonObject op : getUpstream(llStart, graph)) {
                String kind = kind(op);
                if (BVirtualMarker.PARALLEL.isThis(kind))
                    continue;
                if (BVirtualMarker.END_PARALLEL.isThis(kind))
                    continue;
                if (BVirtualMarker.UNION.isThis(kind))
                    continue;
                addColocationTag(op, lowLatencyTag);
            }
        }
    }

    /**
     * Goes through the graph and looks to merge all colocation tags to a
     * single value for the set of colocated operators.
     * This resolves the multiple potential colocate tags
     * through explicit colocation, isolation and low-latency.
     * Each operator with a colocate directive is left with
     * a single key to a map of tags in the graph config.
     * The value in the map is the actual tag used in SPL
     * placement config for the operator.
     */
    void resolveColocationTags() {

        JsonObject tagMaps = objectCreate(graph, CONFIG, CFG_COLOCATE_TAG_MAPPING);

        operators(graph, op -> {
            JsonObject placement = object(op, CONFIG, PLACEMENT);
            if (placement == null)
                return;

            JsonArray tags = GsonUtilities.array(placement, PLACEMENT_COLOCATE_TAGS);
            if (tags == null)
                return;

            Set<String> sameTags = new HashSet<>();
            for (JsonElement e : tags)
                sameTags.add(e.getAsString());

            // Find if any of these tags are already mapped.
            Set<String> existingTags = new HashSet<>();
            for (String tag : sameTags) {
                if (tagMaps.has(tag)) {
                    existingTags.add(jstring(tagMaps, tag));
                }
            }
            final String singleTag;
            if (existingTags.isEmpty()) {
                // pick any one of the existing ones
                // none of which have been seen yet.
                singleTag = sameTags.iterator().next();
            } else if (existingTags.size() == 1) {
                // single existing tag, use it
                singleTag = existingTags.iterator().next();
            } else {
                // Need to merge tags
                // e.g. A->A, B->B were separate but now we need to merge A,B
                // pick one tag as single
                singleTag = existingTags.iterator().next();
                sameTags.addAll(existingTags);
            }

            for (String tag : sameTags)
                tagMaps.addProperty(tag, singleTag);

            // finally add the single tag key to the operator
            placement.addProperty(PLACEMENT_COLOCATE_KEY, singleTag);
        });

        object(graph, CONFIG).add(CFG_COLOCATE_TAG_MAPPING, tagMaps);

        setupForRelativizeColocateTags();
    }

    /**
     * Create a Json object containing a count (initially 0)
     * for each colocation tag. The count will be the number
     * of unique composites it is used in.
     */
    void setupForRelativizeColocateTags() {
         JsonObject tagMaps = object(graph, CONFIG, CFG_COLOCATE_TAG_MAPPING);
         JsonObject colocateIds = objectCreate(graph, CONFIG, CFG_COLOCATE_IDS);

         for (Entry<String, JsonElement> entry : tagMaps.entrySet()) {
             JsonObject idInfo = new JsonObject();
             idInfo.addProperty("parallelUse", 0);
             idInfo.addProperty("lowLatencyUse", 0);
             colocateIds.add(entry.getValue().getAsString(), idInfo);
         }
    }

    /**
     * Bump the count for each colocation id used in a composite.
     * If a colocation tag is only used in a single parallel composite
     * then its actually id needs to be channel based and relative to the
     * composite instance name, otherwise it's absolute.
     */
    void compositeColocateIdUse(JsonObject compositeDefinition) {
        final boolean lowLatency = jboolean(compositeDefinition, "lowLatencyComposite");
        if (lowLatency)
            return;

        Set<String> usedColocateKeys = new HashSet<>();
        operators(compositeDefinition, op -> {
            JsonObject placement = object(op, CONFIG, PLACEMENT);
            if (placement == null)
                return;

            String colocateKey = jstring(placement, PLACEMENT_COLOCATE_KEY);
            if (colocateKey != null)
                usedColocateKeys.add(colocateKey);
        });

        if (usedColocateKeys.isEmpty())
            return;

        Set<String> usedColocateIds = new HashSet<>();
        JsonObject tagMaps = object(graph, CONFIG, CFG_COLOCATE_TAG_MAPPING);
        for (String key : usedColocateKeys)
            usedColocateIds.add(jstring(tagMaps, key));

        final boolean parallel = jboolean(compositeDefinition, "parallelComposite");
        final boolean main = jboolean(compositeDefinition, "__spl_mainComposite");

        JsonObject colocateIds = object(graph, CONFIG, CFG_COLOCATE_IDS);
        for (String id : usedColocateIds) {
            JsonObject idInfo = colocateIds.getAsJsonObject(id);

            if (parallel)
                idInfo.addProperty("parallelUse", idInfo.get("parallelUse").getAsInt()+1);
            if (main)
                idInfo.addProperty("main", main);
        }
    }
}


text:
pwdgen [![Build Status](https://travis-ci.org/blacktaxi/pwdgen-elm.svg)](https://travis-ci.org/blacktaxi/pwdgen-elm)
======

An [Elm](http://elm-lang.org/) "hello world" web app: generate a password from a template.

For example, `{adj}{noun}\d\d` is turned into something like `uncriticalovercapitalisation21`.


text:
namespace mjolnir
{

// XXX Topology class manages the connections between beads.
//
// NOTE: connection kind "bond" is used to determine the molecules.
//       Other kind of connections also can be defined (and overlayed
//       if nessesary). In that case, you can traverse the graph by using
//       your connection kind also, but it will not be considered when
//       it detemines "molecule"s.
//
// Most of the time, "molecule" defined by this class is equivalent to "chain".
// But calling "molecule" as "chain" is misreading because there can be a
// circular molecule or tiny molecule that only has a few number of beads.
// For example, lipid membrane is composed of a large number of lipid molecules
// and people useally does not call each lipid as "chain".
//
// Generally, traversing graph takes time rather than finding value from
// contiguous container. So there are ExclusionList class that stores the
// information for NeighborLists (usually, nonlocal potentials are ignored if
// the two particles are connected by covarent bond). While the simulation,
// ExclusionLists would be called many time, but Topology would only be called
// to construct ExclusionList at the beginning ofjthe simulation.
//
// Since the members of this class are rarely called, in most cases, the speed
// of these methods does not affect to the overall efficiency.class Topology
class Topology
{
  public:

    using molecule_id_type     = std::size_t;
    using group_id_type        = std::string;
    using name_type            = std::string;
    using connection_kind_type = std::string;
    using edge_type            = std::pair<std::size_t, connection_kind_type>;

    static constexpr molecule_id_type uninitialized() noexcept
    {
        return std::numeric_limits<molecule_id_type>::max();
    }

    struct node
    {
        std::size_t molecule_id;
        std::vector<edge_type> adjacents;

        bool operator==(const node& other) const
        {
            return molecule_id == other.molecule_id &&
                   adjacents   == other.adjacents;
        }
        bool operator!=(const node& other) const
        {
            return !(*this == other);
        }
    };

  public:

    Topology()  = default;
    ~Topology() = default;
    Topology(const Topology&) = default;
    Topology(Topology&&)      = default;
    Topology& operator=(const Topology&) = default;
    Topology& operator=(Topology&&)      = default;

    explicit Topology(const std::size_t N)
        : num_molecules_(1), nodes_(N, node{uninitialized(), {}})
    {}

    bool        empty() const noexcept {return nodes_.empty();}
    std::size_t size()  const noexcept {return nodes_.size();}
    void clear()
    {
        std::fill(nodes_.begin(), nodes_.end(), node{uninitialized(), {}});
        return ;
    }

    molecule_id_type  molecule_of(const std::size_t i) const
    {return nodes_.at(i).molecule_id;}
    molecule_id_type& molecule_of(const std::size_t i)
    {return nodes_.at(i).molecule_id;}

    molecule_id_type  molecule_of(const std::size_t i, const std::nothrow_t&) const
    {return nodes_[i].molecule_id;}
    molecule_id_type& molecule_of(const std::size_t i, const std::nothrow_t&)
    {return nodes_[i].molecule_id;}

    void add_connection  (const std::size_t i, const std::size_t j,
                          const connection_kind_type& kind);
    void erase_connection(const std::size_t i, const std::size_t j,
                          const connection_kind_type& kind);
    bool has_connection  (const std::size_t i, const std::size_t j,
                          const connection_kind_type& kind);
    // has_connection returns true if i == j.

    std::vector<std::size_t>
    list_adjacent_within(const std::size_t node_idx, const std::size_t dist,
                         const connection_kind_type& kind) const;

    std::vector<connection_kind_type>
    list_connections_between(const std::size_t i, const std::size_t j) const;

    //! reset molecule_id of all the particles
    void construct_molecules();
    void resize(const std::size_t N) {nodes_.resize(N); return;}

    std::size_t number_of_molecules() const noexcept {return this->num_molecules_;}

    bool operator==(const Topology& other) const
    {
        return this->nodes_ == other.nodes_ &&
               this->num_molecules_ == other.num_molecules_;
    }
    bool operator!=(const Topology& other) const
    {
        return !(*this == other);
    }

  private:

    void list_adjacent_within(
        const std::size_t node_idx, const std::size_t dist,
        const connection_kind_type& kind, std::vector<std::size_t>& out) const
    {
        if(dist == 0)
        {
            out.push_back(node_idx);
            return;
        }

        for(auto edge : this->nodes_.at(node_idx).adjacents)
        {
            if(edge.second != kind)
            {
                continue;
            }
            out.push_back(edge.first);
            this->list_adjacent_within(edge.first, dist-1, kind, out);
        }
        return;
    }

  private:
    // each node corresponds to the particle having the same idx in a system.
    std::size_t       num_molecules_;
    std::vector<node> nodes_;
};

inline void Topology::add_connection(
        const std::size_t i, const std::size_t j,
        const connection_kind_type& kind)
{
    if(nodes_.size() <= std::max(i, j))
    {
        throw_exception<std::out_of_range>(
            "mjolnir::Topology::add_connection: size of nodes = ",
            nodes_.size(), ", i = ", i, ", j = ", j);
    }

    {
        const edge_type edge = std::make_pair(j, kind);

        auto& adjs = nodes_[i].adjacents;
        if(std::find(adjs.cbegin(), adjs.cend(), edge) == adjs.cend())
        {
            adjs.push_back(edge);
        }
    }
    {
        const edge_type edge = std::make_pair(i, kind);

        auto& adjs = nodes_[j].adjacents;
        if(std::find(adjs.cbegin(), adjs.cend(), edge) == adjs.cend())
        {
            adjs.push_back(edge);
        }
    }
    return ;
}

inline void Topology::erase_connection(
        const std::size_t i, const std::size_t j,
        const connection_kind_type& kind)
{
    if(nodes_.size() <= std::max(i, j))
    {
        throw_exception<std::out_of_range>(
            "mjolnir::Topology::erase_connection: size of nodes = ",
            nodes_.size(), ", i = ", i, ", j = ", j);
    }

    {
        auto& adjs = nodes_[i].adjacents;
        const auto found = std::find(
                adjs.begin(), adjs.end(), std::make_pair(j, kind));
        if(found != adjs.end())
        {
            adjs.erase(found);
        }
    }
    {
        auto& adjs = nodes_[j].adjacents;
        const auto found = std::find(
                adjs.begin(), adjs.end(), std::make_pair(i, kind));
        if(found != adjs.end())
        {
            adjs.erase(found);
        }
    }
    return ;
}

inline bool Topology::has_connection(
        const std::size_t i, const std::size_t j,
        const connection_kind_type& kind)
{
    if(nodes_.size() <= std::max(i, j))
    {
        throw_exception<std::out_of_range>(
            "mjolnir::Topology::has_connection: size of nodes = ",
            nodes_.size(), ", i = ", i, ", j = ", j);
    }
    if(i == j) {return true;} // XXX

    const auto& adjs = nodes_[i].adjacents;
    const auto found =
        std::find(adjs.cbegin(), adjs.cend(), std::make_pair(j, kind));
    return found != adjs.cend();
}

inline std::vector<std::size_t>
Topology::list_adjacent_within(
        const std::size_t node_idx, const std::size_t dist,
        const connection_kind_type& kind) const
{
    std::vector<std::size_t> retval = {node_idx};
    if(dist == 0) {return retval;}

    for(auto edge : this->nodes_.at(node_idx).adjacents)
    {
        if(edge.second != kind)
        {
            continue;
        }
        retval.push_back(edge.first);
        this->list_adjacent_within(edge.first, dist - 1, kind, retval);
    }
    std::sort(retval.begin(), retval.end());
    const auto new_end = std::unique(retval.begin(), retval.end());
    retval.erase(new_end, retval.end());
    return retval;
}

inline std::vector<typename Topology::connection_kind_type>
Topology::list_connections_between(const std::size_t i, const std::size_t j) const
{
    std::vector<connection_kind_type> connections;
    for(const auto& edge : this->nodes_.at(i).adjacents)
    {
        if(edge.first == j)
        {
            connections.push_back(edge.second);
        }
    }
    return connections;
}

inline void
Topology::construct_molecules()
{
    if(this->nodes_.empty()){return;}
    // reset molecule-ids
    for(auto& node : nodes_)
    {
        node.molecule_id = uninitialized();
    }

    molecule_id_type next_molecule_id = 0;
    for(auto& node : nodes_)
    {
        // look all the adjacents and update node by their molecule id
        for(const auto& edge : node.adjacents)
        {
            // ignore all the edges that are not bonds
            if(edge.second != "bond") {continue;}

            // if an adjacent that is connected by a "bond",
            // it should be in the same molecule.
            const auto& adj = this->nodes_.at(edge.first);
            if(adj.molecule_id != uninitialized())
            {
                node.molecule_id = adj.molecule_id;
                break;
            }
        }
        // if the molecule_id of this node is still not initialized, that means
        // this node belongs to a new molecule.
        if(node.molecule_id == uninitialized())
        {
            node.molecule_id = next_molecule_id++;
        }
    }
    this->num_molecules_ = next_molecule_id;
    return;
}

} // mjolnir
#endif// MJOLNIR_STRUCTURE_TOPOLOGY_H


text:
require 'spec_helper'

describe 'git::gitosis' do

  context 'defaults' do
    it { should contain_package('gitosis') }
    it { should contain_class('git') }
    it { should create_class('git::gitosis') }
  end
end


text:
package api

import (
	"encoding/json"
	"fmt"
	"net/url"
	"regexp"

	"github.com/circonus-labs/circonus-gometrics/api/config"
)

// CheckBundleMetric individual metric configuration
type CheckBundleMetric struct {
	Name   string   `json:"name"`             // string
	Result *string  `json:"result,omitempty"` // string or null, NOTE not settable - return/information value only
	Status string   `json:"status,omitempty"` // string
	Tags   []string `json:"tags"`             // [] len >= 0
	Type   string   `json:"type"`             // string
	Units  *string  `json:"units,omitempty"`  // string or null

}

// CheckBundleConfig contains the check type specific configuration settings
// as k/v pairs (see https://login.circonus.com/resources/api/calls/check_bundle
// for the specific settings available for each distinct check type)
type CheckBundleConfig map[config.Key]string

// CheckBundle defines a check bundle. See https://login.circonus.com/resources/api/calls/check_bundle for more information.
type CheckBundle struct {
	Brokers            []string            `json:"brokers"`                            // [] len >= 0
	Checks             []string            `json:"_checks,omitempty"`                  // [] len >= 0
	CheckUUIDs         []string            `json:"_check_uuids,omitempty"`             // [] len >= 0
	CID                string              `json:"_cid,omitempty"`                     // string
	Config             CheckBundleConfig   `json:"config"`                             // NOTE contents of config are check type specific, map len >= 0
	Created            uint                `json:"_created,omitempty"`                 // uint
	DisplayName        string              `json:"display_name"`                       // string
	LastModifedBy      string              `json:"_last_modifed_by,omitempty"`         // string
	LastModified       uint                `json:"_last_modified,omitempty"`           // uint
	MetricLimit        int                 `json:"metric_limit,omitempty"`             // int
	Metrics            []CheckBundleMetric `json:"metrics"`                            // [] >= 0
	Notes              *string             `json:"notes,omitempty"`                    // string or null
	Period             uint                `json:"period,omitempty"`                   // uint
	ReverseConnectURLs []string            `json:"_reverse_connection_urls,omitempty"` // [] len >= 0
	Status             string              `json:"status,omitempty"`                   // string
	Tags               []string            `json:"tags,omitempty"`                     // [] len >= 0
	Target             string              `json:"target"`                             // string
	Timeout            float32             `json:"timeout,omitempty"`                  // float32
	Type               string              `json:"type"`                               // string
}

// NewCheckBundle returns new CheckBundle (with defaults, if applicable)
func NewCheckBundle() *CheckBundle {
	return &CheckBundle{
		Config:      make(CheckBundleConfig, config.DefaultConfigOptionsSize),
		MetricLimit: config.DefaultCheckBundleMetricLimit,
		Period:      config.DefaultCheckBundlePeriod,
		Timeout:     config.DefaultCheckBundleTimeout,
		Status:      config.DefaultCheckBundleStatus,
	}
}

// FetchCheckBundle retrieves check bundle with passed cid.
func (a *API) FetchCheckBundle(cid CIDType) (*CheckBundle, error) {
	if cid == nil || *cid == "" {
		return nil, fmt.Errorf("Invalid check bundle CID [none]")
	}

	bundleCID := string(*cid)

	matched, err := regexp.MatchString(config.CheckBundleCIDRegex, bundleCID)
	if err != nil {
		return nil, err
	}
	if !matched {
		return nil, fmt.Errorf("Invalid check bundle CID [%v]", bundleCID)
	}

	result, err := a.Get(bundleCID)
	if err != nil {
		return nil, err
	}

	if a.Debug {
		a.Log.Printf("[DEBUG] fetch check bundle, received JSON: %s", string(result))
	}

	checkBundle := &CheckBundle{}
	if err := json.Unmarshal(result, checkBundle); err != nil {
		return nil, err
	}

	return checkBundle, nil
}

// FetchCheckBundles retrieves all check bundles available to the API Token.
func (a *API) FetchCheckBundles() (*[]CheckBundle, error) {
	result, err := a.Get(config.CheckBundlePrefix)
	if err != nil {
		return nil, err
	}

	var checkBundles []CheckBundle
	if err := json.Unmarshal(result, &checkBundles); err != nil {
		return nil, err
	}

	return &checkBundles, nil
}

// UpdateCheckBundle updates passed check bundle.
func (a *API) UpdateCheckBundle(cfg *CheckBundle) (*CheckBundle, error) {
	if cfg == nil {
		return nil, fmt.Errorf("Invalid check bundle config [nil]")
	}

	bundleCID := string(cfg.CID)

	matched, err := regexp.MatchString(config.CheckBundleCIDRegex, bundleCID)
	if err != nil {
		return nil, err
	}
	if !matched {
		return nil, fmt.Errorf("Invalid check bundle CID [%s]", bundleCID)
	}

	jsonCfg, err := json.Marshal(cfg)
	if err != nil {
		return nil, err
	}

	if a.Debug {
		a.Log.Printf("[DEBUG] update check bundle, sending JSON: %s", string(jsonCfg))
	}

	result, err := a.Put(bundleCID, jsonCfg)
	if err != nil {
		return nil, err
	}

	checkBundle := &CheckBundle{}
	if err := json.Unmarshal(result, checkBundle); err != nil {
		return nil, err
	}

	return checkBundle, nil
}

// CreateCheckBundle creates a new check bundle (check).
func (a *API) CreateCheckBundle(cfg *CheckBundle) (*CheckBundle, error) {
	if cfg == nil {
		return nil, fmt.Errorf("Invalid check bundle config [nil]")
	}

	jsonCfg, err := json.Marshal(cfg)
	if err != nil {
		return nil, err
	}

	if a.Debug {
		a.Log.Printf("[DEBUG] create check bundle, sending JSON: %s", string(jsonCfg))
	}

	result, err := a.Post(config.CheckBundlePrefix, jsonCfg)
	if err != nil {
		return nil, err
	}

	checkBundle := &CheckBundle{}
	if err := json.Unmarshal(result, checkBundle); err != nil {
		return nil, err
	}

	return checkBundle, nil
}

// DeleteCheckBundle deletes passed check bundle.
func (a *API) DeleteCheckBundle(cfg *CheckBundle) (bool, error) {
	if cfg == nil {
		return false, fmt.Errorf("Invalid check bundle config [nil]")
	}
	return a.DeleteCheckBundleByCID(CIDType(&cfg.CID))
}

// DeleteCheckBundleByCID deletes check bundle with passed cid.
func (a *API) DeleteCheckBundleByCID(cid CIDType) (bool, error) {

	if cid == nil || *cid == "" {
		return false, fmt.Errorf("Invalid check bundle CID [none]")
	}

	bundleCID := string(*cid)

	matched, err := regexp.MatchString(config.CheckBundleCIDRegex, bundleCID)
	if err != nil {
		return false, err
	}
	if !matched {
		return false, fmt.Errorf("Invalid check bundle CID [%v]", bundleCID)
	}

	_, err = a.Delete(bundleCID)
	if err != nil {
		return false, err
	}

	return true, nil
}

// SearchCheckBundles returns check bundles matching the specified
// search query and/or filter. If nil is passed for both parameters
// all check bundles will be returned.
func (a *API) SearchCheckBundles(searchCriteria *SearchQueryType, filterCriteria *map[string][]string) (*[]CheckBundle, error) {

	q := url.Values{}

	if searchCriteria != nil && *searchCriteria != "" {
		q.Set("search", string(*searchCriteria))
	}

	if filterCriteria != nil && len(*filterCriteria) > 0 {
		for filter, criteria := range *filterCriteria {
			for _, val := range criteria {
				q.Add(filter, val)
			}
		}
	}

	if q.Encode() == "" {
		return a.FetchCheckBundles()
	}

	reqURL := url.URL{
		Path:     config.CheckBundlePrefix,
		RawQuery: q.Encode(),
	}

	resp, err := a.Get(reqURL.String())
	if err != nil {
		return nil, fmt.Errorf("[ERROR] API call error %+v", err)
	}

	var results []CheckBundle
	if err := json.Unmarshal(resp, &results); err != nil {
		return nil, err
	}

	return &results, nil
}


text:
package io.druid.indexer;

import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.metamx.common.Granularity;
import io.druid.data.input.Firehose;
import io.druid.data.input.InputRow;
import io.druid.data.input.impl.CSVParseSpec;
import io.druid.data.input.impl.DimensionsSpec;
import io.druid.data.input.impl.StringInputRowParser;
import io.druid.data.input.impl.TimestampSpec;
import io.druid.granularity.QueryGranularity;
import io.druid.indexer.path.DatasourcePathSpec;
import io.druid.indexer.path.UsedSegmentLister;
import io.druid.jackson.DefaultObjectMapper;
import io.druid.query.aggregation.AggregatorFactory;
import io.druid.query.aggregation.LongSumAggregatorFactory;
import io.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory;
import io.druid.segment.IndexIO;
import io.druid.segment.QueryableIndex;
import io.druid.segment.QueryableIndexStorageAdapter;
import io.druid.segment.StorageAdapter;
import io.druid.segment.indexing.DataSchema;
import io.druid.segment.indexing.granularity.UniformGranularitySpec;
import io.druid.segment.loading.LocalDataSegmentPuller;
import io.druid.segment.realtime.firehose.IngestSegmentFirehose;
import io.druid.timeline.DataSegment;
import io.druid.timeline.partition.HashBasedNumberedShardSpec;
import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class BatchDeltaIngestionTest
{
  public final
  @Rule
  TemporaryFolder temporaryFolder = new TemporaryFolder();

  private ObjectMapper mapper;
  private Interval interval;
  private List<DataSegment> segments;

  public BatchDeltaIngestionTest() throws IOException
  {
    mapper = new DefaultObjectMapper();
    mapper.registerSubtypes(new NamedType(HashBasedNumberedShardSpec.class, "hashed"));
    InjectableValues inject = new InjectableValues.Std().addValue(ObjectMapper.class, mapper);
    mapper.setInjectableValues(inject);

    this.interval = new Interval("2014-10-22T00:00:00Z/P1D");
    segments = ImmutableList.of(
        new DefaultObjectMapper()
            .readValue(
                this.getClass().getClassLoader().getResource("test-segment/descriptor.json"),
                DataSegment.class
            )
            .withLoadSpec(
                ImmutableMap.<String, Object>of(
                    "type",
                    "local",
                    "path",
                    this.getClass().getClassLoader().getResource("test-segment/index.zip").getPath()
                )
            )
    );
  }

  @Test
  public void testReindexing() throws Exception
  {
    HadoopDruidIndexerConfig config = makeHadoopDruidIndexerConfig(
        ImmutableMap.<String, Object>of(
            "type",
            "dataSource",
            "ingestionSpec",
            ImmutableMap.of(
                "dataSource",
                "xyz",
                "interval",
                interval
            ),
            "segments",
            segments
        ),
        temporaryFolder.newFolder()
    );

    List<ImmutableMap<String, Object>> expectedRows = ImmutableList.of(
        ImmutableMap.<String, Object>of(
            "time", DateTime.parse("2014-10-22T00:00:00.000Z"),
            "host", ImmutableList.of("a.example.com"),
            "visited_sum", 100L,
            "unique_hosts", 1.0d
        ),
        ImmutableMap.<String, Object>of(
            "time", DateTime.parse("2014-10-22T01:00:00.000Z"),
            "host", ImmutableList.of("b.example.com"),
            "visited_sum", 150L,
            "unique_hosts", 1.0d
        ),
        ImmutableMap.<String, Object>of(
            "time", DateTime.parse("2014-10-22T02:00:00.000Z"),
            "host", ImmutableList.of("c.example.com"),
            "visited_sum", 200L,
            "unique_hosts", 1.0d
        )
    );

    testIngestion(config, expectedRows);
  }

  @Test
  public void testDeltaIngestion() throws Exception
  {
    File dataFile = temporaryFolder.newFile();
    FileUtils.writeLines(
        dataFile,
        ImmutableList.of(
            "2014102200,a.example.com,a.example.com,90",
            "2014102201,b.example.com,b.example.com,25",
            "2014102202,c.example.com,c.example.com,70"
        )
    );

    HadoopDruidIndexerConfig config = makeHadoopDruidIndexerConfig(
        ImmutableMap.<String, Object>of(
            "type",
            "multi",
            "children",
            ImmutableList.of(
                ImmutableMap.<String, Object>of(
                    "type",
                    "dataSource",
                    "ingestionSpec",
                    ImmutableMap.of(
                        "dataSource",
                        "xyz",
                        "interval",
                        interval
                    ),
                    "segments",
                    segments
                ),
                ImmutableMap.<String, Object>of(
                    "type",
                    "static",
                    "paths",
                    dataFile.getCanonicalPath()
                )
            )
        ),
        temporaryFolder.newFolder()
    );

    List<ImmutableMap<String, Object>> expectedRows = ImmutableList.of(
        ImmutableMap.<String, Object>of(
            "time", DateTime.parse("2014-10-22T00:00:00.000Z"),
            "host", ImmutableList.of("a.example.com"),
            "visited_sum", 190L,
            "unique_hosts", 1.0d
        ),
        ImmutableMap.<String, Object>of(
            "time", DateTime.parse("2014-10-22T01:00:00.000Z"),
            "host", ImmutableList.of("b.example.com"),
            "visited_sum", 175L,
            "unique_hosts", 1.0d
        ),
        ImmutableMap.<String, Object>of(
            "time", DateTime.parse("2014-10-22T02:00:00.000Z"),
            "host", ImmutableList.of("c.example.com"),
            "visited_sum", 270L,
            "unique_hosts", 1.0d
        )
    );

    testIngestion(config, expectedRows);
  }

  private void testIngestion(HadoopDruidIndexerConfig config, List<ImmutableMap<String, Object>> expectedRowsGenerated)
      throws Exception
  {
    IndexGeneratorJob job = new LegacyIndexGeneratorJob(config);
    JobHelper.runJobs(ImmutableList.<Jobby>of(job), config);

    File segmentFolder = new File(
        String.format(
            "%s/%s/%s_%s/%s/0",
            config.getSchema().getIOConfig().getSegmentOutputPath(),
            config.getSchema().getDataSchema().getDataSource(),
            interval.getStart().toString(),
            interval.getEnd().toString(),
            config.getSchema().getTuningConfig().getVersion()
        )
    );

    Assert.assertTrue(segmentFolder.exists());

    File descriptor = new File(segmentFolder, "descriptor.json");
    File indexZip = new File(segmentFolder, "index.zip");
    Assert.assertTrue(descriptor.exists());
    Assert.assertTrue(indexZip.exists());

    DataSegment dataSegment = mapper.readValue(descriptor, DataSegment.class);
    Assert.assertEquals("website", dataSegment.getDataSource());
    Assert.assertEquals(config.getSchema().getTuningConfig().getVersion(), dataSegment.getVersion());
    Assert.assertEquals(interval, dataSegment.getInterval());
    Assert.assertEquals("local", dataSegment.getLoadSpec().get("type"));
    Assert.assertEquals(indexZip.getCanonicalPath(), dataSegment.getLoadSpec().get("path"));
    Assert.assertEquals("host", dataSegment.getDimensions().get(0));
    Assert.assertEquals("visited_sum", dataSegment.getMetrics().get(0));
    Assert.assertEquals("unique_hosts", dataSegment.getMetrics().get(1));
    Assert.assertEquals(Integer.valueOf(9), dataSegment.getBinaryVersion());

    HashBasedNumberedShardSpec spec = (HashBasedNumberedShardSpec) dataSegment.getShardSpec();
    Assert.assertEquals(0, spec.getPartitionNum());
    Assert.assertEquals(1, spec.getPartitions());

    File tmpUnzippedSegmentDir = temporaryFolder.newFolder();
    new LocalDataSegmentPuller().getSegmentFiles(dataSegment, tmpUnzippedSegmentDir);

    QueryableIndex index = IndexIO.loadIndex(tmpUnzippedSegmentDir);
    StorageAdapter adapter = new QueryableIndexStorageAdapter(index);

    Firehose firehose = new IngestSegmentFirehose(
        ImmutableList.of(adapter),
        ImmutableList.of("host"),
        ImmutableList.of("visited_sum", "unique_hosts"),
        null,
        interval,
        QueryGranularity.NONE
    );

    List<InputRow> rows = Lists.newArrayList();
    while (firehose.hasMore()) {
      rows.add(firehose.nextRow());
    }

    verifyRows(expectedRowsGenerated, rows);
  }

  private HadoopDruidIndexerConfig makeHadoopDruidIndexerConfig(Map<String, Object> inputSpec, File tmpDir)
      throws Exception
  {
    HadoopDruidIndexerConfig config = new HadoopDruidIndexerConfig(
        new HadoopIngestionSpec(
            new DataSchema(
                "website",
                new StringInputRowParser(
                    new CSVParseSpec(
                        new TimestampSpec("timestamp", "yyyyMMddHH", null),
                        new DimensionsSpec(ImmutableList.of("host"), null, null),
                        null,
                        ImmutableList.of("timestamp", "host", "host2", "visited_num")
                    )
                ),
                new AggregatorFactory[]{
                    new LongSumAggregatorFactory("visited_sum", "visited_num"),
                    new HyperUniquesAggregatorFactory("unique_hosts", "host2")
                },
                new UniformGranularitySpec(
                    Granularity.DAY, QueryGranularity.NONE, ImmutableList.of(this.interval)
                )
            ),
            new HadoopIOConfig(
                inputSpec,
                null,
                tmpDir.getCanonicalPath()
            ),
            new HadoopTuningConfig(
                tmpDir.getCanonicalPath(),
                null,
                null,
                null,
                null,
                null,
                false,
                false,
                false,
                false,
                null,
                false,
                false,
                false,
                null,
                null,
                false
            )
        )
    );

    config.setShardSpecs(
        ImmutableMap.<DateTime, List<HadoopyShardSpec>>of(
            interval.getStart(),
            ImmutableList.of(
                new HadoopyShardSpec(
                    new HashBasedNumberedShardSpec(0, 1, HadoopDruidIndexerConfig.jsonMapper),
                    0
                )
            )
        )
    );
    config = HadoopDruidIndexerConfig.fromSpec(config.getSchema());
    return config;
  }

  private void verifyRows(List<ImmutableMap<String, Object>> expectedRows, List<InputRow> actualRows)
  {
    Assert.assertEquals(expectedRows.size(), actualRows.size());

    for (int i = 0; i < expectedRows.size(); i++) {
      Map<String, Object> expected = expectedRows.get(i);
      InputRow actual = actualRows.get(i);

      Assert.assertEquals(ImmutableList.of("host"), actual.getDimensions());

      Assert.assertEquals(expected.get("time"), actual.getTimestamp());
      Assert.assertEquals(expected.get("host"), actual.getDimension("host"));
      Assert.assertEquals(expected.get("visited_sum"), actual.getLongMetric("visited_sum"));
      Assert.assertEquals(
          (Double) expected.get("unique_hosts"),
          (Double) HyperUniquesAggregatorFactory.estimateCardinality(actual.getRaw("unique_hosts")),
          0.001
      );
    }
  }
}