001    /* =========================================================
002     * JAMEL : a Java (tm) Agent-based MacroEconomic Laboratory.
003     * =========================================================
004     *
005     * (C) Copyright 2007-2010, Pascal Seppecher.
006     * 
007     * Project Info <http://p.seppecher.free.fr/jamel/>. 
008     *
009     * This file is a part of JAMEL (Java Agent-based MacroEconomic Laboratory).
010     * 
011     * JAMEL is free software: you can redistribute it and/or modify
012     * it under the terms of the GNU General Public License as published by
013     * the Free Software Foundation, either version 3 of the License, or
014     * (at your option) any later version.
015     * 
016     * JAMEL is distributed in the hope that it will be useful,
017     * but WITHOUT ANY WARRANTY; without even the implied warranty of
018     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019     * GNU General Public License for more details.
020     * 
021     * You should have received a copy of the GNU General Public License
022     * along with JAMEL. If not, see <http://www.gnu.org/licenses/>.
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025     * in the United States and other countries.]
026     */
027    
028    package jamel.markets;
029    
030    import jamel.agents.roles.Offerer;
031    
032    
033    /**
034     * A base class for offers on markets.
035     * <p>
036     * Encapsulates a volume offered and a unit price.
037     * <p>
038     * Last update: 8-Dec-2010.
039     */
040    public abstract class AbstractOffer implements Comparable<AbstractOffer> {
041    
042            /** The agent author of the offer. */
043            private Offerer author ;        
044    
045            /** The unit price. */
046            private final double price ;    
047    
048            /** The offered volume. */
049            private int volume ;
050    
051            /**
052             * Creates a new offer.
053             * @param offerer the agent author of the offer.
054             * @param volume the offered volume.
055             * @param price the unit price of the offer.
056             */
057            protected AbstractOffer(Offerer offerer, int volume, double price) {
058                    if (Double.isNaN(price)) 
059                            throw new RuntimeException("The price is NaN.") ;
060                    if (price == 0) throw new RuntimeException("The price can't be null.") ;
061                    if (volume <= 0) throw new RuntimeException("The volume must be strictly positive.") ;
062                    this.author = offerer ;                         
063                    this.volume = volume ;                                  
064                    this.price = price ;
065            }
066    
067            /**
068             * Returns the unit price.
069             * @return a double that represents the unit price.
070             */
071            protected double getPrice() { 
072                    return this.price ;
073            }
074    
075            /**
076             * Subtract a volume to the offered volume.<br>
077             * @param volume the volume to subtract.
078             */
079            public void subtract(double volume) {
080                    if (volume>this.volume) throw new IllegalArgumentException("The volume to subtract exceed the offered volume.") ;
081                    this.volume -= volume ;
082            }
083    
084            /**
085             * Returns the agent author of the offer.
086             * @return the offerer.
087             */
088            protected Offerer getOfferer() { 
089                    return this.author ; 
090            }
091    
092            /**
093             * Returns the offered volume.
094             * @return a double that represents the volume.
095             */
096            public int getVolume() { 
097                    return this.volume ; 
098            }
099    
100            /**
101             * 
102             */
103            public void clear() {
104                    this.author = null;             
105            }
106    
107    }