ern-api-gen
Version:
Electrode Native API generator
156 lines • 4.08 kB
JavaScript
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fakeMap_1 = __importStar(require("./fakeMap"));
const FakeHashSet_1 = require("./FakeHashSet");
exports.newHashMap = fakeMap_1.default;
/**
* This provides a bunch of Java like classes. Collections, HashMap, HashSet
* and a few conviences. Attention has been paid to make it look very
* similar, however they are not identically. Specifically "size" is not a function
* it still a property. This is mostly due to fear, of what will break if we change
* that value.
*
* It should be useful for Java programmers coming to JS and for porting code from JS->JavaScript
*
* These classes are meant to be convient hence iteration.
*
*/
class HashSet {
constructor() {
this.set = new Set();
}
contains(value) {
return this.set.has(value);
}
isEmpty() {
return this.set.size === 0;
}
add(val) {
const size = this.set.size;
return this.set.add(val).size !== size;
}
addAll(itr) {
const size = this.set.size;
for (const val of itr) {
this.add(val);
}
return size !== this.set.size;
}
containsAll(itr) {
for (const val of itr) {
if (!this.set.has(val)) {
return false;
}
}
return true;
}
remove(val) {
return this.set.delete(val);
}
removeAll(itr) {
const size = this.set.size;
for (const val of itr) {
this.set.delete(val);
}
return size !== this.set.size;
}
retainAll(itr) {
const set = itr instanceof Set ? itr : new Set(itr);
const size = this.set.size;
for (const val of this.set) {
if (!set.has(val)) {
this.remove(val);
}
}
return size !== this.set.size;
}
toArray() {
return Array.from(this.set);
}
toJSON() {
const ret = [];
for (const value of this.set) {
ret.push(value);
}
return ret;
}
toString() {
return JSON.stringify(this.toJSON());
}
}
exports.HashSet = HashSet;
exports.Collections = {
sort(arr, comparator) {
return arr && arr.sort(comparator);
},
emptyList() {
return EMPTY_LIST;
},
emptySet() {
return HASH_SET;
},
emptyHashMap() {
return HASH_MAP;
},
};
exports.Arrays = {
asList(...args) {
return args;
},
};
exports.Lists = {
transform(arr, fn) {
return arr.map(fn);
},
newArrayList(arr) {
return arr ? arr.concat() : [];
},
};
function keyThis(key) {
return [key, this[key]];
}
function arrThis(k, v) {
return [k, v];
}
exports.asMap = obj => {
if (obj == null) {
return exports.newHashMap();
}
if (obj instanceof Map || obj instanceof fakeMap_1.FakeHashMap) {
return obj;
}
if (Array.isArray(obj)) {
return exports.newHashSet(...obj);
}
return exports.newHashMap(...Object.keys(obj).map(keyThis, obj));
};
exports.newHashSet = FakeHashSet_1.fakeSet;
const EMPTY_LIST = Object.freeze([]);
const HASH_SET = Object.freeze(exports.newHashSet());
const HASH_MAP = Object.freeze(exports.newHashMap());
exports.isNotEmptySet = set => {
if (set == null) {
return false;
}
if (set instanceof Set || set instanceof Map) {
return set.size > 0;
}
if (Array.isArray(set)) {
return set.length > 0;
}
if (typeof set[Symbol.iterator] === 'function') {
if ('size' in set) {
return set.size !== 0;
}
return !set[Symbol.iterator]().next().done;
}
return false;
};
//# sourceMappingURL=javaUtil.js.map