001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package net.tridentsdk.service; 018 019import javax.annotation.concurrent.ThreadSafe; 020import java.util.concurrent.atomic.AtomicInteger; 021 022/** 023 * Represents a transaction of a type between supported entities, including players via the 024 * {@link Transactions}, and the callback which is executed for the transaction to occur 025 * 026 * @author The TridentSDK Team 027 * @since 0.3-alpha-DP 028 */ 029@ThreadSafe 030public abstract class Transaction<S, R> { 031 private final Object item; 032 private final S sender; 033 private final R receiver; 034 final AtomicInteger amount = new AtomicInteger(); 035 036 /** 037 * Provides the construction support for transactions 038 * 039 * @param item the item type to transact 040 * @param sender the sender of the transaction. The person being withdrawn from in a withdrawl. 041 * @param receiver the receiver of the transaction 042 * @param amount the amount to operate upon 043 */ 044 public Transaction(Object item, S sender, R receiver, int amount) { 045 this.item = item; 046 this.sender = sender; 047 this.receiver = receiver; 048 this.amount.set(amount); 049 } 050 051 /** 052 * Creates a new transaction that does not have a callback 053 * 054 * @param item the item type to transact 055 * @param sender the sender of the transaction. The person being withdrawn from in a withdrawl. 056 * @param receiver the receiver of the transaction 057 * @param amount the amount to operate upon. Do not make negative for withdrawls. 058 * @return the new transaction 059 */ 060 public static <S, R> Transaction<S, R> quietTransaction(Object item, S sender, R receiver, int amount) { 061 return new Transaction<S, R>(item, sender, receiver, amount) { 062 @Override 063 public void doTransaction(Type type) { 064 } 065 }; 066 } 067 068 /** 069 * The callback after the transaction completes. Use this to send messages or send physical items. 070 * 071 * @param type the type of transaction occuring 072 */ 073 public abstract void doTransaction(Type type); 074 075 /** 076 * The item type being transacted 077 * 078 * @return the transacted item 079 */ 080 public Object item() { 081 return this.item; 082 } 083 084 /** 085 * The sender of the transaction 086 * 087 * @return the transaction's sender 088 */ 089 public S sender() { 090 return this.sender; 091 } 092 093 /** 094 * The receiver of the transaction 095 * 096 * @return the transaction's receiver 097 */ 098 public R receiver() { 099 return this.receiver; 100 } 101 102 /** 103 * The amount of this transaction, always supposed to be positive. 104 * 105 * <p>This returns the absolute value of the amount, in case a withdrawl is thought to have a negative number.</p> 106 * 107 * @return the amount of the transaction 108 */ 109 public int amount() { 110 return this.amount.get(); 111 } 112 113 /** 114 * The transaction type 115 */ 116 public enum Type { 117 /** 118 * The transaction represents a deposit 119 */ 120 DEPOSIT, 121 /** 122 * The transaction represents a withdrawl 123 */ 124 WITHDRAW 125 } 126}