Coverage Report - org.joda.money.MoneyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MoneyUtils
100%
50/50
100%
62/62
3.867
 
 1  
 /*
 2  
  *  Copyright 2009-2013 Stephen Colebourne
 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  
 package org.joda.money;
 17  
 
 18  
 /**
 19  
  * Utilities for working with monetary values that handle null.
 20  
  * <p>
 21  
  * This utility class contains thread-safe static methods.
 22  
  */
 23  
 public final class MoneyUtils {
 24  
 
 25  
     /**
 26  
      * Validates that the object specified is not null.
 27  
      *
 28  
      * @param object  the object to check, not null
 29  
      * @throws NullPointerException if the input value is null
 30  
      */
 31  
     static void checkNotNull(Object object, String message) {
 32  3626
         if (object == null) {
 33  126
             throw new NullPointerException(message);
 34  
         }
 35  3500
     }
 36  
 
 37  
     //-----------------------------------------------------------------------
 38  
     /**
 39  
      * Private constructor.
 40  
      */
 41  1
     private MoneyUtils() {
 42  1
     }
 43  
 
 44  
     //-----------------------------------------------------------------------
 45  
     /**
 46  
      * Checks if the monetary value is zero, treating null as zero.
 47  
      * <p>
 48  
      * This method accepts any implementation of {@code BigMoneyProvider}.
 49  
      * 
 50  
      * @param moneyProvider  the money to check, null returns zero
 51  
      * @return true if the money is null or zero
 52  
      */
 53  
     public static boolean isZero(BigMoneyProvider moneyProvider) {
 54  8
         return (moneyProvider == null || moneyProvider.toBigMoney().isZero());
 55  
     }
 56  
 
 57  
     /**
 58  
      * Checks if the monetary value is positive and non-zero, treating null as zero.
 59  
      * <p>
 60  
      * This method accepts any implementation of {@code BigMoneyProvider}.
 61  
      * 
 62  
      * @param moneyProvider  the money to check, null returns false
 63  
      * @return true if the money is non-null and positive
 64  
      */
 65  
     public static boolean isPositive(BigMoneyProvider moneyProvider) {
 66  8
         return (moneyProvider != null && moneyProvider.toBigMoney().isPositive());
 67  
     }
 68  
 
 69  
     /**
 70  
      * Checks if the monetary value is positive or zero, treating null as zero.
 71  
      * <p>
 72  
      * This method accepts any implementation of {@code BigMoneyProvider}.
 73  
      * 
 74  
      * @param moneyProvider  the money to check, null returns true
 75  
      * @return true if the money is null, zero or positive
 76  
      */
 77  
     public static boolean isPositiveOrZero(BigMoneyProvider moneyProvider) {
 78  8
         return (moneyProvider == null || moneyProvider.toBigMoney().isPositiveOrZero());
 79  
     }
 80  
 
 81  
     /**
 82  
      * Checks if the monetary value is negative and non-zero, treating null as zero.
 83  
      * <p>
 84  
      * This method accepts any implementation of {@code BigMoneyProvider}.
 85  
      * 
 86  
      * @param moneyProvider  the money to check, null returns false
 87  
      * @return true if the money is non-null and negative
 88  
      */
 89  
     public static boolean isNegative(BigMoneyProvider moneyProvider) {
 90  45
         return (moneyProvider != null && moneyProvider.toBigMoney().isNegative());
 91  
     }
 92  
 
 93  
     /**
 94  
      * Checks if the monetary value is negative or zero, treating null as zero.
 95  
      * <p>
 96  
      * This method accepts any implementation of {@code BigMoneyProvider}.
 97  
      * 
 98  
      * @param moneyProvider  the money to check, null returns true
 99  
      * @return true if the money is null, zero or negative
 100  
      */
 101  
     public static boolean isNegativeOrZero(BigMoneyProvider moneyProvider) {
 102  8
         return (moneyProvider == null || moneyProvider.toBigMoney().isNegativeOrZero());
 103  
     }
 104  
 
 105  
     //-----------------------------------------------------------------------
 106  
     /**
 107  
      * Finds the maximum {@code Money} value, handing null.
 108  
      * <p>
 109  
      * This returns the greater of money1 or money2 where null is ignored.
 110  
      * If both input values are null, then null is returned.
 111  
      * 
 112  
      * @param money1  the first money instance, null returns money2
 113  
      * @param money2  the first money instance, null returns money1
 114  
      * @return the maximum value, null if both inputs are null
 115  
      * @throws CurrencyMismatchException if the currencies differ
 116  
      */
 117  
     public static Money max(Money money1, Money money2) {
 118  6
         if (money1 == null) {
 119  2
             return money2;
 120  
         }
 121  4
         if (money2 == null) {
 122  1
             return money1;
 123  
         }
 124  3
         return money1.compareTo(money2) > 0 ? money1 : money2;
 125  
     }
 126  
 
 127  
     /**
 128  
      * Finds the minimum {@code Money} value, handing null.
 129  
      * <p>
 130  
      * This returns the greater of money1 or money2 where null is ignored.
 131  
      * If both input values are null, then null is returned.
 132  
      * 
 133  
      * @param money1  the first money instance, null returns money2
 134  
      * @param money2  the first money instance, null returns money1
 135  
      * @return the minimum value, null if both inputs are null
 136  
      * @throws CurrencyMismatchException if the currencies differ
 137  
      */
 138  
     public static Money min(Money money1, Money money2) {
 139  6
         if (money1 == null) {
 140  2
             return money2;
 141  
         }
 142  4
         if (money2 == null) {
 143  1
             return money1;
 144  
         }
 145  3
         return money1.compareTo(money2) < 0 ? money1 : money2;
 146  
     }
 147  
 
 148  
     //-----------------------------------------------------------------------
 149  
     /**
 150  
      * Adds two {@code Money} objects, handling null.
 151  
      * <p>
 152  
      * This returns {@code money1 + money2} where null is ignored.
 153  
      * If both input values are null, then null is returned.
 154  
      * 
 155  
      * @param money1  the first money instance, null returns money2
 156  
      * @param money2  the first money instance, null returns money1
 157  
      * @return the total, where null is ignored, null if both inputs are null
 158  
      * @throws CurrencyMismatchException if the currencies differ
 159  
      */
 160  
     public static Money add(Money money1, Money money2) {
 161  5
         if (money1 == null) {
 162  2
             return money2;
 163  
         }
 164  3
         if (money2 == null) {
 165  1
             return money1;
 166  
         }
 167  2
         return money1.plus(money2);
 168  
     }
 169  
 
 170  
     //-----------------------------------------------------------------------
 171  
     /**
 172  
      * Subtracts the second {@code Money} from the first, handling null.
 173  
      * <p>
 174  
      * This returns {@code money1 - money2} where null is ignored.
 175  
      * If both input values are null, then null is returned.
 176  
      * 
 177  
      * @param money1  the first money instance, null treated as zero
 178  
      * @param money2  the first money instance, null returns money1
 179  
      * @return the total, where null is ignored, null if both inputs are null
 180  
      * @throws CurrencyMismatchException if the currencies differ
 181  
      */
 182  
     public static Money subtract(Money money1, Money money2) {
 183  5
         if (money2 == null) {
 184  2
             return money1;
 185  
         }
 186  3
         if (money1 == null) {
 187  1
             return money2.negated();
 188  
         }
 189  2
         return money1.minus(money2);
 190  
     }
 191  
 
 192  
     //-----------------------------------------------------------------------
 193  
     /**
 194  
      * Finds the maximum {@code BigMoney} value, handing null.
 195  
      * <p>
 196  
      * This returns the greater of money1 or money2 where null is ignored.
 197  
      * If both input values are null, then null is returned.
 198  
      * 
 199  
      * @param money1  the first money instance, null returns money2
 200  
      * @param money2  the first money instance, null returns money1
 201  
      * @return the maximum value, null if both inputs are null
 202  
      * @throws CurrencyMismatchException if the currencies differ
 203  
      */
 204  
     public static BigMoney max(BigMoney money1, BigMoney money2) {
 205  6
         if (money1 == null) {
 206  2
             return money2;
 207  
         }
 208  4
         if (money2 == null) {
 209  1
             return money1;
 210  
         }
 211  3
         return money1.compareTo(money2) > 0 ? money1 : money2;
 212  
     }
 213  
 
 214  
     /**
 215  
      * Finds the minimum {@code BigMoney} value, handing null.
 216  
      * <p>
 217  
      * This returns the greater of money1 or money2 where null is ignored.
 218  
      * If both input values are null, then null is returned.
 219  
      * 
 220  
      * @param money1  the first money instance, null returns money2
 221  
      * @param money2  the first money instance, null returns money1
 222  
      * @return the minimum value, null if both inputs are null
 223  
      * @throws CurrencyMismatchException if the currencies differ
 224  
      */
 225  
     public static BigMoney min(BigMoney money1, BigMoney money2) {
 226  6
         if (money1 == null) {
 227  2
             return money2;
 228  
         }
 229  4
         if (money2 == null) {
 230  1
             return money1;
 231  
         }
 232  3
         return money1.compareTo(money2) < 0 ? money1 : money2;
 233  
     }
 234  
 
 235  
     //-----------------------------------------------------------------------
 236  
     /**
 237  
      * Adds two {@code BigMoney} objects, handling null.
 238  
      * <p>
 239  
      * This returns {@code money1 + money2} where null is ignored.
 240  
      * If both input values are null, then null is returned.
 241  
      * 
 242  
      * @param money1  the first money instance, null returns money2
 243  
      * @param money2  the first money instance, null returns money1
 244  
      * @return the total, where null is ignored, null if both inputs are null
 245  
      * @throws CurrencyMismatchException if the currencies differ
 246  
      */
 247  
     public static BigMoney add(BigMoney money1, BigMoney money2) {
 248  5
         if (money1 == null) {
 249  2
             return money2;
 250  
         }
 251  3
         if (money2 == null) {
 252  1
             return money1;
 253  
         }
 254  2
         return money1.plus(money2);
 255  
     }
 256  
 
 257  
     //-----------------------------------------------------------------------
 258  
     /**
 259  
      * Subtracts the second {@code BigMoney} from the first, handling null.
 260  
      * <p>
 261  
      * This returns {@code money1 - money2} where null is ignored.
 262  
      * If both input values are null, then null is returned.
 263  
      * 
 264  
      * @param money1  the first money instance, null treated as zero
 265  
      * @param money2  the first money instance, null returns money1
 266  
      * @return the total, where null is ignored, null if both inputs are null
 267  
      * @throws CurrencyMismatchException if the currencies differ
 268  
      */
 269  
     public static BigMoney subtract(BigMoney money1, BigMoney money2) {
 270  5
         if (money2 == null) {
 271  2
             return money1;
 272  
         }
 273  3
         if (money1 == null) {
 274  1
             return money2.negated();
 275  
         }
 276  2
         return money1.minus(money2);
 277  
     }
 278  
 
 279  
 }