Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Reformat code and change TroubleCodesCommand #134

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
469 changes: 235 additions & 234 deletions pom.xml

Large diffs are not rendered by default.

808 changes: 427 additions & 381 deletions src/main/java/com/github/pires/obd/commands/ObdCommand.java

Large diffs are not rendered by default.

104 changes: 56 additions & 48 deletions src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,71 @@
import java.util.ArrayList;

/**
* Container for multiple {@link com.github.pires.obd.commands.ObdMultiCommand} instances.
* Container for multiple {@link com.github.pires.obd.commands.ObdMultiCommand}
* instances.
*
*/
public class ObdMultiCommand {

private ArrayList<ObdCommand> commands;
private ArrayList<ObdCommand> commands;

/**
* Default ctor.
*/
public ObdMultiCommand() {
this.commands = new ArrayList<>();
}
/**
* Default Constructor.
*/
public ObdMultiCommand() {
this.commands = new ArrayList<>();
}

/**
* Add ObdCommand to list of ObdCommands.
*
* @param command a {@link com.github.pires.obd.commands.ObdCommand} object.
*/
public void add(ObdCommand command) {
this.commands.add(command);
}
/**
* Add ObdCommand to list of ObdCommands.
*
* @param command
* a {@link com.github.pires.obd.commands.ObdCommand} object.
*/
public void add(ObdCommand command) {
this.commands.add(command);
}

/**
* Removes ObdCommand from the list of ObdCommands.
*
* @param command a {@link com.github.pires.obd.commands.ObdCommand} object.
*/
public void remove(ObdCommand command) {
this.commands.remove(command);
}
/**
* Removes ObdCommand from the list of ObdCommands.
*
* @param command
* a {@link com.github.pires.obd.commands.ObdCommand} object.
*/
public void remove(ObdCommand command) {
this.commands.remove(command);
}

/**
* Iterate all commands, send them and read response.
*
* @param in a {@link java.io.InputStream} object.
* @param out a {@link java.io.OutputStream} object.
* @throws java.io.IOException if any.
* @throws java.lang.InterruptedException if any.
*/
public void sendCommands(InputStream in, OutputStream out)
throws IOException, InterruptedException {
for (ObdCommand command : commands)
command.run(in, out);
}
/**
* Iterate all commands, send them and read response.
*
* @param in
* a {@link java.io.InputStream} object.
* @param out
* a {@link java.io.OutputStream} object.
* @throws java.io.IOException
* if any.
* @throws java.lang.InterruptedException
* if any.
*/
public void sendCommands(InputStream in, OutputStream out) throws IOException, InterruptedException {
for (ObdCommand command : commands)
command.run(in, out);
}

/**
* <p>getFormattedResult.</p>
*
* @return a {@link java.lang.String} object.
*/
public String getFormattedResult() {
StringBuilder res = new StringBuilder();
for (ObdCommand command : commands)
res.append(command.getFormattedResult()).append(",");
/**
* <p>
* getFormattedResult.
* </p>
*
* @return a {@link java.lang.String} object.
*/
public String getFormattedResult() {
StringBuilder res = new StringBuilder();
for (ObdCommand command : commands)
res.append(command.getFormattedResult()).append(",");

return res.toString();
}
return res.toString();
}

}
101 changes: 55 additions & 46 deletions src/main/java/com/github/pires/obd/commands/PercentageObdCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,67 @@
*/
public abstract class PercentageObdCommand extends ObdCommand {

protected float percentage = 0f;
protected float percentage = 0f;

/**
* <p>Constructor for PercentageObdCommand.</p>
*
* @param command a {@link java.lang.String} object.
*/
public PercentageObdCommand(String command) {
super(command);
}
/**
* <p>
* Constructor for PercentageObdCommand.
* </p>
*
* @param command
* a {@link java.lang.String} object.
*/
public PercentageObdCommand(String command) {
super(command);
}

/**
* <p>Constructor for PercentageObdCommand.</p>
*
* @param other a {@link com.github.pires.obd.commands.PercentageObdCommand} object.
*/
public PercentageObdCommand(PercentageObdCommand other) {
super(other);
}
/**
* <p>
* Constructor for PercentageObdCommand.
* </p>
*
* @param other
* a {@link com.github.pires.obd.commands.PercentageObdCommand}
* object.
*/
public PercentageObdCommand(PercentageObdCommand other) {
super(other);
}

/** {@inheritDoc} */
@Override
protected void performCalculations() {
// ignore first two bytes [hh hh] of the response
percentage = (buffer.get(2) * 100.0f) / 255.0f;
}
/** {@inheritDoc} */
@Override
protected void performCalculations() {
// ignore first two bytes [hh hh] of the response
percentage = (buffer.get(2) * 100.0f) / 255.0f;
}

/** {@inheritDoc} */
@Override
public String getFormattedResult() {
return String.format("%.1f%s", percentage, getResultUnit());
}
/** {@inheritDoc} */
@Override
public String getFormattedResult() {
return String.format("%.1f%s", percentage, getResultUnit());
}

/**
* <p>Getter for the field <code>percentage</code>.</p>
*
* @return a float.
*/
public float getPercentage() {
return percentage;
}
/**
* <p>
* Getter for the field <code>percentage</code>.
* </p>
*
* @return a float.
*/
public float getPercentage() {
return percentage;
}

/** {@inheritDoc} */
@Override
public String getResultUnit() {
return "%";
}
/** {@inheritDoc} */
@Override
public String getResultUnit() {
return "%";
}

/** {@inheritDoc} */
@Override
public String getCalculatedResult() {
return String.valueOf(percentage);
}
/** {@inheritDoc} */
@Override
public String getCalculatedResult() {
return String.valueOf(percentage);
}

}
121 changes: 66 additions & 55 deletions src/main/java/com/github/pires/obd/commands/PersistentCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,76 @@
*/
public abstract class PersistentCommand extends ObdCommand {

private static Map<String, String> knownValues = new HashMap<>();
private static Map<String, ArrayList<Integer>> knownBuffers = new HashMap<>();
private static Map<String, String> knownValues = new HashMap<>();
private static Map<String, ArrayList<Integer>> knownBuffers = new HashMap<>();

/**
* <p>Constructor for PersistentCommand.</p>
*
* @param command a {@link java.lang.String} object.
*/
public PersistentCommand(String command) {
super(command);
}
/**
* <p>
* Constructor for PersistentCommand.
* </p>
*
* @param command
* a {@link java.lang.String} object.
*/
public PersistentCommand(String command) {
super(command);
}

/**
* <p>Constructor for PersistentCommand.</p>
*
* @param other a {@link com.github.pires.obd.commands.ObdCommand} object.
*/
public PersistentCommand(ObdCommand other) {
this(other.cmd);
}
/**
* <p>
* Constructor for PersistentCommand.
* </p>
*
* @param other
* a {@link com.github.pires.obd.commands.ObdCommand} object.
*/
public PersistentCommand(ObdCommand other) {
this(other.cmd);
}

/**
* <p>reset.</p>
*/
public static void reset() {
knownValues = new HashMap<>();
knownBuffers = new HashMap<>();
}
/**
* <p>
* reset.
* </p>
*/
public static void reset() {
knownValues = new HashMap<>();
knownBuffers = new HashMap<>();
}

/**
* <p>knows.</p>
*
* @param cmd a {@link java.lang.Class} object.
* @return a boolean.
*/
public static boolean knows(Class cmd) {
String key = cmd.getSimpleName();
return knownValues.containsKey(key);
}
/**
* <p>
* knows.
* </p>
*
* @param cmd
* a {@link java.lang.Class} object.
* @return a boolean.
*/
public static boolean knows(Class<?> cmd) {
String key = cmd.getSimpleName();
return knownValues.containsKey(key);
}

/** {@inheritDoc} */
@Override
protected void readResult(InputStream in) throws IOException {
super.readResult(in);
String key = getClass().getSimpleName();
knownValues.put(key, rawData);
knownBuffers.put(key, new ArrayList<>(buffer));
}
/** {@inheritDoc} */
@Override
protected void readResult(InputStream in) throws IOException {
super.readResult(in);
String key = getClass().getSimpleName();
knownValues.put(key, rawData);
knownBuffers.put(key, new ArrayList<>(buffer));
}

/** {@inheritDoc} */
@Override
public void run(InputStream in, OutputStream out) throws IOException, InterruptedException {
String key = getClass().getSimpleName();
if (knownValues.containsKey(key)) {
rawData = knownValues.get(key);
buffer = knownBuffers.get(key);
performCalculations();
} else {
super.run(in, out);
}
}
/** {@inheritDoc} */
@Override
public void run(InputStream in, OutputStream out) throws IOException, InterruptedException {
String key = getClass().getSimpleName();
if (knownValues.containsKey(key)) {
rawData = knownValues.get(key);
buffer = knownBuffers.get(key);
performCalculations();
} else {
super.run(in, out);
}
}
}
Loading