An integer synchronized counter class. : Number « Data Type « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Data Type » Number 
2. 13. 5. An integer synchronized counter class.
/*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This 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 software 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 software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package org.jboss.util;

import java.io.Serializable;

/**
 * An integer counter class.
 *
 @version <tt>$Revision: 2800 $</tt>
 @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
 */
public class Counter
   implements Serializable, Cloneable
{
   /** The serialVersionUID */
   private static final long serialVersionUID = 7736259185393081556L;
   /** The current count */
   private int count;

   /**
    * Construct a Counter with a starting value.
    *
    @param count   Starting value for counter.
    */
   public Counter(final int count) {
      this.count = count;
   }

   /**
    * Construct a Counter.
    */
   public Counter() {}

   /**
    * Increment the counter. (Optional operation)
    *
    @return  The incremented value of the counter.
    */
   public int increment() {
      return ++count;
   }

   /**
    * Decrement the counter. (Optional operation)
    *
    @return  The decremented value of the counter.
    */
   public int decrement() {
      return --count;
   }

   /**
    * Return the current value of the counter.
    *
    @return  The current value of the counter.
    */
   public int getCount() {
      return count;
   }

   /**
    * Reset the counter to zero. (Optional operation)
    */
   public void reset() {
      this.count = 0;
   }

   /**
    * Check if the given object is equal to this.
    *
    @param obj  Object to test equality with.
    @return     True if object is equal to this.
    */
   public boolean equals(final Object obj) {
      if (obj == thisreturn true;

      if (obj != null && obj.getClass() == getClass()) {
         return ((Counter)obj).count == count;
      }
      
      return false;
   }

   /**
    * Return a string representation of this.
    *
    @return  A string representation of this.
    */
   public String toString() {
      return String.valueOf(count);
   }

   /**
    * Return a cloned copy of this object.
    *
    @return  A cloned copy of this object.
    */
   public Object clone() {
      try {
         return super.clone();
      }
      catch (CloneNotSupportedException e) {
         throw new InternalError();
      }
   }


   /////////////////////////////////////////////////////////////////////////
   //                                Wrappers                             //
   /////////////////////////////////////////////////////////////////////////

   /**
    * Base wrapper class for other wrappers.
    */
   private static class Wrapper
      extends Counter
   {
      /** The serialVersionUID */
      private static final long serialVersionUID = -1803971437884946242L;
      /** The wrapped counter */
      protected final Counter counter;

      public Wrapper(final Counter counter) {
         this.counter = counter;
      }

      public int increment() {
         return counter.increment();
      }

      public int decrement() {
         return counter.decrement();
      }

      public int getCount() {
         return counter.getCount();
      }

      public void reset() {
         counter.reset();
      }

      public boolean equals(final Object obj) {
         return counter.equals(obj);
      }

      public String toString() {
         return counter.toString();
      }

      public Object clone() {
         return counter.clone();
      }
   }

   /**
    * Return a synchronized counter.
    *
    @param counter    Counter to synchronize.
    @return           Synchronized counter.
    */
   public static Counter makeSynchronized(final Counter counter) {
      return new Wrapper(counter) {
            /** The serialVersionUID */
         private static final long serialVersionUID = -6024309396861726945L;

            public synchronized int increment() {
               return this.counter.increment();
            }

            public synchronized int decrement() {
               return this.counter.decrement();
            }

            public synchronized int getCount() {
               return this.counter.getCount();
            }

            public synchronized void reset() {
               this.counter.reset();
            }

            public synchronized int hashCode() {
               return this.counter.hashCode();
            }

            public synchronized boolean equals(final Object obj) {
               return this.counter.equals(obj);
            }

            public synchronized String toString() {
               return this.counter.toString();
            }

            public synchronized Object clone() {
               return this.counter.clone();
            }
         };
   }

   /**
    * Returns a directional counter.
    *
    @param counter       Counter to make directional.
    @param increasing    True to create an increasing only
    *                      or false to create a decreasing only.
    @return              A directional counter.
    */
   public static Counter makeDirectional(final Counter counter,
                                         final boolean increasing)
   {
      Counter temp;
      if (increasing) {
         temp = new Wrapper(counter) {
               /** The serialVersionUID */
            private static final long serialVersionUID = 2161377898611431781L;

               public int decrement() {
                  throw new UnsupportedOperationException();
               }

               public void reset() {
                  throw new UnsupportedOperationException();
               }
            };
      }
      else {
         temp = new Wrapper(counter) {
            /** The serialVersionUID */
            private static final long serialVersionUID = -4683457706354663230L;
               public int increment() {
                  throw new UnsupportedOperationException();
               }
            };
      }
      
      return temp;
   }
}
2. 13. Number
2. 13. 1. Check Number properties and convert from Number
2. 13. 2. Turns a string value into a java.lang.Number.
2. 13. 3. Fraction is a Number implementation that stores fractions accurately.
2. 13. 4. Represents a range of Number objects.
2. 13. 5. An integer synchronized counter class.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.