• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2022 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Simple builder-style class to implement object equality more succinctly.
16export class EqualsBuilder<T> {
17  result = true;
18  first: T;
19  second: T;
20
21  constructor(first: T, second: T) {
22    this.first = first;
23    this.second = second;
24  }
25
26  comparePrimitive(getter: (arg: T) => string | number): EqualsBuilder<T> {
27    if (this.result) {
28      this.result = getter(this.first) === getter(this.second);
29    }
30    return this;
31  }
32
33  compare<S>(
34      comparator: (first: S, second: S) => boolean,
35      getter: (arg: T) => S): EqualsBuilder<T> {
36    if (this.result) {
37      this.result = comparator(getter(this.first), getter(this.second));
38    }
39    return this;
40  }
41
42  equals(): boolean {
43    return this.result;
44  }
45}
46