Simplify ObjectRecorder

by replacing direct access of fields with a Tracker interface
Remove support for getting multiple values from a Tracker,
it was unused.
This commit is contained in:
zzz
2022-01-09 08:32:52 -05:00
parent b554c5b647
commit 7003bcbd2a
8 changed files with 60 additions and 149 deletions

View File

@ -1,8 +1,11 @@
package net.i2p.itoopie.gui.component.chart; package net.i2p.itoopie.gui.component.chart;
public class DummyDataCollector extends Thread { /**
* Unused, for testing only
*/
public class DummyDataCollector extends Thread implements Tracker {
/** Streches or compresses the grade of jumping of the internal number. */ /** Streches or compresses the grade of jumping of the internal number. */
protected double m_factor; protected final double m_factor;
/** The bumping number. */ /** The bumping number. */
protected double m_number = 0; protected double m_number = 0;
@ -11,7 +14,7 @@ public class DummyDataCollector extends Thread {
protected double m_plusminus = 0.5; protected double m_plusminus = 0.5;
/** Needed for randomization of bumping the number. */ /** Needed for randomization of bumping the number. */
protected java.util.Random m_randomizer = new java.util.Random(); protected final java.util.Random m_randomizer = new java.util.Random();
/** /**
* Creates an instance. * Creates an instance.
@ -58,4 +61,9 @@ public class DummyDataCollector extends Thread {
} }
} }
/**
* @since 0.0.4
*/
public double getData() { return m_number; }
} }

View File

@ -12,7 +12,7 @@ import net.i2p.itoopie.i2pcontrol.methods.GetRateStat;
import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo; import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo;
import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO; import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
public class InboundBandwidthTracker extends Thread { public class InboundBandwidthTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance(); private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */ /** Last read bw */
@ -60,4 +60,9 @@ public class InboundBandwidthTracker extends Thread {
m_value = 0; m_value = 0;
} }
} }
/**
* @since 0.0.4
*/
public double getData() { return m_value; }
} }

View File

@ -69,7 +69,7 @@ public class ObjRecorder2Trace2DAdapter implements ChangeListener {
* @param interval * @param interval
* the interval of inspections in ms. * the interval of inspections in ms.
*/ */
public ObjRecorder2Trace2DAdapter(final ITrace2D view, final Object toinspect, public ObjRecorder2Trace2DAdapter(final ITrace2D view, final Tracker toinspect,
final String fieldname, final long interval) { final String fieldname, final long interval) {
this.m_view = view; this.m_view = view;
this.m_fieldname = fieldname; this.m_fieldname = fieldname;

View File

@ -23,9 +23,6 @@
package net.i2p.itoopie.gui.component.chart; package net.i2p.itoopie.gui.component.chart;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
@ -76,45 +73,28 @@ public class ObjectRecorder extends Thread {
public final class ObjectInspection { public final class ObjectInspection {
/** Time stamp of the inspection. */ /** Time stamp of the inspection. */
protected long m_time; protected final long m_time;
/** The values taken on the inspection. */ /** The values taken on the inspection. */
private final LinkedList<Object> m_values; private final Object m_value;
/** /**
* Creates an instance linked to the outer recorder. * Creates an instance linked to the outer recorder.
* <p> * <p>
* *
*/ */
protected ObjectInspection() { protected ObjectInspection(Object value) {
this.m_time = new java.util.Date().getTime(); this.m_time = System.currentTimeMillis();
this.m_values = new LinkedList<Object>(); this.m_value = value;
} }
/** /**
* Adds an inspected value to this inspection. * Get the value for the attribute
* <p>
* *
* @param value * @return the value for the attribute
* an inspected value of this inspection.
*/ */
protected void add(final Object value) { public Object get() {
this.m_values.add(value); return this.m_value;
}
/**
* Get the value for the attribute at the given index.
* <p>
*
* @param index
* the index of the inspected value according to the order it was
* found on the instance by {@link Class#getDeclaredFields()}.
* <p>
*
* @return the value for the attribute at the given index.
*/
public Object get(final int index) {
return this.m_values.get(index);
} }
/** /**
@ -127,21 +107,6 @@ public class ObjectRecorder extends Thread {
return this.m_time; return this.m_time;
} }
/**
* Removes the inspected value from this inspection.
* <p>
*
* The value is identified by means of
* {@link Object#equals(java.lang.Object)}.
* <p>
*
* @param value
* the inspected value from this inspection.
*/
protected void remove(final Object value) {
this.m_values.remove(value);
}
/** /**
* Returns a pretty print of this inspection. * Returns a pretty print of this inspection.
* <p> * <p>
@ -156,10 +121,6 @@ public class ObjectRecorder extends Thread {
ret.append("-----------------\n"); ret.append("-----------------\n");
ret.append("Inspected: ").append(ObjectRecorder.this.getInspected().toString()).append("\n"); ret.append("Inspected: ").append(ObjectRecorder.this.getInspected().toString()).append("\n");
ret.append("time: ").append(this.m_time).append("\n"); ret.append("time: ").append(this.m_time).append("\n");
for (int i = ObjectRecorder.this.m_fields.length - 1; i >= 0; i--) {
ret.append(ObjectRecorder.this.m_fields[i].getName()).append(": ").append(
this.m_values.get(i).toString()).append("\n");
}
return ret.toString(); return ret.toString();
} }
} }
@ -167,23 +128,20 @@ public class ObjectRecorder extends Thread {
/** Verbosity constant. */ /** Verbosity constant. */
protected static final boolean VERBOSE = false; protected static final boolean VERBOSE = false;
/** Fast buffer to store recorded fiels. */ /** Fast buffer to store recorded fields. */
protected IRingBuffer<ObjectRecorder.ObjectInspection> m_buffer = new RingBufferArrayFast<ObjectRecorder.ObjectInspection>( protected IRingBuffer<ObjectRecorder.ObjectInspection> m_buffer = new RingBufferArrayFast<ObjectRecorder.ObjectInspection>(
100); 100);
/** The listeners on this recorder. */ /** The listeners on this recorder. */
protected EventListenerList m_changeListeners = new EventListenerList(); protected EventListenerList m_changeListeners = new EventListenerList();
/** The fields to inspect on the instance. */
protected Field[] m_fields;
/** /**
* The time - interval between to inspections of the Object. * The time - interval between to inspections of the Object.
*/ */
protected long m_interval; protected long m_interval;
/** The instance to inspect. */ /** The instance to inspect. */
protected Object m_toinspect; protected Tracker m_toinspect;
/** /**
* Creates an instance that will inspect the given Object in the given time * Creates an instance that will inspect the given Object in the given time
@ -196,12 +154,10 @@ public class ObjectRecorder extends Thread {
* @param interval * @param interval
* the interval of inspection in ms. * the interval of inspection in ms.
*/ */
public ObjectRecorder(final Object toinspect, final long interval) { public ObjectRecorder(final Tracker toinspect, final long interval) {
this.m_interval = interval; this.m_interval = interval;
this.m_toinspect = toinspect; this.m_toinspect = toinspect;
this.setDaemon(true); this.setDaemon(true);
// getting the field names.
this.m_fields = toinspect.getClass().getDeclaredFields();
this.start(); this.start();
} }
@ -249,9 +205,6 @@ public class ObjectRecorder extends Thread {
} else if (!this.m_changeListeners.equals(other.m_changeListeners)) { } else if (!this.m_changeListeners.equals(other.m_changeListeners)) {
return false; return false;
} }
if (!Arrays.equals(this.m_fields, other.m_fields)) {
return false;
}
if (this.m_interval != other.m_interval) { if (this.m_interval != other.m_interval) {
return false; return false;
} }
@ -298,44 +251,17 @@ public class ObjectRecorder extends Thread {
*/ */
public TimeStampedValue[] getAttributeHistory(final String attributeName) public TimeStampedValue[] getAttributeHistory(final String attributeName)
throws NoSuchAttributeException { throws NoSuchAttributeException {
// search for the field
int attribindex = -1;
for (int i = this.m_fields.length - 1; i >= 0; i--) {
if (this.m_fields[i].getName().equals(attributeName)) {
attribindex = i;
break;
}
}
if (attribindex == -1) {
throw new NoSuchAttributeException("The Attribute with the name: " + attributeName
+ " does not exist in " + this.m_toinspect.getClass().getName());
}
final int stop = this.m_buffer.size(); final int stop = this.m_buffer.size();
final TimeStampedValue[] ret = new TimeStampedValue[stop]; final TimeStampedValue[] ret = new TimeStampedValue[stop];
synchronized (this.m_buffer) { synchronized (this.m_buffer) {
for (final ObjectInspection tmp : this.m_buffer) { for (final ObjectInspection tmp : this.m_buffer) {
int i = 0; int i = 0;
ret[i++] = new TimeStampedValue(tmp.getTime(), tmp.get(attribindex)); ret[i++] = new TimeStampedValue(tmp.getTime(), tmp.get());
} }
} }
return ret; return ret;
} }
/**
* Returns the names of the fields to inspect.
* <p>
*
* @return the names of the fields to inspect.
*/
public String[] getAttributeNames() {
final String[] ret = new String[this.m_fields.length];
for (int i = 0; i < this.m_fields.length; i++) {
ret[i] = this.m_fields[i].getName();
}
return ret;
}
/** /**
* Returns the inspected instance. * Returns the inspected instance.
* <p> * <p>
@ -362,20 +288,8 @@ public class ObjectRecorder extends Thread {
* *
*/ */
public TimeStampedValue getLastValue(final String fieldname) throws NoSuchAttributeException { public TimeStampedValue getLastValue(final String fieldname) throws NoSuchAttributeException {
// search for the field
int attribindex = -1;
for (int i = this.m_fields.length - 1; i >= 0; i--) {
if (this.m_fields[i].getName().equals(fieldname)) {
attribindex = i;
break;
}
}
if (attribindex == -1) {
throw new NoSuchAttributeException("The Attribute with the name: " + fieldname
+ " does not exist in " + this.m_toinspect.getClass().getName());
}
final ObjectInspection tmp = this.m_buffer.getYoungest(); final ObjectInspection tmp = this.m_buffer.getYoungest();
return new TimeStampedValue(tmp.getTime(), tmp.get(attribindex)); return new TimeStampedValue(tmp.getTime(), tmp.get());
} }
/** /**
@ -401,7 +315,6 @@ public class ObjectRecorder extends Thread {
result = prime * result + ((this.m_buffer == null) ? 0 : this.m_buffer.hashCode()); result = prime * result + ((this.m_buffer == null) ? 0 : this.m_buffer.hashCode());
result = prime * result result = prime * result
+ ((this.m_changeListeners == null) ? 0 : this.m_changeListeners.hashCode()); + ((this.m_changeListeners == null) ? 0 : this.m_changeListeners.hashCode());
result = prime * result + Arrays.hashCode(this.m_fields);
result = prime * result + (int) (this.m_interval ^ (this.m_interval >>> 32)); result = prime * result + (int) (this.m_interval ^ (this.m_interval >>> 32));
result = prime * result + ((this.m_toinspect == null) ? 0 : this.m_toinspect.hashCode()); result = prime * result + ((this.m_toinspect == null) ? 0 : this.m_toinspect.hashCode());
return result; return result;
@ -417,46 +330,7 @@ public class ObjectRecorder extends Thread {
* with first letter upper case. * with first letter upper case.
*/ */
public void inspect() { public void inspect() {
final ObjectInspection newentry = new ObjectInspection(); final ObjectInspection newentry = new ObjectInspection(m_toinspect.getData());
for (final Field mField : this.m_fields) {
if (ObjectRecorder.VERBOSE) {
System.out.println(this.getClass().getName() + " inpspecting " + mField.getName() + " of "
+ this.m_toinspect.getClass().getName() + ".");
}
try {
mField.setAccessible(true);
newentry.add(mField.get(this.m_toinspect));
} catch (final IllegalAccessException e) {
if (ObjectRecorder.VERBOSE) {
System.err.println(this.getClass().getName() + ".inspect(): No public access to "
+ mField.getName() + " of " + this.m_toinspect.getClass().getName());
}
// Try to invoke bean- conform getter method.
String fieldname = mField.getName();
final char[] fieldnm = fieldname.toCharArray();
fieldnm[0] = Character.toUpperCase(fieldnm[0]);
fieldname = new String(fieldnm);
final String methodname = new StringBuffer("get").append(fieldname).toString();
// name of method constructed. Now invoke it.
try {
final Method toinvoke = this.m_toinspect.getClass().getDeclaredMethod(methodname,
new Class[] {});
newentry.add(toinvoke.invoke(this.m_toinspect, new Object[] {}));
} catch (final NoSuchMethodException f) {
if (ObjectRecorder.VERBOSE) {
System.err.println(this.getClass().getName() + ".inspect(): Failure at getting field "
+ mField.getName() + " by trying to invoke a method: " + methodname);
}
} catch (final SecurityException g) {
g.printStackTrace();
} catch (final IllegalAccessException h) {
h.printStackTrace();
} catch (final InvocationTargetException l) {
l.printStackTrace();
}
}
}
this.m_buffer.add(newentry); this.m_buffer.add(newentry);
this.fireChange(); this.fireChange();
} }

View File

@ -12,7 +12,7 @@ import net.i2p.itoopie.i2pcontrol.methods.GetRateStat;
import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo; import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo;
import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO; import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
public class OutboundBandwidthTracker extends Thread { public class OutboundBandwidthTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance(); private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */ /** Last read bw */
@ -60,4 +60,9 @@ public class OutboundBandwidthTracker extends Thread {
m_value = 0; m_value = 0;
} }
} }
/**
* @since 0.0.4
*/
public double getData() { return m_value; }
} }

View File

@ -12,7 +12,7 @@ import net.i2p.itoopie.i2pcontrol.methods.GetRateStat;
import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo; import net.i2p.itoopie.i2pcontrol.methods.GetRouterInfo;
import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO; import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
public class ParticipatingTunnelsTracker extends Thread { public class ParticipatingTunnelsTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance(); private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */ /** Last read bw */
@ -59,4 +59,9 @@ public class ParticipatingTunnelsTracker extends Thread {
m_value = 0; m_value = 0;
} }
} }
/**
* @since 0.0.4
*/
public double getData() { return m_value; }
} }

View File

@ -7,7 +7,7 @@ import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException; import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.methods.GetRateStat; import net.i2p.itoopie.i2pcontrol.methods.GetRateStat;
public class RateStatTracker extends Thread { public class RateStatTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance(); private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */ /** Last read bw */
@ -56,4 +56,9 @@ public class RateStatTracker extends Thread {
} }
} }
/**
* @since 0.0.4
*/
public double getData() { return m_value; }
} }

View File

@ -0,0 +1,9 @@
package net.i2p.itoopie.gui.component.chart;
/**
* @since 0.0.4
*/
public interface Tracker {
public double getData();
}