Skip to content

Commit

Permalink
Merge pull request #17 from psibre/tostring
Browse files Browse the repository at this point in the history
toString
  • Loading branch information
psibre authored Jun 20, 2018
2 parents 0cbd814 + b0cacbf commit 7496ee1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ JTGT

- publish Javadoc artifact
- link to hosted Javadoc
- JSON-compatible String representation of TextGrid objects

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ repositories {
}

dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
compile group: 'org.json', name: 'json', version: '20180130'
testCompile group: 'org.testng', name: 'testng', version: '6.8.13'
testCompile group: 'commons-io', name: 'commons-io', version: '2.6'
}

test {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/m2ci/msp/jtgt/Annotation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.m2ci.msp.jtgt;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.json.JSONObject;

/**
* TextGrid annotation wrapper class
Expand Down Expand Up @@ -104,6 +107,11 @@ public boolean equals(Object o) {
(getEnd() == ((Annotation) o).getEnd()) &&
getText().equals(((Annotation) o).getText()));
}

public String toString() {
String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString();
return new JSONObject(jsonStr).toString(4);
}
}


Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/m2ci/msp/jtgt/TextGrid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.m2ci.msp.jtgt;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.json.JSONObject;

import java.util.ArrayList;

/**
Expand Down Expand Up @@ -243,6 +247,11 @@ public boolean equals(Object o) {
(((getFilename() == null) && (((TextGrid) o).getFilename() == null)) || getFilename().equals(((TextGrid) o).getFilename())) &&
(getTiers().equals(((TextGrid) o).getTiers())));
}

public String toString() {
String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString();
return new JSONObject(jsonStr).toString(4);
}
}


Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/m2ci/msp/jtgt/Tier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.m2ci.msp.jtgt;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.json.JSONObject;

import java.util.ArrayList;

/**
Expand Down Expand Up @@ -183,6 +187,11 @@ public boolean equals(Object o) {
getName().equals(((Tier) o).getName()) &&
getAnnotations().equals(((Tier) o).getAnnotations()));
}

public String toString() {
String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString();
return new JSONObject(jsonStr).toString(4);
}
}

/* Tier.java ends here */
37 changes: 37 additions & 0 deletions src/test/java/org/m2ci/msp/jtgt/TextGridTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.m2ci.msp.jtgt;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.m2ci.msp.jtgt.annotation.IntervalAnnotation;
import org.m2ci.msp.jtgt.tier.IntervalTier;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class TextGridTest {

@Test
public void testToString() throws IOException {
// create TextGrid
IntervalAnnotation foo = new IntervalAnnotation(0, 0.5, "foo");
IntervalAnnotation bar = new IntervalAnnotation(0.5, 1, "bar");
IntervalTier tier = new IntervalTier("foobar");
tier.addAnnotation(foo);
tier.addAnnotation(bar);
tier.setStart(foo.getStart());
tier.setEnd(bar.getEnd());
TextGrid tg = new TextGrid();
tg.addTier(tier);
tg.setStart(tier.getStart());
tg.setEnd(tier.getEnd());
// compare as JSON
String actualStr = tg.toString();
String expectedStr = IOUtils.resourceToString("/org/m2ci/msp/jtgt/foobar.json", StandardCharsets.UTF_8);
Map actual = new JSONObject(actualStr).toMap();
Map expected = new JSONObject(expectedStr).toMap();
Assert.assertEquals(actual, expected);
}
}
24 changes: 24 additions & 0 deletions src/test/resources/org/m2ci/msp/jtgt/foobar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"_filename": null,
"_start": 0,
"_end": 1,
"_tiers": [
{
"_start": 0,
"_end": 1,
"_name": "foobar",
"_annotations": [
{
"_start": 0,
"_end": 0.5,
"_text": "foo"
},
{
"_start": 0.5,
"_end": 1,
"_text": "bar"
}
]
}
]
}

0 comments on commit 7496ee1

Please sign in to comment.