piycomparator.java
字号:
package piy;
import java.io.Serializable;
/**
* Represents an abstract comparator - a class that can be used to compare two objects.
* Each comparator can provide any number of comparison methods, for example a String
* comparator might return true if stringA starts with stringB, or stringA ends with
* stringB, etc. Negative statements need not (should not) be provided - PIY
* gives the user the option to negate.<p>
* Classes extending this should include the code (in the constructor)
* <code>super.init(this)</code> to initialise the class.
* @author David Vivash
* @version 1.0, 30/04/01
*/
public abstract class PIYComparator extends BooleanComparable implements Serializable {
private FixedProperty first = null, second = null;
private String mode = null;
private PIYComparator implementingClass = null;
/**
* Initialises the new PIYComparator. If the extending class does not return
* an array of strings containing at least one element from the getModes() method,
* this method throws a PIYComparatorError.
* @param instance the instance object of the extending PIYComparator class
*/
public void init(PIYComparator instance) {
if (instance == null) throw new PIYComparatorError();
implementingClass = instance;
first = new FixedProperty(null, instance.getFirstType());
second = new FixedProperty(null, instance.getSecondType());
String[] modes = instance.getModes();
if ((modes == null) || (modes.length == 0)) throw new PIYComparatorError();
else mode = modes[0];
}
/**
* Get the first object for comparison
* @return the first object
*/
public FixedProperty getFirst() { return first; }
/**
* Get the second object for comparison, the thing the first object is being compared to
* @return the second object
*/
public FixedProperty getSecond() { return second; }
/**
* Gets what type the first object should be. The <code>first</code> object will
* be set up to have this type. If there is no type, and support should not be set up,
* this method should be set up to return void.class.
*/
public abstract Class getFirstType();
/**
* Gets what type the first object should be. The <code>second</code> object will
* be set up to have this type. If there is no type, and support should not be set up,
* this method should be set up to return void.class.
*/
public abstract Class getSecondType();
/**
* Gets the mode of operation for this comparator.
* @return the currently selected mode
*/
public String getMode() { return mode; }
/**
* Set the mode of operation for this comparator. If the mode is not valid, ie. not one
* of the modes specified in getModes(), the mode will remain with its previous value.
* @param val the mode to set
*/
public void setMode(String val) {
String[] modes = implementingClass.getModes();
for (int i=0; i<modes.length; i++)
if (modes[i] == val) mode = val;
}
public abstract String[] getModes();
}
class PIYComparatorError extends Error {
public PIYComparatorError() { super(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -