1 2 Previous Next 17 Replies Latest reply on Jan 21, 2004 10:03 AM by 807582 Go to original post
      • 15. Re: number of instances
        807582
        could instanceOf method be any help in this context?
        static int i;
        
        if (someObject  instanceOf  Class)
        {
         i++;
        }
        Correct me if i am wrong
        • 16. Re: number of instances
          807582
          A cleaner option:


          import java.util.Hashtable;

          public class InstanceCounterManager {
               private final static Hashtable counterTable = new Hashtable();

               /**
               * Registers a new instance.
               *
               * @param aClass The class of the instance.
               * @return The number of instances of that class.
               */
               public static int register(Class aClass) {
                    int i = 0;
                    if (aClass != null) {
                         i = 1;
                         synchronized (aClass) {
                              Integer counter = (Integer) counterTable.remove(aClass);
                              if (counter != null) {
                                   i = counter.intValue() + 1;
                              }
                              counter = new Integer(i);
                              counterTable.put(aClass, counter);
                         }
                    }
                    return i;
               }

               /**
               * Unregisters an instance.
               *
               * @param o An instance.
               */
               public static void unregister(Object o) {
                    int i = 0;
          int j = 0;
                    if (o != null) {
                         Class classArray[] = o.getClass().getClasses();
          for(int l = classArray.length; j < l; j++) {
          Class c = classArray[j];
          synchronized (c) {
          Integer counter = (Integer) counterTable.remove(c);
          if (counter != null) {
          i = counter.intValue() - 1;
          if (i > 0) {
          counter = new Integer(i);
          counterTable.put(c, counter);
          }
          }
          }
          }
                    }
               }

          /**
          * Gets the number of instances of class c.
          *
          * @param c A class
          * @return The number of instances of class c.
          */
          public static int getNumberOfInstances(Class c) {
          int i = 0;
          Integer counter = (Integer) counterTable.get(c);
          if (counter != null) {
          i = counter.intValue();
          }
          return i;
          }
          }

          // Class whose instances must be registered:
          class YourClass {
          public YourClass(/*...*/) {
          InstanceCounterManager.register(YourClass.class);
          /**
          * ...
          */
          }

          /**
          * ...
          */

          protected void finalize() throws Throwable {
          InstanceCounterManager.unregister(this);
          }
          }
          • 17. Re: number of instances
            807582
            Excuse me for the formatting error:
            import java.util.Hashtable;
            
            public class InstanceCounterManager {
                 private final static Hashtable counterTable = new Hashtable();
            
                 /**
                  *  Registers a new instance.
                  *
                  * @param  aClass  The class of the instance.
                  * @return         The number of instances of that class.
                  */
                 public static int register(Class aClass) {
                      int i = 0;
                      if (aClass != null) {
                           i = 1;
                           synchronized (aClass) {
                                Integer counter = (Integer) counterTable.remove(aClass);
                                if (counter != null) {
                                     i = counter.intValue() + 1;
                                }
                                counter = new Integer(i);
                                counterTable.put(aClass, counter);
                           }
                      }
                      return i;
                 }
            
                 /**
                  *  Unregisters an instance.
                  *
                  * @param  o  An instance.
                  */
                 public static void unregister(Object o) {
                      int i = 0;
                    int j = 0;
                      if (o != null) {
                           Class classArray[] = o.getClass().getClasses();
                        for(int l = classArray.length; j < l; j++) {
                            Class c = classArray[j];
                            synchronized (c) {
                                Integer counter = (Integer) counterTable.remove(c);
                                if (counter != null) {
                                    i = counter.intValue() - 1;
                                    if (i > 0) {
                                        counter = new Integer(i);
                                        counterTable.put(c, counter);
                                    }
                                }
                            }
                        }
                      }
                 }
            
                /**
                 *  Gets the number of instances of class c.
                 *
                 * @param c     A class
                 * @return      The number of instances of class c.
                 */
                public static int getNumberOfInstances(Class c) {
                    int i = 0;
                    Integer counter = (Integer) counterTable.get(c);
                    if (counter != null) {
                        i = counter.intValue();
                    }
                    return i;
                }
            }
            
            // Class whose instances must be registered:
            class YourClass {
                public YourClass(/*...*/) {
                    InstanceCounterManager.register(YourClass.class);
                    /**
                     *  ...
                     */
                }
            
                /**
                 *  ...
                 */
            
                protected void finalize() throws Throwable {
                    InstanceCounterManager.unregister(this);
                }
            }
            1 2 Previous Next