Skip to content

Commit 9381c7d

Browse files
author
zhenya
committed
rename classes
1 parent ee8a1d2 commit 9381c7d

7 files changed

Lines changed: 139 additions & 138 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.iusenko.subrip.srt;
2+
3+
/**
4+
*
5+
* @author iusenko
6+
*/
7+
public class Phrase {
8+
9+
private int id;
10+
private long startTime;
11+
private long endTime;
12+
private String text;
13+
14+
public long getEndTime() {
15+
return endTime;
16+
}
17+
18+
public void setEndTime(long endTime) {
19+
this.endTime = endTime;
20+
}
21+
22+
public int getId() {
23+
return id;
24+
}
25+
26+
public void setId(int id) {
27+
this.id = id;
28+
}
29+
30+
public long getStartTime() {
31+
return startTime;
32+
}
33+
34+
public void setStartTime(long startTime) {
35+
this.startTime = startTime;
36+
}
37+
38+
public String getText() {
39+
return text;
40+
}
41+
42+
public void setText(String text) {
43+
this.text = text;
44+
}
45+
}

src/main/java/com/iusenko/subrip/srt/SrtUnitBuilder.java renamed to src/main/java/com/iusenko/subrip/srt/PhraseBuilder.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@
99
*
1010
* @author iusenko
1111
*/
12-
public class SrtUnitBuilder {
12+
public class PhraseBuilder {
1313

1414
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("hh:mm:ss,SSS");
1515
private static final String TIME_DELIM = "-->";
1616
private String timeline;
1717
private String id;
1818
private String text;
1919

20-
public SrtUnitBuilder timeline(String timeline) {
20+
public PhraseBuilder timeline(String timeline) {
2121
this.timeline = timeline;
2222
return this;
2323
}
2424

25-
public SrtUnitBuilder id(String id) {
25+
public PhraseBuilder id(String id) {
2626
this.id = id;
2727
return this;
2828
}
2929

30-
public SrtUnitBuilder text(String text) {
30+
public PhraseBuilder text(String text) {
3131
this.text = text;
3232
return this;
3333
}
3434

35-
public SrtUnit create() throws SrtParserException {
36-
SrtUnit unit = new SrtUnit();
35+
public Phrase create() throws PhraseParserException {
36+
Phrase unit = new Phrase();
3737
unit.setId(getId(id));
3838
String[] startEnd = splitTimeline(timeline);
3939
unit.setStartTime(getTimestamp(startEnd[0]));
@@ -42,34 +42,34 @@ public SrtUnit create() throws SrtParserException {
4242
return unit;
4343
}
4444

45-
protected String[] splitTimeline(String timeline) throws SrtParserException {
45+
protected String[] splitTimeline(String timeline) throws PhraseParserException {
4646
String[] startAndEnd = timeline.split(TIME_DELIM);
4747
if (startAndEnd.length != 2) {
48-
throw new SrtParserException("time line has wrong format:" + timeline);
48+
throw new PhraseParserException("time line has wrong format:" + timeline);
4949
}
5050
for (int i = 0; i < startAndEnd.length; i++) {
5151
startAndEnd[i] = startAndEnd[i].trim();
5252
if (Utils.isBlank(startAndEnd[i])) {
53-
throw new SrtParserException("blank " + (i == 0 ? "start" : "end") + " timestamp value");
53+
throw new PhraseParserException("blank " + (i == 0 ? "start" : "end") + " timestamp value");
5454
}
5555
}
5656
return startAndEnd;
5757
}
5858

59-
protected long getTimestamp(String timestamp) throws SrtParserException {
59+
protected long getTimestamp(String timestamp) throws PhraseParserException {
6060
try {
6161
Date date = DATE_FORMAT.parse(timestamp);
6262
return date.getTime();
6363
} catch (ParseException ex) {
64-
throw new SrtParserException("timestamp string has wrong format:" + timestamp);
64+
throw new PhraseParserException("timestamp string has wrong format:" + timestamp);
6565
}
6666
}
6767

68-
protected int getId(String id) throws SrtParserException {
68+
protected int getId(String id) throws PhraseParserException {
6969
try {
7070
return Integer.parseInt(id);
7171
} catch (NumberFormatException e) {
72-
throw new SrtParserException("id string has wrong format(not a number):" + id);
72+
throw new PhraseParserException("id string has wrong format(not a number):" + id);
7373
}
7474
}
7575
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iusenko.subrip.srt;
2+
3+
/**
4+
*
5+
* @author iusenko
6+
*/
7+
public class PhraseParserException extends Exception {
8+
9+
public PhraseParserException(String message) {
10+
super(message);
11+
}
12+
}

src/main/java/com/iusenko/subrip/srt/SrtParserException.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/main/java/com/iusenko/subrip/srt/SrtUnit.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.iusenko.subrip.srt;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
import org.junit.Test;
6+
7+
/**
8+
*
9+
* @author iusenko
10+
*/
11+
public class PhraseBuilderTest {
12+
13+
private PhraseBuilder parser = new PhraseBuilder();
14+
15+
@Test
16+
public void testGetTimestamp() throws Exception {
17+
long l1 = parser.getTimestamp("00:00:20,000");
18+
long l2 = parser.getTimestamp("00:00:20,010");
19+
System.out.println("l1=" + l1 + ", l2=" + l2 + ", l2-l1=" + (l2 - l1));
20+
assertEquals(10, l2 - l1);
21+
22+
l1 = parser.getTimestamp("1:2:2,00");
23+
l2 = parser.getTimestamp("1:2:2,10");
24+
System.out.println("l1=" + l1 + ", l2=" + l2 + ", l2-l1=" + (l2 - l1));
25+
assertEquals(10, l2 - l1);
26+
}
27+
28+
@Test(expected = PhraseParserException.class)
29+
public void testGetTimestampError() throws Exception {
30+
parser.getTimestamp("00:0020,000");
31+
}
32+
33+
@Test
34+
public void testGetId() throws PhraseParserException {
35+
assertEquals(10, parser.getId("10"));
36+
assertEquals(24, parser.getId("24"));
37+
assertEquals(31, parser.getId("31"));
38+
}
39+
40+
@Test
41+
public void testGetIdError() throws PhraseParserException {
42+
String[] vals = { "a", ",", "0x1" };
43+
for (String val : vals) {
44+
try {
45+
parser.getId(val);
46+
fail("should never reach this state");
47+
} catch (Exception e) {
48+
}
49+
}
50+
}
51+
52+
@Test
53+
public void testSplitTimeline() throws PhraseParserException {
54+
String[] startEnd = parser
55+
.splitTimeline("00:02:31,567 --> 00:02:37,164 ");
56+
assertEquals("00:02:31,567", startEnd[0]);
57+
assertEquals("00:02:37,164", startEnd[1]);
58+
}
59+
60+
@Test(expected = PhraseParserException.class)
61+
public void testSplitTimelineErrorStart() throws PhraseParserException {
62+
parser.splitTimeline(" --> 00:02:37,164 ");
63+
}
64+
65+
@Test(expected = PhraseParserException.class)
66+
public void testSplitTimelineErrorEnd() throws PhraseParserException {
67+
parser.splitTimeline("00:02:31,567 --> ");
68+
}
69+
}

src/test/java/com/iusenko/subrip/srt/SrtUnitBuilderTest.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)