📄 visibleinteger.java
字号:
package edu.odu.cs.zeil.AlgAE.Demos.Utils;import edu.odu.cs.zeil.AlgAE.Direction;import edu.odu.cs.zeil.AlgAE.Server.Rendering;import edu.odu.cs.zeil.AlgAE.Server.Visible;import edu.odu.cs.zeil.AlgAE.Server.VisibleRendering;import edu.odu.cs.zeil.AlgAE.Demos.Utils.Indexable;import java.awt.Color;/** * Useful class for portraying int's within algorithm animations, * especially int's serving as array indices. * * @author Steven Zeil */public class VisibleInteger implements Visible, Cloneable{ private VisibleRendering vr; private Integer theInt; private Visible[] theArray; private Indexable theVector; private static Color defaultColor = Color.yellow; /** * The smallest value of type <code>int</code>. * */ public static final int MIN_VALUE = Integer.MIN_VALUE; /** * The largest value of type <code>int</code>. * */ public static final int MAX_VALUE = Integer.MAX_VALUE; /** * Creates a string representation of the first argument in the * radix specified by the second argument. * * @param i an integer. * @param radix the radix. * @return a string representation of the argument in the specified radix. * @see java.lang.Integer#toString */ public static String toString(int i, int radix) { return Integer.toString(i, radix); } /** * Creates a string representation of the integer argument as an * unsigned integer in base 16. * @param i an integer. * @return the string representation of the unsigned integer value * represented by the argument in hexadecimal (base 16). */ public static String toHexString(int i) { return Integer.toHexString(i); } /** * Creates a string representation of the integer argument as an * unsigned integer in base 8. * * @param i an integer * @return the string representation of the unsigned integer value * represented by the argument in octal (base&mnsp;8). */ public static String toOctalString(int i) { return Integer.toOctalString(i); } /** * Creates a string representation of the integer argument as an * unsigned integer in base 2. * * @param i an integer. * @return the string representation of the unsigned integer value * represented by the argument in binary (base 2). */ public static String toBinaryString(int i) { return Integer.toBinaryString(i); } /** * Returns a new String object representing the specified integer. The radix * is assumed to be 10. * * @param i an integer to be converted. * @return a string representation of the argument in base 10. */ public static String toString(int i) { return Integer.toString(i); } /** * Parses the string argument as a signed integer in the radix * specified by the second argument. * * @param s the <code>String</code> containing the integer. * @param radix the radix to be used. * @return the integer represented by the string argument in the * specified radix. * @exception NumberFormatException if the string does not contain a * parsable integer. */ public static int parseInt(String s, int radix) throws NumberFormatException { return Integer.parseInt(s, radix); } /** * Parses the string argument as a signed decimal integer. * * @param s a string. * @return the integer represented by the argument in decimal. * @exception NumberFormatException if the string does not contain a * parsable integer. */ public static int parseInt(String s) throws NumberFormatException { return Integer.parseInt(s); } /** * Returns a new VisibleInteger object initialized to the value of the * specified String. Throws an exception if the String cannot be * parsed as an int. * * @param s the string to be parsed. * @return a newly constructed <code>Integer</code> initialized to the * value represented by the string argument in the specified * radix. * @exception NumberFormatException if the <code>String</code> does not * contain a parsable integer. */ public static VisibleInteger valueOf(String s, int radix) throws NumberFormatException { return new VisibleInteger(parseInt(s,radix), defaultColor); } /** * Returns a new VisibleInteger object initialized to the value of the * specified String. Throws an exception if the String cannot be * parsed as an int. The radix is assumed to be 10. * * @param s the string to be parsed. * @return a newly constructed <code>Integer</code> initialized to the * value represented by the string argument. * @exception NumberFormatException if the string does not contain a * parsable integer. */ public static VisibleInteger valueOf(String s) throws NumberFormatException { return new VisibleInteger(parseInt(s, 10), defaultColor); } /** * Constructs a newly allocated <code>VisibleInteger</code> object that * represents the primitive <code>int</code> argument. * * @param value the value to be represented by the * <code>VisibleInteger</code>. */ public VisibleInteger(int value, Color color) { theInt = new Integer(value); vr = new VisibleRendering (this, color); theArray = null; theVector = null; } /** * Constructs a newly allocated <code>VisibleInteger</code> object that * represents the value represented by the string. The string is * converted to an int value as if by the <code>valueOf</code> method. * * @param s the <code>String</code> to be converted to an * <code>VisibleInteger</code>. * @exception NumberFormatException if the <code>String</code> does not * contain a parsable integer. * @see java.lang.Integer#valueOf(java.lang.String, int) */ public VisibleInteger(String s, Color color) throws NumberFormatException { theInt = new Integer(s); vr = new VisibleRendering (this, color); theArray = null; theVector = null; } /** * Returns the value of this VisibleInteger as a byte. * */ public byte byteValue() { return theInt.byteValue(); } /** * Returns the value of this VisibleInteger as a short. * */ public short shortValue() { return theInt.shortValue(); } /** * Returns the value of this VisibleInteger as an int. * * @return the <code>int</code> value represented by this object. */ public int intValue() { return theInt.intValue(); } /** * Returns the value of this VisibleInteger as a long. * * @return the <code>int</code> value represented by this object that is * converted to type <code>long</code> and the result of the * conversion is returned. */ public long longValue() { return theInt.longValue(); } /** * Returns the value of this VisibleInteger as a float. * * @return the <code>int</code> value represented by this object is * converted to type <code>float</code> and the result of the * conversion is returned. */ public float floatValue() { return theInt.floatValue(); } /** * Returns the value of this VisibleInteger as a double. * * @return the <code>int</code> value represented by this object is * converted to type <code>double</code> and the result of the * conversion is returned. */ public double doubleValue() { return theInt.doubleValue(); } /** * Returns a String object representing this VisibleInteger's value. * * @return a string representation of the value of this object in * base 10. */ public String toString() { return theInt.toString(); } /** * Returns a hashcode for this VisibleInteger. * * @return a hash code value for this object. */ public int hashCode() { return theInt.hashCode(); } /** * Compares this object to the specified object. * The result is <code>true</code> if and only if the argument is not * <code>null</code> and is an <code>Integer</code> or a * <code>VisibleInteger</code> object that contains * the same <code>int</code> value as this object. * * @param obj the object to compare with. * @return <code>true</code> if the objects are the same; * <code>false</code> otherwise. */ public boolean equals(Object obj) { int value = theInt.intValue(); if ((obj != null) && (obj instanceof VisibleInteger)) { return value == ((VisibleInteger)obj).intValue(); } else if ((obj != null) && (obj instanceof Integer)) { return value == ((Integer)obj).intValue(); } return false; } /** * Return a copy of this object * */ public Object clone() { VisibleInteger theClone = new VisibleInteger(theInt.intValue(), vr.color()); return theClone; } /** * This function returns the VisibleRendering data member * * @return the rendering information for this object. **/ public Rendering getRendering() { return vr; } /** * Produce the text string to be displayed in the picture of the object. * * @return the text string to be displayed **/ public String getAlgAEText() { return theInt.toString(); } /** * If an object <CODE>s</CODE> is to be portrayed as having * pointers/references/arrows to other Visible objects, then when * <CODE>s.touchAllPointers()</CODE> is called, * the body of <CODE>touchAllPointers</CODE> should call, for each arrow * from <CODE>s</CODE> to another Visible object <CODE>p</CODE>, * <CODE>s.getRendering().touch(p, dir)</CODE> or * <CODE>s.getRendering().touch(p, dir, label)</CODE>. * <P> * <CODE>dir</CODE> is the "exit" direction from which the AlgAE system * will draw the arrow from <CODE>s</CODE> to <CODE>p</CODE>. **/ public void touchAllPointers() { if (theArray != null) { int i = theInt.intValue(); if ((i >= 0) && (i < theArray.length)) { Visible v = theArray[i]; if (v != null) vr.touch (v, Direction.ANYDIR); } } else if (theVector != null) { int i = theInt.intValue(); if ((i >= 0) && (i < theVector.size())) { Visible v = (Visible)theVector.elementAt(i); if (v != null) vr.touch (v, Direction.ANYDIR); } } } /** * If an object <CODE>s</CODE> is to be portrayed as a compound structure * containing other Visible objects, then when * <CODE>s.touchAllComponents()</CODE> is called, * the body of <CODE>touchAllComponents</CODE> should call, for each * contained Visible component <CODE>p</CODE>, * <CODE>s.getRendering().touch(p)</CODE>. * <P> * Note that the the distinction between a "Pointer" and a "Component" * is a logical distinction, as they are both typically implemented as * simple data members of the Visible class. It is up to the designer * of the algorithm animation to decide what to portray as references and * what to portray as contained components. **/ public void touchAllComponents() { } /** * Treat this integer as an index into an array, drawing an arrow * to the array element it indexes. * * @param array The array this integer indexes into. **/ public void indexes(Visible[] array) { theArray = array; } /** * Treat this integer as an index into a vector-like collection, * drawing an arrow to the array element it indexes. * * @param array The array this integer indexes into. **/ public void indexes(Indexable v) { theVector = v; } /** * Change the value of this integer **/ public void setInt (int i) { theInt = new Integer(i); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -