[server] add module
This commit is contained in:
52
src/app/haxe/ru/m/update/Version.hx
Normal file
52
src/app/haxe/ru/m/update/Version.hx
Normal file
@@ -0,0 +1,52 @@
|
||||
package ru.m.update;
|
||||
|
||||
abstract Version(Array<Int>) {
|
||||
|
||||
public function new(mayor:Int, minor:Int, patch:Int, snapshot:Bool = false) {
|
||||
this = [
|
||||
mayor,
|
||||
minor,
|
||||
patch,
|
||||
snapshot ? -1 : 0,
|
||||
];
|
||||
}
|
||||
|
||||
@:arrayAccess public inline function get(key:Int) return this[key];
|
||||
|
||||
public function compare(other:Version):Int {
|
||||
if (other == null) {
|
||||
return 1;
|
||||
}
|
||||
for (i in 0...4) {
|
||||
var d = this[i] - other[i];
|
||||
if (d != 0) {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@:op(A > B) static function gt(a:Version, b:Version):Bool return a.compare(b) > 0;
|
||||
|
||||
@:op(A == B) static function eq(a:Version, b:Version):Bool return a.compare(b) == 0;
|
||||
|
||||
@:op(A < B) static function lt(a:Version, b:Version):Bool return a.compare(b) < 0;
|
||||
|
||||
@:from public static function fromString(value:String):Version {
|
||||
var r = ~/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)?/;
|
||||
return if (r.match(value)) {
|
||||
new Version(
|
||||
Std.parseInt(r.matched(1)),
|
||||
Std.parseInt(r.matched(2)),
|
||||
Std.parseInt(r.matched(3)),
|
||||
r.matched(4) != null
|
||||
);
|
||||
} else {
|
||||
new Version(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@:to public function toString():String {
|
||||
return '${this[0]}.${this[1]}.${this[2]}${this[3] < 0 ? "-SNAPSHOT" : ""}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user