aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/com/google/common/truth/BigDecimalSubject.java
blob: dbba59170e62f81e4c65f66ef48ba9aa8d5cce46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (c) 2015 Google, Inc.
 *
 * 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.
 */
package com.google.common.truth;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Fact.simpleFact;

import java.math.BigDecimal;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * Propositions for {@link BigDecimal} typed subjects.
 *
 * @author Kurt Alfred Kluever
 */
public final class BigDecimalSubject extends ComparableSubject<BigDecimal> {
  private final @Nullable BigDecimal actual;

  BigDecimalSubject(FailureMetadata metadata, @Nullable BigDecimal actual) {
    super(metadata, actual);
    this.actual = actual;
  }

  /**
   * Fails if the subject's value is not equal to the value of the given {@link BigDecimal}. (i.e.,
   * fails if {@code actual.comparesTo(expected) != 0}).
   *
   * <p><b>Note:</b> The scale of the BigDecimal is ignored. If you want to compare the values and
   * the scales, use {@link #isEqualTo(Object)}.
   */
  public void isEqualToIgnoringScale(BigDecimal expected) {
    compareValues(expected);
  }

  /**
   * Fails if the subject's value is not equal to the value of the {@link BigDecimal} created from
   * the expected string (i.e., fails if {@code actual.comparesTo(new BigDecimal(expected)) != 0}).
   *
   * <p><b>Note:</b> The scale of the BigDecimal is ignored. If you want to compare the values and
   * the scales, use {@link #isEqualTo(Object)}.
   */
  public void isEqualToIgnoringScale(String expected) {
    compareValues(new BigDecimal(expected));
  }

  /**
   * Fails if the subject's value is not equal to the value of the {@link BigDecimal} created from
   * the expected {@code long} (i.e., fails if {@code actual.comparesTo(new BigDecimal(expected)) !=
   * 0}).
   *
   * <p><b>Note:</b> The scale of the BigDecimal is ignored. If you want to compare the values and
   * the scales, use {@link #isEqualTo(Object)}.
   */
  public void isEqualToIgnoringScale(long expected) {
    compareValues(new BigDecimal(expected));
  }

  /**
   * Fails if the subject's value and scale is not equal to the given {@link BigDecimal}.
   *
   * <p><b>Note:</b> If you only want to compare the values of the BigDecimals and not their scales,
   * use {@link #isEqualToIgnoringScale(BigDecimal)} instead.
   */
  @Override // To express more specific javadoc
  public void isEqualTo(@Nullable Object expected) {
    super.isEqualTo(expected);
  }

  /**
   * Fails if the subject is not equivalent to the given value according to {@link
   * Comparable#compareTo}, (i.e., fails if {@code a.comparesTo(b) != 0}). This method behaves
   * identically to (the more clearly named) {@link #isEqualToIgnoringScale(BigDecimal)}.
   *
   * <p><b>Note:</b> Do not use this method for checking object equality. Instead, use {@link
   * #isEqualTo(Object)}.
   */
  @Override
  public void isEquivalentAccordingToCompareTo(@Nullable BigDecimal expected) {
    compareValues(expected);
  }

  private void compareValues(@Nullable BigDecimal expected) {
    if (checkNotNull(actual).compareTo(checkNotNull(expected)) != 0) {
      failWithoutActual(fact("expected", expected), butWas(), simpleFact("(scale is ignored)"));
    }
  }
}