graph-builder
Version:
A graph builder library for modeling abstract graph structures.
115 lines • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const NaturalOrdering_1 = require("../collect/NaturalOrdering");
const Maps_1 = require("../collect/Maps");
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Modifications (C) 2019 Ben Sorohan
*/
/**
* The type of ordering that this object specifies.
*
* <ul>
* <li>UNORDERED: no order is guaranteed.
* <li>INSERTION: insertion ordering is guaranteed.
* <li>SORTED: ordering according to a supplied comparator is guaranteed.
* </ul>
*/
var Type;
(function (Type) {
Type[Type["UNORDERED"] = 0] = "UNORDERED";
Type[Type["INSERTION"] = 1] = "INSERTION";
Type[Type["SORTED"] = 2] = "SORTED";
})(Type = exports.Type || (exports.Type = {}));
/**
* Used to represent the order of elements in a data structure that supports different options for
* iteration order guarantees.
*
* @remarks
*
* Example usage:
*
* ```typescript
* const graph: MutableGraph<number> =
* GraphBuilder.directed().nodeOrder(ElementOrder.natural<number>()).build();
* }
* ```
*
* @public
*/
class ElementOrder {
constructor(type, comparator) {
this.type = type;
this.comparator = comparator;
}
/** Returns an instance which specifies that no ordering is guaranteed. */
static unordered() {
return new ElementOrder(Type.UNORDERED, undefined);
}
/** Returns an instance which specifies that insertion ordering is guaranteed. */
static insertion() {
return new ElementOrder(Type.INSERTION, undefined);
}
/**
* Returns an instance which specifies that the natural ordering of the elements is guaranteed.
*/
static natural() {
return new ElementOrder(Type.SORTED, NaturalOrdering_1.NaturalOrdering.of());
}
/**
* Returns an instance which specifies that the ordering of the elements is guaranteed to be
* determined by `comparator`.
*/
static sorted(comparator) {
return new ElementOrder(Type.SORTED, comparator);
}
/**
* Returns the {@link Comparator} used.
*
* Throws an error if comparator is not defined
*/
getComparator() {
if (this.comparator) {
return this.comparator;
}
throw new Error("This ordering does not define a comparator.");
}
equals(obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ElementOrder)) {
return false;
}
const other = obj;
return (this.type == other.type) && this.comparator === other.getComparator();
}
/** Returns an empty mutable map whose keys will respect this {@link ElementOrder}. */
createMap(expectedSize) {
switch (this.type) {
case Type.UNORDERED:
new Map();
case Type.INSERTION:
return new Maps_1.LinkedHashMap();
case Type.SORTED:
return new Maps_1.TreeMap(this.getComparator());
default:
throw new Error();
}
}
}
exports.ElementOrder = ElementOrder;
//# sourceMappingURL=ElementOrder.js.map