feat(types): add new pair type
This commit is contained in:
parent
597ecf23e2
commit
d63e7a8a07
56
packages/wyatt_type_utils/lib/src/pair/pair.dart
Normal file
56
packages/wyatt_type_utils/lib/src/pair/pair.dart
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
extension PairExtension<T> on Pair<T, T> {
|
||||
List<T?> toList() => List.from([left, right]);
|
||||
}
|
||||
|
||||
/// {@template pair}
|
||||
/// [Pair] is a simple object which contains pair of two values.
|
||||
/// {@endtemplate}
|
||||
class Pair<L, R> {
|
||||
final L? left;
|
||||
final R? right;
|
||||
|
||||
/// {@macro pair}
|
||||
const Pair(this.left, this.right);
|
||||
|
||||
@override
|
||||
String toString() => '($left, $right)';
|
||||
|
||||
Pair<L, R> copyWith({
|
||||
L? left,
|
||||
R? right,
|
||||
}) =>
|
||||
Pair<L, R>(
|
||||
left ?? this.left,
|
||||
right ?? this.right,
|
||||
);
|
||||
|
||||
@override
|
||||
// ignore: avoid_equals_and_hash_code_on_mutable_classes
|
||||
bool operator ==(covariant Pair<L, R> other) {
|
||||
if (identical(this, other)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return other.left == left && other.right == right;
|
||||
}
|
||||
|
||||
@override
|
||||
// ignore: avoid_equals_and_hash_code_on_mutable_classes
|
||||
int get hashCode => left.hashCode ^ right.hashCode;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user