com.adobe.agl.impl.Utility Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aem-sdk-api Show documentation
Show all versions of aem-sdk-api Show documentation
The Adobe Experience Manager SDK
//##header J2SE15
/*
*******************************************************************************
* Copyright (C) 1996-2008, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
/*
* File: Utility.java
* ************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2012 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.agl.impl;
import java.util.ArrayList;
import com.adobe.agl.lang.*;
import com.adobe.agl.text.*;
import com.adobe.agl.impl.UCharacterProperty;
public final class Utility {
static final char DIGITS[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
/**
* Convert a char to 4 hex uppercase digits. E.g., hex('a') =>
* "0041".
*/
public static String hex(char ch) {
StringBuffer temp = new StringBuffer();
return hex(ch, temp).toString();
}
/**
* Convert a string to comma-separated groups of 4 hex uppercase
* digits. E.g., hex('ab') => "0041,0042".
*/
public static String hex(String s) {
StringBuffer temp = new StringBuffer();
return hex(s, temp).toString();
}
/**
* Convert a string to comma-separated groups of 4 hex uppercase
* digits. E.g., hex('ab') => "0041,0042".
*/
public static String hex(StringBuffer s) {
return hex(s.toString());
}
/**
* Convert a char to 4 hex uppercase digits. E.g., hex('a') =>
* "0041". Append the output to the given StringBuffer.
*/
public static StringBuffer hex(char ch, StringBuffer output) {
return appendNumber(output, ch, 16, 4);
}
/**
* Convert a integer to size width hex uppercase digits.
* E.g., hex('a', 4, str) => "0041".
* Append the output to the given StringBuffer.
* If width is too small to fit, nothing will be appended to output.
*/
public static StringBuffer hex(int ch, int width, StringBuffer output) {
return appendNumber(output, ch, 16, width);
}
/**
* Convert a integer to size width (minimum) hex uppercase digits.
* E.g., hex('a', 4, str) => "0041". If the integer requires more
* than width digits, more will be used.
*/
public static String hex(int ch, int width) {
StringBuffer buf = new StringBuffer();
return appendNumber(buf, ch, 16, width).toString();
}
/**
* Append a number to the given StringBuffer in the radix 10
* generating at least one digit.
*/
public static StringBuffer appendNumber(StringBuffer result, int n) {
return appendNumber(result, n, 10, 1);
}
/**
* Append the digits of a positive integer to the given
* StringBuffer
in the given radix. This is
* done recursively since it is easiest to generate the low-
* order digit first, but it must be appended last.
*
* @param result is the StringBuffer
to append to
* @param n is the positive integer
* @param radix is the radix, from 2 to 36 inclusive
* @param minDigits is the minimum number of digits to append.
*/
private static void recursiveAppendNumber(StringBuffer result, int n,
int radix, int minDigits)
{
int digit = n % radix;
if (n >= radix || minDigits > 1) {
recursiveAppendNumber(result, n / radix, radix, minDigits - 1);
}
result.append(DIGITS[digit]);
}
/**
* Append a number to the given StringBuffer in the given radix.
* Standard digits '0'-'9' are used and letters 'A'-'Z' for
* radices 11 through 36.
* @param result the digits of the number are appended here
* @param n the number to be converted to digits; may be negative.
* If negative, a '-' is prepended to the digits.
* @param radix a radix from 2 to 36 inclusive.
* @param minDigits the minimum number of digits, not including
* any '-', to produce. Values less than 2 have no effect. One
* digit is always emitted regardless of this parameter.
* @return a reference to result
*/
public static StringBuffer appendNumber(StringBuffer result, int n,
int radix, int minDigits)
throws IllegalArgumentException
{
if (radix < 2 || radix > 36) {
throw new IllegalArgumentException("Illegal radix " + radix);
}
int abs = n;
if (n < 0) {
abs = -n;
result.append("-");
}
recursiveAppendNumber(result, abs, radix, minDigits);
return result;
}
/**
* Supplies a zero-padded hex representation of an integer (without 0x)
*/
static public String hex(long i, int places) {
if (i == Long.MIN_VALUE) return "-8000000000000000";
boolean negative = i < 0;
if (negative) {
i = -i;
}
String result = Long.toString(i, 16).toUpperCase();
if (result.length() < places) {
result = "0000000000000000".substring(result.length(),places) + result;
}
if (negative) {
return '-' + result;
}
return result;
}
public static String hex(long ch) {
return hex(ch,4);
}
/**
* Convert a string to comma-separated groups of 4 hex uppercase
* digits. E.g., hex('ab') => "0041,0042". Append the output
* to the given StringBuffer.
*/
public static StringBuffer hex(String s, StringBuffer result) {
for (int i = 0; i < s.length(); ++i) {
if (i != 0) result.append(',');
hex(s.charAt(i), result);
}
return result;
}
}