home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Utility.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  155 lines

  1. /*
  2.  * @(#)Utility.java    1.3 97/01/27
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30. package java.text;
  31.  
  32. final class Utility {
  33.  
  34.     /**
  35.      * Convenience utility to compare two Object[]s.
  36.      * Ought to be in System
  37.      */
  38.     final static boolean arrayEquals(Object[] source, Object target) {
  39.         if (source == null) return (target == null);
  40.         if (!(target instanceof Object[])) return false;
  41.         Object[] targ = (Object[]) target;
  42.         return (source.length == targ.length
  43.                 && arrayRegionMatches(source, 0, targ, 0, source.length));
  44.     }
  45.  
  46.     /**
  47.      * Convenience utility to compare two int[]s
  48.      * Ought to be in System
  49.      */
  50.     final static boolean arrayEquals(int[] source, Object target) {
  51.         if (source == null) return (target == null);
  52.         if (!(target instanceof int[])) return false;
  53.         int[] targ = (int[]) target;
  54.         return (source.length == targ.length
  55.                 && arrayRegionMatches(source, 0, targ, 0, source.length));
  56.     }
  57.  
  58.     /**
  59.      * Convenience utility to compare two double[]s
  60.      * Ought to be in System
  61.      */
  62.     final static boolean arrayEquals(double[] source, Object target) {
  63.         if (source == null) return (target == null);
  64.         if (!(target instanceof double[])) return false;
  65.         double[] targ = (double[]) target;
  66.         return (source.length == targ.length
  67.                 && arrayRegionMatches(source, 0, targ, 0, source.length));
  68.     }
  69.  
  70.     /**
  71.      * Convenience utility to compare two Object[]s
  72.      * Ought to be in System
  73.      */
  74.     final static boolean arrayEquals(Object source, Object target) {
  75.         if (source == null) return (target == null);
  76.         // for some reason, the correct arrayEquals is not being called
  77.         // so do it by hand for now.
  78.         if (source instanceof Object[])
  79.             return(arrayEquals((Object[]) source,target));
  80.         if (source instanceof int[])
  81.             return(arrayEquals((int[]) source,target));
  82.         if (source instanceof double[])
  83.             return(arrayEquals((int[]) source,target));
  84.         return source.equals(target);
  85.     }
  86.  
  87.     /**
  88.      * Convenience utility to compare two Object[]s
  89.      * Ought to be in System.
  90.      * @param len the length to compare.
  91.      * The start indices and start+len must be valid.
  92.      */
  93.     final static boolean arrayRegionMatches(Object[] source, int sourceStart,
  94.                                             Object[] target, int targetStart,
  95.                                             int len)
  96.     {
  97.         int sourceEnd = sourceStart + len;
  98.         int delta = targetStart - sourceStart;
  99.         for (int i = sourceStart; i < sourceEnd; i++) {
  100.             if (!arrayEquals(source[i],target[i + delta]))
  101.              return false;
  102.         }
  103.         return true;
  104.     }
  105.  
  106.     /**
  107.      * Convenience utility to compare two int[]s.
  108.      * @param len the length to compare.
  109.      * The start indices and start+len must be valid.
  110.      * Ought to be in System
  111.      */
  112.     final static boolean arrayRegionMatches(int[] source, int sourceStart,
  113.                                             int[] target, int targetStart,
  114.                                             int len)
  115.     {
  116.         int sourceEnd = sourceStart + len;
  117.         int delta = targetStart - sourceStart;
  118.         for (int i = sourceStart; i < sourceEnd; i++) {
  119.             if (source[i] != target[i + delta])
  120.              return false;
  121.         }
  122.         return true;
  123.     }
  124.  
  125.     /**
  126.      * Convenience utility to compare two arrays of doubles.
  127.      * @param len the length to compare.
  128.      * The start indices and start+len must be valid.
  129.      * Ought to be in System
  130.      */
  131.     final static boolean arrayRegionMatches(double[] source, int sourceStart,
  132.                                             double[] target, int targetStart,
  133.                                             int len)
  134.     {
  135.         int sourceEnd = sourceStart + len;
  136.         int delta = targetStart - sourceStart;
  137.         for (int i = sourceStart; i < sourceEnd; i++) {
  138.             if (source[i] != target[i + delta])
  139.              return false;
  140.         }
  141.         return true;
  142.     }
  143.  
  144.     /**
  145.      * Convenience utility. Does null checks on objects, then calls equals.
  146.      */
  147.     final static boolean objectEquals(Object source, Object target) {
  148.        if (source == null)
  149.             return (target == null);
  150.        else
  151.             return arrayEquals(source,target);
  152.     }
  153. }
  154.  
  155.