📄 parameter.java
字号:
//-------------------------------------------------------------- } //================================================================== // VALUES OUT OF ORDER EXCEPTION public static class ValuesOutOfOrderException extends ParameterException { //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// public ValuesOutOfOrderException( Parameter param ) { super( ErrorId.PARAMETER_VALUES_OUT_OF_ORDER, param ); } //-------------------------------------------------------------- } //================================================================== // PARAMETER EXCEPTION CLASS protected static class ParameterException extends AppException { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// private static final String NAME_STR = "Name: "; private static final String VALUE_STR = "Value: "; //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// public ParameterException( ErrorId id, Parameter param ) { super( "[ " + param.type + " ]\n" + NAME_STR + param.name + ((param.index < 0) ? new String( ) : "#" + param.index) + "\n" + VALUE_STR + param.value + "\n" + AppException.getString( id ) ); } //-------------------------------------------------------------- } //==================================================================////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// private Parameter( Type type, String name, String value ) { this( type, name, -1, value ); } //------------------------------------------------------------------ private Parameter( Type type, String name, int index, String value ) { this.type = type; this.name = name; this.index = index; this.value = value; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static Parameter getParam( PropertyFile configFile, String name ) { return getParam( null, configFile, name ); } //------------------------------------------------------------------ public static Parameter getParam( PropertyFile configFile, String name, int index ) { return getParam( null, configFile, name, index ); } //------------------------------------------------------------------ public static Parameter getParam( JApplet applet, PropertyFile configFile, String name ) { Parameter param = null; if ( configFile != null ) param = new ConfigFileParam( configFile, name ); if ( (param == null) || (param.value == null) ) { if ( applet == null ) param = new PropertyParam( name ); else param = new AppletParam( applet, name ); } return param; } //------------------------------------------------------------------ public static Parameter getParam( JApplet applet, PropertyFile configFile, String name, int index ) { Parameter param = null; if ( configFile != null ) param = new ConfigFileParam( configFile, name, index ); if ( (param == null) || (param.value == null) ) { if ( applet == null ) param = new PropertyParam( name, index ); else param = new AppletParam( applet, name, index ); } return param; } //------------------------------------------------------------------ public static Boolean parseParamBoolean( Parameter param ) throws AppException { NoYes value = NoYes.get( param.value ); if ( value == null ) throw new IllegalValueException( param ); return new Boolean( value.toBoolean( ) ); } //------------------------------------------------------------------ public static Integer parseParamInteger( Parameter param, IntegerRange range ) throws AppException { try { Integer value = new Integer( param.value ); if ( (range != null) && !range.contains( value.intValue( ) ) ) throw new ValueOutOfBoundsException( param ); return value; } catch ( NumberFormatException e ) { throw new IllegalValueException( param ); } } //------------------------------------------------------------------ public static int[] parseParamIntegers( Parameter param, int length, IntegerRange[] ranges ) throws AppException { return parseParamIntegers( param, length, ranges, null ); } //------------------------------------------------------------------ public static int[] parseParamIntegers( Parameter param, int length, IntegerRange[] ranges, Order order ) throws AppException { String[] paramStrs = param.value.split( " *, *", -1 ); if ( paramStrs.length != length ) throw new IllegalValueException( param ); try { int[] values = new int[length]; for ( int i = 0; i < length; ++i ) { values[i] = Integer.parseInt( paramStrs[i] ); if ( (ranges != null) && (ranges[i] != null) && !ranges[i].contains( values[i] ) ) throw new ValueOutOfBoundsException( param ); if ( (order != null) && (i > 0) ) { boolean ordered = false; switch ( order ) { case LESS_THAN: ordered = (values[i] < values[i - 1]); break; case LESS_THAN_OR_EQUAL_TO: ordered = (values[i] <= values[i - 1]); break; case GREATER_THAN: ordered = (values[i] > values[i - 1]); break; case GREATER_THAN_OR_EQUAL_TO: ordered = (values[i] >= values[i - 1]); break; } if ( !ordered ) throw new ValuesOutOfOrderException( param ); } } return values; } catch ( NumberFormatException e ) { throw new IllegalValueException( param ); } } //------------------------------------------------------------------ public static Double parseParamDouble( Parameter param, DoubleRange range ) throws AppException { try { Double value = new Double( param.value ); if ( (range != null) && !range.contains( value.doubleValue( ) ) ) throw new ValueOutOfBoundsException( param ); return value; } catch ( NumberFormatException e ) { throw new IllegalValueException( param ); } } //------------------------------------------------------------------ public static double[] parseParamDoubles( Parameter param, int length, DoubleRange[] ranges ) throws AppException { return parseParamDoubles( param, length, ranges, null ); } //------------------------------------------------------------------ public static double[] parseParamDoubles( Parameter param, int length, DoubleRange[] ranges, Order order ) throws AppException { String[] paramStrs = param.value.split( " *, *", -1 ); if ( paramStrs.length != length ) throw new IllegalValueException( param ); try { double[] values = new double[length]; for ( int i = 0; i < length; ++i ) { values[i] = Double.parseDouble( paramStrs[i] ); if ( (ranges != null) && (ranges[i] != null) && !ranges[i].contains( values[i] ) ) throw new ValueOutOfBoundsException( param ); if ( (order != null) && (i > 0) ) { boolean ordered = false; switch ( order ) { case LESS_THAN: ordered = (values[i] < values[i - 1]); break; case LESS_THAN_OR_EQUAL_TO: ordered = (values[i] <= values[i - 1]); break; case GREATER_THAN: ordered = (values[i] > values[i - 1]); break; case GREATER_THAN_OR_EQUAL_TO: ordered = (values[i] >= values[i - 1]); break; } if ( !ordered ) throw new ValuesOutOfOrderException( param ); } } return values; } catch ( NumberFormatException e ) { throw new IllegalValueException( param ); } } //------------------------------------------------------------------ public static Color parseParamColour( Parameter param ) throws AppException { try { return ColourUtilities.parseColour( param.getValue( ) ); } catch ( IllegalArgumentException e ) { throw new IllegalValueException( param ); } catch ( IndexOutOfBoundsException e ) { throw new ValueOutOfBoundsException( param ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public Type getType( ) { return type; } //------------------------------------------------------------------ public String getName( ) { return name; } //------------------------------------------------------------------ public int getIndex( ) { return index; } //------------------------------------------------------------------ public String getValue( ) { return value; } //------------------------------------------------------------------ public boolean isValid( ) { return ( value != null ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private Type type; private String name; private int index; private String value;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -