📄 util.java
字号:
/*====================================================================*\Util.javaUtility methods 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/>.\*====================================================================*/// IMPORTSimport java.awt.Component;import java.awt.KeyboardFocusManager;import java.awt.Window;import java.io.File;import java.io.IOException;import java.lang.reflect.Method;import java.util.Arrays;//----------------------------------------------------------------------// UTILITY METHODS CLASSclass Util{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// private static final String FAILED_TO_GET_PATHNAME_STR = "Failed to get the canonical pathname " + "for the file or directory.";////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// private Util( ) { } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static Method getDeclaredMethod( Class<?> cls, String methodName, boolean hasParam ) throws NoSuchMethodException { while ( true ) { try { return ( hasParam ? cls.getDeclaredMethod( methodName, String.class ) : cls.getDeclaredMethod( methodName ) ); } catch ( NoSuchMethodException e ) { cls = cls.getSuperclass( ); if ( cls == null ) throw e; } } } //------------------------------------------------------------------ public static int getIndex( Object[] values, Object target ) { for ( int i = 0; i < values.length; ++i ) { if ( values[i].equals( target ) ) return i; } return -1; } //------------------------------------------------------------------ public static String mapString( String[] map, String key ) throws IllegalArgumentException { if ( (map == null) || (map.length % 2 != 0) ) throw new IllegalArgumentException( ); String value = key; for ( int i = 0; i < map.length; i += 2 ) { if ( map[i].equals( key ) ) { value = map[i + 1]; break; } } return value; } //------------------------------------------------------------------ public static char[] createCharArray( char ch, int length ) { char[] array = new char[length]; Arrays.fill( array, ch ); return array; } //------------------------------------------------------------------ public static int changeLineSeparators( char[] buffer, int offset, int length ) { int inIndex = offset; int outIndex = offset; int endIndex = offset + length; while ( inIndex < endIndex ) { char ch = buffer[inIndex++]; if ( ch == '\r' ) { if ( (inIndex < endIndex) && (buffer[inIndex] == '\n') ) ++inIndex; ch = '\n'; } buffer[outIndex++] = ch; } return outIndex; } //------------------------------------------------------------------ public static void moveFocus( Window window ) { KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager( ); if ( (window != null) && (window == focusManager.getFocusedWindow( )) ) { Component initialFocusOwner = focusManager.getFocusOwner( ); if ( (initialFocusOwner != null) && !initialFocusOwner.isEnabled( ) ) { Component focusOwner = null; while ( focusOwner != initialFocusOwner ) { focusManager.focusNextComponent( ); focusOwner = focusManager.getFocusOwner( ); if ( (focusOwner != null) && focusOwner.isEnabled( ) ) break; } } } } //------------------------------------------------------------------ public static String getPathname( File file, boolean unix ) { String pathname = new String( ); if ( file != null ) { try { pathname = file.getCanonicalPath( ); } catch ( Exception e ) { System.err.println( FAILED_TO_GET_PATHNAME_STR + "\n(" + e + ")" ); 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 pathname; } //------------------------------------------------------------------ public static boolean isSameFile( File file1, File file2 ) { try { if ( file1 == null ) return ( file2 == null ); return ( (file2 != null) && file1.getCanonicalPath( ).equals( file2.getCanonicalPath( ) ) ); } catch ( IOException e ) { return false; } } //------------------------------------------------------------------ public static File appendSuffix( File file, String suffix ) { String filename = file.getName( ); if ( (filename.length( ) > 0) && (filename.indexOf( '.' ) < 0) ) file = new File( file.getParentFile( ), filename + suffix ); return file; } //------------------------------------------------------------------}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -