A long integer counter class : Debug « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
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 » Development Class » DebugScreenshots 
A long integer 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.
 */

import java.io.Serializable;

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

  /** The current count */
  private long count;

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

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

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

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

  /**
   * Return the current value of the counter.
   
   @return The current value of the counter.
   */
  public long 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 == this)
      return true;

    if (obj != null && obj.getClass() == getClass()) {
      return ((LongCounterobj).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 LongCounter {
    /** The serialVersionUID */
    private static final long serialVersionUID = 6720507617603163762L;

    /** The wrapped counter */
    protected final LongCounter counter;

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

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

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

    public long 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
   *          LongCounter to synchronize.
   @return Synchronized counter.
   */
  public static LongCounter makeSynchronized(final LongCounter counter) {
    return new Wrapper(counter) {
      /** The serialVersionUID */
      private static final long serialVersionUID = 8903330696503363758L;

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

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

      public synchronized long 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
   *          LongCounter to make directional.
   @param increasing
   *          True to create an increasing only or false to create a decreasing
   *          only.
   @return A directional counter.
   */
  public static LongCounter makeDirectional(final LongCounter counter, final boolean increasing) {
    LongCounter temp;
    if (increasing) {
      temp = new Wrapper(counter) {
        /** The serialVersionUID */
        private static final long serialVersionUID = -8902748795144754375L;

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

        public void reset() {
          throw new UnsupportedOperationException();
        }
      };
    else {
      temp = new Wrapper(counter) {
        /** The serialVersionUID */
        private static final long serialVersionUID = 2584758778978644599L;

        public long increment() {
          throw new UnsupportedOperationException();
        }
      };
    }

    return temp;
  }
}

   
    
    
  
Related examples in the same category
1. A simple logging facility.
2. Debug Utilities
3. Debug InputStream
4. Methods for printing Debug messages
5. Trace InputStream
6. Trace OutputStream
7. Debug Utility
8. Debugging utility that reports, in a brute force manner, any internal data of a class instance
9. Swing Console
10. How to do Benchmark
11. Methods for logging events
12. Printing indented text
13. Prints messages formatted for a specific line width.
14. Class providing static methods to log diagnostics
15. A bean that can be used to keep track of a counter
16. An integer synchronized counter class.
17. Counts down from a specified value the number of bytes actually read from the wrapped InputStream.
18. Logging class to record errors or unexpected behavior to a file
19. Handle obtaining string timestamps
20. Scans java source files in cvs tree and validates the license header
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.