Skip to content

Commit

Permalink
Replaced Enumeration with Iterator (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Sep 21, 2021
1 parent 9cc2ca7 commit a320a36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
21 changes: 11 additions & 10 deletions src/main/java/com/dalsemi/onewire/adapter/UPacketBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Enumeration;
import java.util.Vector;
import java.util.ArrayList;
import java.util.List;


/** UPacketBuilder contains the methods to build a communication packet
Expand Down Expand Up @@ -152,7 +152,7 @@ class UPacketBuilder {
/**
* Vector of raw send packets
*/
protected final Vector<RawSendPacket> packetsVector = new Vector<>();
protected final List<RawSendPacket> rawSendPackets = new ArrayList<>();

/**
* Flag to send only 'bit' commands to the DS2480
Expand Down Expand Up @@ -206,7 +206,7 @@ public void restart ()
{

// clear the vector list of packets
packetsVector.removeAllElements();
rawSendPackets.clear();

// truncate the packet to 0 length
packet.buffer.setLength(0);
Expand All @@ -225,7 +225,7 @@ public void restart ()
public void newPacket () {

// add the packet
packetsVector.addElement(packet);
rawSendPackets.add(packet);

// get a new packet
packet = new RawSendPacket();
Expand All @@ -234,15 +234,16 @@ public void newPacket () {
/**
* Retrieve List of raw send packets
*
* @return the enumeration of packets
* @return Packet list
*/
public Enumeration<RawSendPacket> getPackets () {
public List<RawSendPacket> getPackets () {

// put the last packet into the vector if it is non zero
if (packet.buffer.length() > 0)
// put the last packet into the vector if it is non-zero
if (packet.buffer.length() > 0) {
newPacket();
}

return packetsVector.elements();
return rawSendPackets;
}

//--------
Expand Down
17 changes: 3 additions & 14 deletions src/main/java/com/dalsemi/onewire/adapter/USerialAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.logging.log4j.ThreadContext;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Set;

/**
Expand Down Expand Up @@ -1519,7 +1518,7 @@ private void setStreamingSpeed(int operation) throws OneWireIOException {
// send command, no response at this baud rate
serial.flush();

RawSendPacket pkt = (RawSendPacket) uBuild.getPackets().nextElement();
RawSendPacket pkt = (RawSendPacket) uBuild.getPackets().iterator().next();
char[] temp_buf = new char[pkt.buffer.length()];

pkt.buffer.getChars(0, pkt.buffer.length(), temp_buf, 0);
Expand Down Expand Up @@ -1764,19 +1763,15 @@ private boolean uVerify() {
*/
private char[] uTransaction(UPacketBuilder tempBuild) throws OneWireIOException {

//int offset;

try {
// clear the buffers
serial.flush();
inBuffer.setLength(0);

// loop to send all of the packets
for (Enumeration<RawSendPacket> packet_enum = tempBuild.getPackets(); packet_enum.hasMoreElements();) {
// loop to send all the packets
for (RawSendPacket pkt : tempBuild.getPackets()) {

// get the next packet
RawSendPacket pkt = packet_enum.nextElement();

// bogus packet to indicate need to wait for long DS2480 alarm
// reset
if ((pkt.buffer.length() == 0) && (pkt.returnLength == 0)) {
Expand All @@ -1791,9 +1786,6 @@ private char[] uTransaction(UPacketBuilder tempBuild) throws OneWireIOException

pkt.buffer.getChars(0, pkt.buffer.length(), temp_buf, 0);

// remember number of bytes in input
//offset = inBuffer.length();

// send the packet
serial.write(temp_buf);

Expand All @@ -1806,9 +1798,6 @@ private char[] uTransaction(UPacketBuilder tempBuild) throws OneWireIOException

inBuffer.getChars(0, inBuffer.length(), ret_buffer, 0);

// check for extra bytes in inBuffer
//extraBytesReceived = (inBuffer.length() > tempBuild.totalReturnLength);

// clear the inbuffer
inBuffer.setLength(0);

Expand Down

0 comments on commit a320a36

Please sign in to comment.