home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / Point.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  5.6 KB  |  229 lines  |  [TEXT/CWIE]

  1. // Point.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. /** Object subclass representing an (x, y) coordinate.
  10.   */
  11.  
  12. public class Point implements Codable {
  13.     /** The Point's X-coordinate. */
  14.     public int                  x;
  15.     /** The Point's Y-coordinate. */
  16.     public int                  y;
  17.  
  18.     static private Vector       _pointCache = new Vector();
  19.     static private boolean      _cachePoints = true;
  20.  
  21.     static final String         X_KEY = "x";
  22.     static final String         Y_KEY = "y";
  23.  
  24.  
  25.  
  26. /* constructors */
  27.  
  28.     /** Constructs a Point with coordinates (0, 0).
  29.       */
  30.     public Point() {
  31.     }
  32.  
  33.     /** Constructs a Point with coordinates (<b>x</b>, <b>y</b>).
  34.       */
  35.     public Point(int x, int y) {
  36.         this.x = x;
  37.         this.y = y;
  38.     }
  39.  
  40.     /** Constructs a Point with coordinates
  41.       * (<b>templatePoint.x</b>, <b>templatePoint.y</b>).
  42.       */
  43.     public Point(Point templatePoint) {
  44.         x = templatePoint.x;
  45.         y = templatePoint.y;
  46.     }
  47.  
  48.  
  49. /* actions */
  50.  
  51.  
  52.     /** Returns the Point's String representation.
  53.       */
  54.     public String toString() {
  55.         return "(" + x + ", " + y + ")";
  56.     }
  57.  
  58.     /** Sets the Point's coordinates to (<b>x</b>, <b>y</b>).
  59.       */
  60.     public void moveTo(int x, int y) {
  61.         this.x = x;
  62.         this.y = y;
  63.     }
  64.  
  65.     /** Moves the Point by (<b>deltaX</b>, <b>deltaY</b>).
  66.       */
  67.     public void moveBy(int deltaX, int deltaY) {
  68.         x += deltaX;
  69.         y += deltaY;
  70.     }
  71.  
  72.     /** Returns <b>true</b> if the Point equals <b>anObject</b>.
  73.       */
  74.     public boolean equals(Object anObject) {
  75.         Point aPoint;
  76.  
  77.         if (!(anObject instanceof Point))
  78.             return false;
  79.  
  80.         aPoint = (Point)anObject;
  81.         return (aPoint.x == x && aPoint.y == y);
  82.     }
  83.  
  84.     /** Returns the Point's hash code.
  85.       */
  86.     public int hashCode() {
  87.         // ALERT!
  88.         // This is an arbitrarily choosen hash implementation.
  89.         // There should be a better one for points.
  90.         return x ^ y;
  91.     }
  92.  
  93.  
  94.  
  95. /* archiving */
  96.  
  97.  
  98.     /** Describes the Point class' coding information.
  99.      * @see Codable#describeClassInfo
  100.      */
  101.     public void describeClassInfo(ClassInfo info) {
  102.         info.addClass("netscape.application.Point", 1);
  103.         info.addField(X_KEY, INT_TYPE);
  104.         info.addField(Y_KEY, INT_TYPE);
  105.     }
  106.  
  107.     /** Encodes the Point.
  108.      * @see Codable#encode
  109.      */
  110.     public void encode(Encoder encoder) throws CodingException {
  111.         encoder.encodeInt(X_KEY, x);
  112.         encoder.encodeInt(Y_KEY, y);
  113.     }
  114.  
  115.     /** Decodes the Point.
  116.      * @see Codable#decode
  117.      */
  118.     public void decode(Decoder decoder) throws CodingException {
  119.         x = decoder.decodeInt(X_KEY);
  120.         y = decoder.decodeInt(Y_KEY);
  121.     }
  122.  
  123.     /** Finishes the Point decoding.
  124.      * @see Codable#finishDecoding
  125.      */
  126.     public void finishDecoding() throws CodingException {
  127.     }
  128.  
  129.  
  130.  
  131. /* point cache */
  132.  
  133.  
  134.     /** Returns a Point from the Point cache with coordinates
  135.       * (<b>x</b>, <b>y</b>).  Creates a new Point if the cache is empty.
  136.       * @private
  137.       */
  138.     static Point newPoint(int x, int y) {
  139.         Point   thePoint;
  140.  
  141.         synchronized(_pointCache) {
  142.             if (!_cachePoints || _pointCache.isEmpty()) {
  143.                 return new Point(x, y);
  144.             }
  145.  
  146.             thePoint = (Point)_pointCache.removeLastElement();
  147.         }
  148.         thePoint.moveTo(x, y);
  149.  
  150.         return thePoint;
  151.     }
  152.  
  153.     /** Returns a Point from the Point cache with the same coordinates as
  154.       * <b>templatePoint</b>.  Creates a new Point if the cache is empty.
  155.       * @private
  156.       */
  157.     static Point newPoint(Point templatePoint) {
  158.         if (templatePoint == null) {
  159.             return Point.newPoint(0, 0);
  160.         } else {
  161.             return Point.newPoint(templatePoint.x, templatePoint.y);
  162.         }
  163.     }
  164.  
  165.     /** Returns a Point from the Point cache with coordinates (0, 0).
  166.       * Equivalent to the code:
  167.       * <pre>
  168.       *     aPoint = Point.newPoint(0, 0);
  169.       * </pre>
  170.       * Creates a new Point if the cache is empty.
  171.       * @private
  172.       */
  173.     static Point newPoint() {
  174.         return Point.newPoint(0, 0);
  175.     }
  176.  
  177.     /** Places <b>aPoint</b> back in the Point cache (if the cache is not full).
  178.       * @private
  179.       */
  180.     static void returnPoint(Point aPoint) {
  181.         if (!_cachePoints) {
  182.             return;
  183.         }
  184.  
  185.         synchronized(_pointCache) {
  186.             if (_pointCache.count() < 30) {
  187.                 _pointCache.addElement(aPoint);
  188.             }
  189.         }
  190.     }
  191.  
  192.     /** Places the Points contained in <b>points</b> back in the Point cache
  193.       * (if the cache is not full) and empties the Vector.
  194.       * @private
  195.       */
  196.     static void returnPoints(Vector points) {
  197.         int     i;
  198.  
  199.         if (points == null || !_cachePoints) {
  200.             return;
  201.         }
  202.  
  203.         i = points.count();
  204.         while (i-- > 0) {
  205.             Point.returnPoint((Point)points.elementAt(i));
  206.         }
  207.  
  208.         points.removeAllElements();
  209.     }
  210.  
  211.     /** Enables or disables Point caching.  With setShouldCachePoints(false),
  212.       * Point.newPoint() methods create new Point instances and
  213.       * Point.returnPoint() methods do nothing with the Points they're given.
  214.       * Disabling Point caching can help you track down problems in your code
  215.       * where you return Points to the cache but accidentally continue to
  216.       * maintain references to them.
  217.       * @private
  218.       */
  219.     static void setShouldCachePoints(boolean flag) {
  220.         synchronized(_pointCache) {
  221.             _cachePoints = flag;
  222.  
  223.             if (!_cachePoints) {
  224.                 _pointCache.removeAllElements();
  225.             }
  226.         }
  227.     }
  228. }
  229.