📄 tools.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Gereon Fassbender, Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.components.util;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/** miscellaneous util methods.*/
public abstract class Tools
{
/** same as new Object[0].*/
public static Object[] EMPTY_ARRAY = new Object[0];
public static Class[] EMPTY_CLASS_ARRAY = new Class[0];
public static String[] EMPTY_STRING_ARRAY = new String[0];
public static Iterator EMPTY_ITERATOR = new ArrayList().iterator();
/** an unmodifiable list with no elements. */
public static List EMPTY_LIST = new ArrayList() {
private static final long serialVersionUID = 1L;
public boolean add(Object object) {
throw new UnsupportedOperationException();
}
public void add(int index,Object object) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection col) {
throw new UnsupportedOperationException();
}
public boolean addAll(int index,Collection col) {
throw new UnsupportedOperationException();
}
public Iterator iterator() {
return EMPTY_ITERATOR;
}
};
/** an unmodifiable set with no elements. */
public static Set EMPTY_SET = new HashSet() {
private static final long serialVersionUID = 1L;
public boolean add(Object object) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection col) {
throw new UnsupportedOperationException();
}
public Iterator iterator() {
return EMPTY_ITERATOR;
}
};
/** test if 2 char arrays match. */
public static boolean match(char[] p1, char[] p2) {
boolean bMatch = true;
if (p1.length == p2.length) {
for (int i = 0; i<p1.length; i++) {
if (p1[i] != p2[i]) {
bMatch = false;
break;
}
}
} else {
bMatch = false;
}
return bMatch;
}
private static boolean validStart(char c)
{
return (c == '_' || c =='-' || Character.isLetter(c)) ?
true : false;
}
private static boolean validLetter(char c) {
return (validStart(c) || Character.isDigit(c));
}
/** Rudimentary tests if the string is a valid xml-tag.*/
public static boolean isKey(String key) {
char[] c = key.toCharArray();
if (c.length == 0)
return false;
if (!validStart(c[0]))
return false;
for (int i=0;i<c.length;i++) {
if (!validLetter(c[i]))
return false;
}
return true;
}
/** same as substring(0,width-1) except that it will not
not throw an <code>ArrayIndexOutOfBoundsException</code> if string.length()<width.
*/
public static String left(String string,int width) {
return string.substring(0, Math.min(string.length(), width -1));
}
/** Convert a byte array into a printable format containing aString of hexadecimal digit characters (two per byte).
* This method is taken form the apache jakarata
* tomcat project.
*/
public static String convert(byte bytes[]) {
StringBuffer sb = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
sb.append(convertDigit((int) (bytes[i] >> 4)));
sb.append(convertDigit((int) (bytes[i] & 0x0f)));
}
return (sb.toString());
}
/** Convert the specified value (0-15) to the corresponding hexadecimal digit.
* This method is taken form the apache jakarata tomcat project.
*/
public static char convertDigit(int value) {
value &= 0x0f;
if (value >= 10)
return ((char) (value - 10 + 'a'));
else
return ((char) (value + '0'));
}
public static boolean equalsOrBothNull(Object o1, Object o2) {
if (o1 == null) {
if (o2 != null) {
return false;
}
} else if ( o2 == null) {
return false;
} else if (!o1.equals( o2 ) ) {
return false;
}
return true;
}
/** 1.3 compatibility method */
public static String[] split(String stringToSplit, char delimiter) {
List keys = new ArrayList();
int lastIndex = 0;
while( true ) {
int index = stringToSplit.indexOf( delimiter,lastIndex);
if ( index < 0)
{
String token = stringToSplit.substring( lastIndex );
if ( token.length() > 0)
{
keys.add( token );
}
break;
}
String token = stringToSplit.substring( lastIndex , index );
keys.add( token );
lastIndex = index + 1;
}
return (String[])keys.toArray( new String[] {});
}
/** 1.3 compatibility method */
public static String replaceAll( String string, String stringToReplace, String newString ) {
if ( stringToReplace.equals( newString))
return string;
int length = stringToReplace.length();
int oldPos = 0;
while ( true ) {
int pos = string.indexOf( stringToReplace,oldPos);
if ( pos < 0 )
return string;
string = string.substring(0, pos) + newString + string.substring( pos + length);
oldPos = pos + 1;
if ( oldPos >= string.length() )
return string;
}
}
/** reads a table from a csv file. You can specify a minimum number of columns */
public static String[][] csvRead(Reader reader, int expectedColumns) throws IOException {
//System.out.println( "Using Encoding " + reader.getEncoding() );
StringBuffer buf = new StringBuffer();
while (true) {
int c = reader.read();
if ( c == -1 )
break;
buf.append( (char) c );
}
String[] lines = split( buf.toString(),'\n');
String[][] lineEntries = new String[ lines.length ][];
for ( int i=0;i<lines.length; i++ ) {
lineEntries[i] = split( lines[i],';');
if ( lineEntries[i].length < expectedColumns ) {
throw new IOException("Can't parse line " + i + "\n" + lines[i] + "\n Expected " + expectedColumns + " Entries. Found " + lineEntries[i].length);
}
}
return lineEntries;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -