Android Open Source - android-api Money






From Project

Back to project page android-api.

License

The source code is released under:

Apache License

If you think the Android project android-api listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Copyright 2010 Square, Inc.
package com.squareup.android;
//from  w  w  w.j a  v a 2  s  .  c om
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

/**
 * A quantity of a specific {@linkplain Currency currency}.
 *
 * @author Bob Lee (bob@squareup.com)
 */
public final class Money implements Serializable {
  private static final long serialVersionUID = 0;

  /**
   * The maximum amount supported by Square. This is the absolute maximum
   * supported by the Square application; it's likely much greater than the
   * merchant-specific card payment limit.
   */
  public static final long MAX_AMOUNT = 999999999; // 9 digits or $9,999,999.99

  private final long amount;
  private final Currency currency;

  /**
   * Constructs a new Money. The amount is specified in
   * atomic units of the specified currency. For example, if the currency is the
   * {@linkplain Currency#USD U.S. dollar}, the amount is specified in
   * cents.
   *
   * @param amount atomic units of the specified currency, >= 0 && <=
   *  {@link #MAX_AMOUNT}
   * @param currency type
   * @throws IllegalArgumentException if amount is < 0 || >
   *  {@link #MAX_AMOUNT}
   * @throws NullPointerException if currency is null
   */
  public Money(long amount, Currency currency) {
    if (currency == null) throw new NullPointerException("currency");
    if (amount < 0) throw new IllegalArgumentException("amount < 0");
    if (amount > MAX_AMOUNT) {
      throw new IllegalArgumentException("amount > MAX_AMOUNT");
    }

    this.amount = amount;
    this.currency = currency;
  }

  /**
   * Returns the amount in atomic units of {@link #currency}.
   */
  public long amount() {
    return amount;
  }

  /**
   * Returns the currency type for this value.
   */
  public Currency currency() {
    return currency;
  }

  @Override public String toString() {
    return "Money{" +
        "amount=" + amount +
        ", currency=" + currency +
        '}';
  }

  private void readObject(ObjectInputStream in) throws IOException,
      ClassNotFoundException {
    in.defaultReadObject();
    if (currency == null) throw new AssertionError("missing currency");
    if (amount < 0) throw new AssertionError("amount < 0");
    if (amount > MAX_AMOUNT) throw new AssertionError("amount > MAX_AMOUNT");
  }
}




Java Source Code List

com.squareup.android.Bill.java
com.squareup.android.Currency.java
com.squareup.android.ImageNotFoundException.java
com.squareup.android.Image.java
com.squareup.android.LineItem.java
com.squareup.android.Money.java
com.squareup.android.Square.java
com.squareup.android.examples.twocents.TwoCents.java
com.squareup.android.package-info.java