All Downloads are FREE. Search and download functionalities are using the official Maven repository.

us.fatehi.creditcardnumber.AccountNumber Maven / Gradle / Ivy

Go to download

Credit Card Number is a library that can provide details of a bank issued credit card number. All classes are immutable and thread-safe. The standard `toString()` function formats data in a readable form.

There is a newer version: 3.0.0
Show newest version
/*
 *
 * Credit Card Number
 * https://github.com/sualeh/credit_card_number
 * Copyright (c) 2014-2015, Sualeh Fatehi.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */
package us.fatehi.creditcardnumber;


import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.left;
import static org.apache.commons.lang3.StringUtils.leftPad;
import static org.apache.commons.lang3.StringUtils.right;
import static org.apache.commons.lang3.StringUtils.rightPad;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;
import static us.fatehi.creditcardnumber.Utility.non_digit;

import java.util.Arrays;

/**
 * Parses and represents the primary account number of the bank card.
 */
public final class AccountNumber
  extends BaseRawData
  implements PrimaryAccountNumber, Comparable
{

  private static final long serialVersionUID = -7012531091389412459L;

  private final String accountNumber;
  private final CardBrand cardBrand;
  private final MajorIndustryIdentifier majorIndustryIdentifier;
  private final boolean passesLuhnCheck;

  /**
   * No primary account number of the bank card.
   */
  public AccountNumber()
  {
    this(null);
  }

  /**
   * Parses the primary account number of the bank card. Can accept card
   * numbers with spaces or dashes.
   *
   * @param rawAccountNumber
   *        Raw primary account number.
   */
  public AccountNumber(final String rawAccountNumber)
  {
    super(rawAccountNumber);

    accountNumber = non_digit.matcher(trimToEmpty(rawAccountNumber))
      .replaceAll("");
    passesLuhnCheck = luhnCheck();
    majorIndustryIdentifier = MajorIndustryIdentifier.from(accountNumber);
    cardBrand = CardBrand.from(accountNumber);
  }

  @Override
  public int compareTo(final AccountNumber otherPan)
  {
    if (otherPan == null)
    {
      return 1;
    }
    else if (!hasPrimaryAccountNumber() && !otherPan.hasPrimaryAccountNumber())
    {
      return 0;
    }
    else if (hasPrimaryAccountNumber() && !otherPan.hasPrimaryAccountNumber())
    {
      return 1;
    }
    else
    {
      return accountNumber.compareTo(otherPan.accountNumber);
    }
  }

  /**
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(final Object obj)
  {
    if (this == obj)
    {
      return true;
    }
    if (obj == null)
    {
      return false;
    }
    if (!(obj instanceof AccountNumber))
    {
      return false;
    }
    final AccountNumber other = (AccountNumber) obj;
    if (accountNumber == null)
    {
      if (other.accountNumber != null)
      {
        return false;
      }
    }
    else if (!accountNumber.equals(other.accountNumber))
    {
      return false;
    }
    return true;
  }

  /**
   * @see us.fatehi.creditcardnumber.RawData#exceedsMaximumLength()
   */
  @Override
  public boolean exceedsMaximumLength()
  {
    return getAccountNumberLength() > 19;
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#getAccountNumber()
   */
  @Override
  public String getAccountNumber()
  {
    return accountNumber;
  }

  @Override
  public int getAccountNumberLength()
  {
    return accountNumber.length();
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#getCardBrand()
   */
  @Override
  public CardBrand getCardBrand()
  {
    return cardBrand;
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#getIssuerIdentificationNumber()
   */
  @Override
  public String getIssuerIdentificationNumber()
  {
    return rightPad(left(accountNumber, 6), 6, "0");
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#getLastFourDigits()
   */
  @Override
  public String getLastFourDigits()
  {
    return leftPad(right(accountNumber, 4), 4, "0");
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#getMajorIndustryIdentifier()
   */
  @Override
  public MajorIndustryIdentifier getMajorIndustryIdentifier()
  {
    return majorIndustryIdentifier;
  }

  /**
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result
             + (accountNumber == null? 0: accountNumber.hashCode());
    return result;
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#hasPrimaryAccountNumber()
   */
  @Override
  public boolean hasPrimaryAccountNumber()
  {
    return !isBlank(accountNumber);
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#isLengthValid()
   */
  @Override
  public boolean isLengthValid()
  {
    return Arrays.asList(13, 14, 15, 16, 19).contains(getAccountNumberLength());
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#isPrimaryAccountNumberValid()
   */
  @Override
  public boolean isPrimaryAccountNumberValid()
  {
    return hasPrimaryAccountNumber() && isLengthValid() && passesLuhnCheck()
           && cardBrand != null && cardBrand != CardBrand.Unknown;
  }

  /**
   * @see us.fatehi.creditcardnumber.PrimaryAccountNumber#passesLuhnCheck()
   */
  @Override
  public boolean passesLuhnCheck()
  {
    return passesLuhnCheck;
  }

  /**
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return accountNumber;
  }

  private boolean luhnCheck()
  {

    final int length = accountNumber.length();
    int sum = 0;
    boolean alternate = false;
    for (int i = length - 1; i >= 0; i--)
    {
      int digit = Character.digit(accountNumber.charAt(i), 10);
      if (alternate)
      {
        digit = digit * 2;
        digit = digit > 9? digit - 9: digit;
      }
      sum = sum + digit;
      alternate = !alternate;
    }
    final boolean passesLuhnCheck = sum % 10 == 0;
    return passesLuhnCheck;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy