• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright 2024 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Ensures the truth of an expression involving one or more parameters to the
19 * calling method.
20 */
21export function checkArgument(condition: boolean, message?: string) {
22  if (!condition) {
23    throw new Error(`Invalid argument [detail: ${message}]`);
24  }
25}
26
27/**
28 * Ensures that an object reference passed as a parameter to the calling method
29 * is not null or undefined.
30 */
31export function checkNotNull<T>(
32  reference: T | null | undefined,
33  message?: string
34): T {
35  if (reference === null || reference === undefined) {
36    throw new Error(`null reference exception [detail: ${message}]`);
37  }
38  return reference;
39}
40
41/**
42 * Ensures the truth of an expression involving the state of the calling
43 * instance, but not involving any parameters to the calling method.
44 */
45export function checkState(condition: boolean, message?: string) {
46  if (!condition) {
47    throw new Error(`Invalid state [detail: ${message}]`);
48  }
49}
50