Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add clean method to release objects. #3

Open
wants to merge 3 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.project
.settings
.classpath
.gitignore
96 changes: 54 additions & 42 deletions lib/src/main/java/com/tragicphantom/stdf/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,72 @@
*
* Stdf4j is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with stdf4j. If not, see <http://www.gnu.org/licenses/>.
**/
* along with stdf4j. If not, see <http://www.gnu.org/licenses/>.
**/
package com.tragicphantom.stdf;

import java.nio.ByteOrder;

import java.text.ParseException;

public class Record{
private RecordDescriptor desc;
private int pos;
private byte [] data;
private ByteOrder byteOrder;
private RecordData rd;
public class Record {

private RecordDescriptor desc;

private int pos;

private byte[] data;

private ByteOrder byteOrder;

private RecordData rd;

public Record(RecordDescriptor desc, int pos, byte[] data, ByteOrder byteOrder) {
this.desc = desc;
this.pos = pos;
this.data = data;
this.byteOrder = byteOrder;
this.rd = null;
}

public Record(RecordDescriptor desc, int pos,
byte [] data, ByteOrder byteOrder){
this.desc = desc;
this.pos = pos;
this.data = data;
this.byteOrder = byteOrder;
this.rd = null;
}
public Record(RecordDescriptor desc, RecordData rd) {
this.desc = desc;
this.rd = rd;
this.pos = -1;
}

public Record(RecordDescriptor desc, RecordData rd){
this.desc = desc;
this.rd = rd;
this.pos = -1;
}
public String getType() {
return this.desc.getType();
}

public String getType(){
return desc.getType();
}
public int getPosition() {
return this.pos;
}

public int getPosition(){
return pos;
}
public RecordData getData() throws ParseException {
if (this.rd == null) {
this.rd = this.desc.parse(this.pos, this.data, this.byteOrder);
}
return this.rd;
}

public RecordData getData() throws ParseException{
if(rd == null)
rd = desc.parse(pos, data, byteOrder);
return rd;
}
public void clean() {
if (this.rd != null) {
this.rd.clean();
}
this.rd = null;
}

public String toString(){
try{
return getData().toString();
}
catch(Exception e){
return "(null)";
}
}
@Override
public String toString() {
try {
return getData().toString();
}
catch (Exception e) {
return "(null)";
}
}
}
265 changes: 140 additions & 125 deletions lib/src/main/java/com/tragicphantom/stdf/RecordData.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,136 +10,151 @@
*
* Stdf4j is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with stdf4j. If not, see <http://www.gnu.org/licenses/>.
**/
* along with stdf4j. If not, see <http://www.gnu.org/licenses/>.
**/
package com.tragicphantom.stdf;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Calendar;
import java.util.TimeZone;

public class RecordData{
private RecordDescriptor desc;
private Object [] fields;

// TODO: make configurable
//private static String dateFormat = "%1$tY-%1$tm-%1$tdT%1$tT";
private static String dateFormat = "%1$ta %1$tb %1$td %1$tT %1$tY";

public RecordData(RecordDescriptor desc, Object [] fields){
this.desc = desc;
this.fields = fields;
}

public String getType(){
return desc.getType();
}

public int size(){
return fields.length;
}

public boolean hasField(String name){
return desc.contains(name);
}

public void setField(int index, Object value){
fields[index] = value;
}

public void setField(String name, Object value){
setField(desc.getIndex(name), value);
}

public Object getField(String name){
return getField(desc.getIndex(name));
}

public Object getField(int index){
return fields[index];
}

public HashMap<String, Object> getFields(){
HashMap<String, Object> values = new HashMap<String, Object>();
int index = 0;
for(Field field : desc.getFields())
values.put(field.getName(), fields[index++]);
return values;
}

public String getString(String name){
int index = desc.getIndex(name);
Object value = getField(index);
char type = desc.getFields()[index].getType();

String repr;
if(value == null)
repr = "(null)";
else if(type == 'V' || type == 'k'){
StringBuilder lb = new StringBuilder();
for(Object item : (Object[])value){
if(lb.length() > 0)
lb.append(", ");
lb.append(item == null ? "(null)" : item.toString());
}
repr = lb.toString();
}
else if(value instanceof Double)
repr = String.format("%f", (Double)value);
else if(value instanceof Float)
repr = String.format("%f", (Float)value);
else if(value instanceof Byte)
repr = String.format("%x", (Byte)value);
else if(value instanceof byte[]){
StringBuilder bb = new StringBuilder("[");
for(byte b : (byte[])value){
if(bb.length() > 1)
bb.append(", ");
bb.append(Byte.toString(b));
}
bb.append("]");
repr = bb.toString();
}
else if((name.endsWith("_T") && !name.equals("TEST_T"))
|| name.equals("MOD_TIM")){
Calendar cal = Calendar.getInstance();
//cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis((Long)value * 1000L);
repr = String.format("%1$ta %1$tb %1$td %1$tT %1$tY", cal);
}
else
repr = value.toString();

if(repr.length() == 0)
repr = "(null)";

return repr;
}

// this method has been tailored to output similar to libstdf format
// to help with comparing output of the two libraries
public String toString(){
StringBuilder sb = new StringBuilder(getType().toUpperCase());
sb.append("\n");

// sort fields for easier comparisons
ArrayList<String> names = new ArrayList<String>(desc.getFieldNames());
Collections.sort(names);

for(String name : names){
sb.append(" ")
.append(name)
.append(": ")
.append(getString(name))
.append("\n");
}

return sb.toString();
}
import java.util.Collections;
import java.util.HashMap;

public class RecordData {

private RecordDescriptor desc;

private Object[] fields;

// TODO: make configurable
//private static String dateFormat = "%1$tY-%1$tm-%1$tdT%1$tT";
private static String dateFormat = "%1$ta %1$tb %1$td %1$tT %1$tY";

public RecordData(RecordDescriptor desc, Object[] fields) {
this.desc = desc;
this.fields = fields;
}

public String getType() {
return this.desc.getType();
}

public int size() {
return this.fields.length;
}

public boolean hasField(String name) {
return this.desc.contains(name);
}

public void setField(int index, Object value) {
this.fields[index] = value;
}

public void setField(String name, Object value) {
setField(this.desc.getIndex(name), value);
}

public Object getField(String name) {
return getField(this.desc.getIndex(name));
}

public Object getField(int index) {
return this.fields[index];
}

public HashMap<String, Object> getFields() {
HashMap<String, Object> values = new HashMap<String, Object>();
int index = 0;
for (Field field : this.desc.getFields()) {
values.put(field.getName(), this.fields[index++]);
}
return values;
}

public void clean() {
this.fields = new Object[0];
}

public String getString(String name) {
int index = this.desc.getIndex(name);
Object value = getField(index);
char type = this.desc.getFields()[index].getType();

String repr;
if (value == null) {
repr = "(null)";
}
else if (type == 'V' || type == 'k') {
StringBuilder lb = new StringBuilder();
for (Object item : (Object[]) value) {
if (lb.length() > 0) {
lb.append(", ");
}
lb.append(item == null ? "(null)" : item.toString());
}
repr = lb.toString();
}
else if (value instanceof Double) {
repr = String.format("%f", (Double) value);
}
else if (value instanceof Float) {
repr = String.format("%f", (Float) value);
}
else if (value instanceof Byte) {
repr = String.format("%x", (Byte) value);
}
else if (value instanceof byte[]) {
StringBuilder bb = new StringBuilder("[");
for (byte b : (byte[]) value) {
if (bb.length() > 1) {
bb.append(", ");
}
bb.append(Byte.toString(b));
}
bb.append("]");
repr = bb.toString();
}
else if ((name.endsWith("_T") && !name.equals("TEST_T"))
|| name.equals("MOD_TIM")) {
Calendar cal = Calendar.getInstance();
//cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis((Long) value * 1000L);
repr = String.format("%1$ta %1$tb %1$td %1$tT %1$tY", cal);
}
else {
repr = value.toString();
}

if (repr.length() == 0) {
repr = "(null)";
}

return repr;
}

// this method has been tailored to output similar to libstdf format
// to help with comparing output of the two libraries
@Override
public String toString() {
StringBuilder sb = new StringBuilder(getType().toUpperCase());
sb.append("\n");

// sort fields for easier comparisons
ArrayList<String> names = new ArrayList<String>(this.desc.getFieldNames());
Collections.sort(names);

for (String name : names) {
sb.append(" ")
.append(name)
.append(": ")
.append(getString(name))
.append("\n");
}

return sb.toString();
}
}