|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [https://neo4j.com] |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + * @access public |
| 21 | + * @class A class representing a protocol version the driver is using to communicate with the server. |
| 22 | + * @param {number} major the major version of the protocol. |
| 23 | + * @param {number} minor the minor version of the protocol. |
| 24 | + */ |
| 25 | +export class ProtocolVersion { |
| 26 | + private readonly major: number |
| 27 | + private readonly minor: number |
| 28 | + constructor (major: number, minor: number) { |
| 29 | + this.major = major |
| 30 | + this.minor = minor |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * |
| 35 | + * @returns {number} The major version of the protocol |
| 36 | + */ |
| 37 | + getMajor (): number { |
| 38 | + return this.major |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @returns {number} The minor version of the protocol |
| 43 | + */ |
| 44 | + getMinor (): number { |
| 45 | + return this.major |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * |
| 50 | + * @param {ProtocolVersion | {major: number, minor: number}} other the protocol version to compare to |
| 51 | + * @returns {boolean} If this version semantically smaller than the other version. |
| 52 | + */ |
| 53 | + isLessThan (other: ProtocolVersion): boolean { |
| 54 | + if (this.major < other.major) { |
| 55 | + return true |
| 56 | + } else if (this.major === other.major && this.minor < other.minor) { |
| 57 | + return true |
| 58 | + } |
| 59 | + return false |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * |
| 64 | + * @param {ProtocolVersion | {major: number, minor: number}} other the protocol version to compare to |
| 65 | + * @returns {boolean} If this version is semantically larger than the other version. |
| 66 | + */ |
| 67 | + isGreaterThan (other: ProtocolVersion): boolean { |
| 68 | + if (this.major > other.major) { |
| 69 | + return true |
| 70 | + } else if (this.major === other.major && this.minor > other.minor) { |
| 71 | + return true |
| 72 | + } |
| 73 | + return false |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * |
| 78 | + * @param {ProtocolVersion | {major: number, minor: number}} other the protocol version to compare to |
| 79 | + * @returns {boolean} if this version is semantically larger or equal to the other version. |
| 80 | + */ |
| 81 | + isGreaterOrEqualTo (other: ProtocolVersion): boolean { |
| 82 | + return !this.isLessThan(other) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * |
| 87 | + * @param {ProtocolVersion | {major: number, minor: number}} other the protocol version to compare to |
| 88 | + * @returns {boolean} if this version is semantically smaller or equal to the other version. |
| 89 | + */ |
| 90 | + isLessOrEqualTo (other: ProtocolVersion): boolean { |
| 91 | + return !this.isGreaterThan(other) |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * |
| 96 | + * @param {ProtocolVersion | {major: number, minor: number}} other the protocol version to compare to |
| 97 | + * @returns {boolean} If this version is the equal to the other version. |
| 98 | + */ |
| 99 | + isEqualTo (other: ProtocolVersion): boolean { |
| 100 | + return this.major === other.major && this.minor === other.minor |
| 101 | + } |
| 102 | + |
| 103 | + toString (): string { |
| 104 | + return this.major.toString() + '.' + this.minor.toString() |
| 105 | + } |
| 106 | +} |
0 commit comments