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

net.yapbam.data.AbstractTransaction Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.yapbam.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** An abstract transaction.
 * These transactions have a description, an amount, an account, a mode, a category and a list of subtransactions.
 */
public abstract class AbstractTransaction implements Cloneable, Serializable {
	private static final long serialVersionUID = 1L;

	private static volatile long currentId = 0;
	
	private long id;
	private String description;
	private String comment;
	private double amount;
	private Account account;
	private Mode mode;
	private Category category;
	private List subTransactions;
	
	private static synchronized void setId(AbstractTransaction transaction) {
		if (currentId==Long.MAX_VALUE) {
			throw new RuntimeException("Transaction counter has an overflow"); //$NON-NLS-1$
		}
		transaction.id = currentId++;
	}

	/**
	 * Constructor.
	 * @param description The description
	 * @param comment The comment associated with the transaction
	 * @param amount The amount (negative for expenses)
	 * @param account The account
	 * @param mode the payment mode
	 * @param category the category
	 * @param subTransactions a subtransactions list or null
	 */
	public AbstractTransaction(String description, String comment, double amount, Account account, Mode mode, Category category, List subTransactions) {
		super();
		if ((mode==null) || (category==null) || (description==null)) {
			throw new IllegalArgumentException();
		}
		this.description = getCachedDescription(description);
		if ((comment!=null) && (comment.isEmpty())) {
			comment = null;
		}
		this.comment = comment;
		this.amount = amount;
		this.account = account;
		this.mode = mode;
		this.category = category;
		if (subTransactions!=null) {
			this.subTransactions = subTransactions;
		} else {
			this.subTransactions = Collections.emptyList();
		}
		setId(this);
	}
	
	// The following lines are a test to implement a description cache in order to prevent from duplicating same description into memory.
	// In real life, it seems to not have a positive impact (HashMap size is greater than the saved String memory footprint).
	//	private static final WeakHashMap descriptionCache = new WeakHashMap();
	private String getCachedDescription(String description) {
		return description;
//		String result = descriptionCache.get(description);
//		if (result == null) {
//			result = description;
//			descriptionCache.put(description, description);
//		}
//		return result;
	}

	@Override
	public Object clone() {
		AbstractTransaction result = null;
		try {
			result = (AbstractTransaction) super.clone();
			result.subTransactions = new ArrayList();
			for (int i=0;i changeSubTransactions(Category oldCategory, Category newCategory) {
		List subTransactions = new ArrayList(getSubTransactionSize());
		for (int i = 0; i < getSubTransactionSize(); i++) {
			SubTransaction sub = getSubTransaction(i);
			subTransactions.add(sub.getCategory().equals(oldCategory) ?
					new SubTransaction(sub.getAmount(), sub.getDescription(), newCategory) : sub);
		}
		return subTransactions;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy