📄 utility.java
字号:
/* * * @(#)Utility.java 1.17 06/10/10 * * Portions Copyright 2000-2008 Sun Microsystems, Inc. All Rights * Reserved. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program 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 * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//* * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved * * The original version of this source code and documentation * is copyrighted and owned by Taligent, Inc., a wholly-owned * subsidiary of IBM. These materials are provided under terms * of a License Agreement between Taligent and Sun. This technology * is protected by multiple US and International patents. * * This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */package sun.text;public final class Utility { /** * Convenience utility to compare two Object[]s. * Ought to be in System */ public final static boolean arrayEquals(Object[] source, Object target) { if (source == null) return (target == null); if (!(target instanceof Object[])) return false; Object[] targ = (Object[]) target; return (source.length == targ.length && arrayRegionMatches(source, 0, targ, 0, source.length)); } /** * Convenience utility to compare two int[]s * Ought to be in System */ public final static boolean arrayEquals(int[] source, Object target) { if (source == null) return (target == null); if (!(target instanceof int[])) return false; int[] targ = (int[]) target; return (source.length == targ.length && arrayRegionMatches(source, 0, targ, 0, source.length)); } /** * Convenience utility to compare two double[]s * Ought to be in System */ public final static boolean arrayEquals(double[] source, Object target) { if (source == null) return (target == null); if (!(target instanceof double[])) return false; double[] targ = (double[]) target; return (source.length == targ.length && arrayRegionMatches(source, 0, targ, 0, source.length)); } /** * Convenience utility to compare two Object[]s * Ought to be in System */ public final static boolean arrayEquals(Object source, Object target) { if (source == null) return (target == null); // for some reason, the correct arrayEquals is not being called // so do it by hand for now. if (source instanceof Object[]) return(arrayEquals((Object[]) source,target)); if (source instanceof int[]) return(arrayEquals((int[]) source,target)); if (source instanceof double[]) return(arrayEquals((double[]) source,target)); return source.equals(target); } /** * Convenience utility to compare two Object[]s * Ought to be in System. * @param len the length to compare. * The start indices and start+len must be valid. */ public final static boolean arrayRegionMatches(Object[] source, int sourceStart, Object[] target, int targetStart, int len) { int sourceEnd = sourceStart + len; int delta = targetStart - sourceStart; for (int i = sourceStart; i < sourceEnd; i++) { if (!arrayEquals(source[i],target[i + delta])) return false; } return true; } /** * Convenience utility to compare two int[]s. * @param len the length to compare. * The start indices and start+len must be valid. * Ought to be in System */ public final static boolean arrayRegionMatches(int[] source, int sourceStart, int[] target, int targetStart, int len) { int sourceEnd = sourceStart + len; int delta = targetStart - sourceStart; for (int i = sourceStart; i < sourceEnd; i++) { if (source[i] != target[i + delta]) return false; } return true; } /** * Convenience utility to compare two arrays of doubles. * @param len the length to compare. * The start indices and start+len must be valid. * Ought to be in System */ public final static boolean arrayRegionMatches(double[] source, int sourceStart, double[] target, int targetStart, int len) { int sourceEnd = sourceStart + len; int delta = targetStart - sourceStart; for (int i = sourceStart; i < sourceEnd; i++) { if (source[i] != target[i + delta]) return false; } return true; } /** * Convenience utility. Does null checks on objects, then calls equals. */ public final static boolean objectEquals(Object source, Object target) { if (source == null) return (target == null); else return source.equals(target); } /** * The ESCAPE character is used during run-length encoding. It signals * a run of identical chars. */ static final char ESCAPE = '\uA5A5'; /** * The ESCAPE_BYTE character is used during run-length encoding. It signals * a run of identical bytes. */ static final byte ESCAPE_BYTE = (byte)0xA5; /** * Construct a string representing a short array. Use run-length encoding. * A character represents itself, unless it is the ESCAPE character. Then * the following notations are possible: * ESCAPE ESCAPE ESCAPE literal * ESCAPE n c n instances of character c * Since an encoded run occupies 3 characters, we only encode runs of 4 or * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. * If we encounter a run where n == ESCAPE, we represent this as: * c ESCAPE n-1 c * The ESCAPE value is chosen so as not to collide with commonly * seen values. */ public static final String arrayToRLEString(short[] a) { StringBuffer buffer = new StringBuffer(); // for (int i=0; i<a.length; ++i) buffer.append((char) a[i]); buffer.append((char) (a.length >> 16)); buffer.append((char) a.length); short runValue = a[0]; int runLength = 1; for (int i=1; i<a.length; ++i) { short s = a[i]; if (s == runValue && runLength < 0xFFFF) { ++runLength; } else { encodeRun(buffer, runValue, runLength); runValue = s; runLength = 1; } } encodeRun(buffer, runValue, runLength); return buffer.toString(); } /** * Construct a string representing a byte array. Use run-length encoding. * Two bytes are packed into a single char, with a single extra zero byte at * the end if needed. A byte represents itself, unless it is the * ESCAPE_BYTE. Then the following notations are possible: * ESCAPE_BYTE ESCAPE_BYTE ESCAPE_BYTE literal * ESCAPE_BYTE n b n instances of byte b * Since an encoded run occupies 3 bytes, we only encode runs of 4 or * more bytes. Thus we have n > 0 and n != ESCAPE_BYTE and n <= 0xFF. * If we encounter a run where n == ESCAPE_BYTE, we represent this as: * b ESCAPE_BYTE n-1 b * The ESCAPE_BYTE value is chosen so as not to collide with commonly * seen values. */ public static final String arrayToRLEString(byte[] a) { StringBuffer buffer = new StringBuffer(); buffer.append((char) (a.length >> 16)); buffer.append((char) a.length); byte runValue = a[0]; int runLength = 1; byte[] state = new byte[2]; for (int i=1; i<a.length; ++i) { byte b = a[i]; if (b == runValue && runLength < 0xFF) { ++runLength; } else { encodeRun(buffer, runValue, runLength, state); runValue = b; runLength = 1; } } encodeRun(buffer, runValue, runLength, state); // We must save the final byte, if there is one, by padding // an extra zero. if (state[0] != 0) { appendEncodedByte(buffer, (byte)0, state); } return buffer.toString(); } /** * Construct a string representing a char array. Use run-length encoding. * A character represents itself, unless it is the ESCAPE character. Then * the following notations are possible: * ESCAPE ESCAPE ESCAPE literal * ESCAPE n c n instances of character c * Since an encoded run occupies 3 characters, we only encode runs of 4 or * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. * If we encounter a run where n == ESCAPE, we represent this as: * c ESCAPE n-1 c * The ESCAPE value is chosen so as not to collide with commonly * seen values. */ public static final String arrayToRLEString(char[] a) { StringBuffer buffer = new StringBuffer(); buffer.append((char) (a.length >> 16)); buffer.append((char) a.length); char runValue = a[0]; int runLength = 1; for (int i=1; i<a.length; ++i) { char s = a[i]; if (s == runValue && runLength < 0xFFFF) ++runLength; else { encodeRun(buffer, (short)runValue, runLength); runValue = s; runLength = 1; } } encodeRun(buffer, (short)runValue, runLength); return buffer.toString(); } /** * Construct a string representing an int array. Use run-length encoding. * A character represents itself, unless it is the ESCAPE character. Then * the following notations are possible: * ESCAPE ESCAPE ESCAPE literal * ESCAPE n c n instances of character c * Since an encoded run occupies 3 characters, we only encode runs of 4 or * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. * If we encounter a run where n == ESCAPE, we represent this as: * c ESCAPE n-1 c * The ESCAPE value is chosen so as not to collide with commonly * seen values. */ public static final String arrayToRLEString(int[] a) { StringBuffer buffer = new StringBuffer(); appendInt(buffer, a.length); int runValue = a[0]; int runLength = 1; for (int i=1; i<a.length; ++i) { int s = a[i]; if (s == runValue && runLength < 0xFFFF) { ++runLength; } else { encodeRun(buffer, runValue, runLength); runValue = s; runLength = 1; } } encodeRun(buffer, runValue, runLength); return buffer.toString(); } /** * Encode a run, possibly a degenerate run (of < 4 values). * @param length The length of the run; must be > 0 && <= 0xFFFF. */ private static final void encodeRun(StringBuffer buffer, short value, int length) { if (length < 4) { for (int j=0; j<length; ++j) { if (value == (int) ESCAPE) { buffer.append(ESCAPE); } buffer.append((char) value); } } else { if (length == (int) ESCAPE) { if (value == (int) ESCAPE) { buffer.append(ESCAPE); } buffer.append((char) value); --length; } buffer.append(ESCAPE); buffer.append((char) length); buffer.append((char) value); // Don't need to escape this value } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -