View Javadoc
1   /*
2    *  jDTAUS Banking SPI
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.banking.dtaus.spi;
22  
23  import java.io.Serializable;
24  import java.util.Currency;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.Set;
30  
31  /**
32   * Utility class for maintaining a mapping of currencies to corresponding counters.
33   *
34   * <p><b>Note:</b><br/>This class is not synchronized and must not be used concurrently without external
35   * synchronization.</p>
36   *
37   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
38   * @version $JDTAUS: CurrencyCounter.java 8661 2012-09-27 11:29:58Z schulte $
39   */
40  public class CurrencyCounter implements Serializable
41  {
42  
43      /** Serial version UID for backwards compatibility with 1.1.x classes. */
44      private static final long serialVersionUID = -2765988202184349786L;
45  
46      /**
47       * Maps ISO currency codes to counters.
48       * @serial
49       */
50      private Map currencyMap;
51  
52      /**
53       * Gets the currencies for which the instance holds counters.
54       *
55       * @return The currencies for which the instance holds counters.
56       */
57      public Currency[] getCurrencies()
58      {
59          final Set currencies = new HashSet( this.getCurrencyMap().size() );
60          for ( Iterator it = this.getCurrencyMap().entrySet().iterator(); it.hasNext(); )
61          {
62              final Map.Entry entry = (Map.Entry) it.next();
63              currencies.add( Currency.getInstance( (String) entry.getKey() ) );
64          }
65  
66          return (Currency[]) currencies.toArray( new Currency[ currencies.size() ] );
67      }
68  
69      /**
70       * Gets the value of the counter for a given {@code Currency}.
71       *
72       * @param currency The currency to return the value of the counter for.
73       *
74       * @return The value of the counter of {@code currency}.
75       *
76       * @throws NullPointerException if {@code currency} is {@code null}.
77       */
78      public long getValue( final Currency currency )
79      {
80          if ( currency == null )
81          {
82              throw new NullPointerException( "currency" );
83          }
84  
85          final Long value = (Long) this.getCurrencyMap().get( currency.getCurrencyCode() );
86          return value != null ? value.longValue() : 0L;
87      }
88  
89      /**
90       * Adds to the counter of a currency.
91       *
92       * @param currency The currency to add to.
93       *
94       * @throws NullPointerException if {@code currency} is {@code null}.
95       * @throws IndexOutOfBoundsException if the value of the counter of {@code currency} is equal to
96       * {@link Long#MAX_VALUE}.
97       */
98      public void add( final Currency currency )
99      {
100         if ( currency == null )
101         {
102             throw new NullPointerException( "currency" );
103         }
104 
105         final long value = this.getValue( currency );
106 
107         if ( value == Long.MAX_VALUE )
108         {
109             throw new IndexOutOfBoundsException();
110         }
111 
112         this.getCurrencyMap().put( currency.getCurrencyCode(), new Long( value + 1L ) );
113     }
114 
115     /**
116      * Substracts from the counter of a currency.
117      *
118      * @param currency The currency to substract from.
119      *
120      * @throws NullPointerException if {@code currency} is {@code null}.
121      * @throws IndexOutOfBoundsException if the value of the counter of {@code currency} is equal to zero.
122      */
123     public void substract( final Currency currency )
124     {
125         if ( currency == null )
126         {
127             throw new NullPointerException( "currency" );
128         }
129 
130         final long value = this.getValue( currency );
131 
132         if ( value == 0L )
133         {
134             throw new IndexOutOfBoundsException();
135         }
136 
137         this.getCurrencyMap().put( currency.getCurrencyCode(), new Long( value - 1L ) );
138     }
139 
140     /**
141      * Gets the {@code Map} backing the instance.
142      *
143      * @return The {@code Map} backing the instance.
144      */
145     private Map getCurrencyMap()
146     {
147         if ( this.currencyMap == null )
148         {
149             this.currencyMap = new HashMap( 10 );
150         }
151 
152         return this.currencyMap;
153     }
154 
155 }