📄 exceptionutilities.java
字号:
/*====================================================================*\ExceptionUtilities.javaException utilities class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage exception;//----------------------------------------------------------------------// IMPORTSimport java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;//----------------------------------------------------------------------// EXCEPTION UTILITIES CLASSpublic class ExceptionUtilities{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// private static final String ELLIPSIS_STR = "...";////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// private ExceptionUtilities( ) { } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static boolean isUnix( ) { return unix; } //------------------------------------------------------------------ public static void setUnix( boolean unix ) { ExceptionUtilities.unix = unix; } //------------------------------------------------------------------ public static String getLimitedPathname( File file, int maxLength ) { String pathname = null; try { pathname = file.getCanonicalPath( ); } catch ( IOException e ) { System.err.println( e ); StackTraceElement[] stackTraceElements = e.getStackTrace( ); if ( stackTraceElements.length > 0 ) System.err.println( stackTraceElements[0] ); pathname = file.getAbsolutePath( ); } if ( unix ) { try { String userHome = System.getProperty( "user.home" ); if ( (userHome != null) && pathname.startsWith( userHome ) ) pathname = "~" + pathname.substring( userHome.length( ) ); } catch ( SecurityException e ) { // ignore } if ( File.separatorChar != '/' ) pathname = pathname.replace( File.separatorChar, '/' ); } return getLimitedPathname( pathname, maxLength ); } //------------------------------------------------------------------ public static String getLimitedPathname( String pathname, int maxLength ) { // Test for null pathname if ( pathname == null ) return null; // Split the pathname into its components char separatorChar = unix ? '/' : File.separatorChar; List<String> strs = new ArrayList<String>( ); int index = 0; while ( index < pathname.length( ) ) { int startIndex = index; index = pathname.indexOf( separatorChar, index ); if ( index < 0 ) index = pathname.length( ); if ( index > startIndex ) strs.add( pathname.substring( startIndex, index ) ); ++index; } if ( strs.isEmpty( ) ) return pathname; // Get the maximum number of components StringBuilder buffer = new StringBuilder( ELLIPSIS_STR ); int numComponents = 0; for ( int i = strs.size( ) - 1; i >= 0; --i ) { buffer.append( separatorChar ); buffer.append( strs.get( i ) ); if ( buffer.length( ) > maxLength ) break; ++numComponents; } // If last component is too wide, remove leading characters until it fits if ( numComponents == 0 ) { String str = strs.get( strs.size( ) - 1 ); return ( ELLIPSIS_STR + str.substring( Math.max( 0, str.length( ) - maxLength + ELLIPSIS_STR.length( ) ) ) ); } // If the entire pathname fits, return it if ( numComponents == strs.size( ) ) return pathname; // Construct a reduced pathname buffer = new StringBuilder( ELLIPSIS_STR ); for ( int i = strs.size( ) - numComponents; i < strs.size( ); ++i ) { buffer.append( separatorChar ); buffer.append( strs.get( i ) ); } return buffer.toString( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class variables//////////////////////////////////////////////////////////////////////// private static boolean unix;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -