forked from 42BV/CSVeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42BV#47 added EasyAbstractConverter class with pre-implemented toStri…
…ng() method
- Loading branch information
1 parent
ea825d9
commit fc1cc16
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/org/csveed/bean/conversion/EasyAbstractConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.csveed.bean.conversion; | ||
|
||
public abstract class EasyAbstractConverter<K> extends AbstractConverter<K> { | ||
|
||
public EasyAbstractConverter(Class<K> clazz) { | ||
super(clazz); | ||
} | ||
|
||
@Override | ||
public String toString(K value) throws Exception { | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.csveed.bean.conversion; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class Coordinate { | ||
|
||
private final Integer x; | ||
|
||
private final Integer y; | ||
|
||
public Coordinate(Integer x, Integer y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public Integer getX() { | ||
return x; | ||
} | ||
|
||
public Integer getY() { | ||
return y; | ||
} | ||
|
||
public static Coordinate fromString(String coordinateText) { | ||
Pattern r = Pattern.compile("(\\d+)/(\\d+)"); | ||
Matcher m = r.matcher(coordinateText); | ||
if (m.find()) { | ||
return new Coordinate( | ||
Integer.parseInt(m.group(1)), | ||
Integer.parseInt(m.group(2)) | ||
); | ||
} | ||
return null; | ||
} | ||
|
||
public String toString() { | ||
return x + "/" + y; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/org/csveed/bean/conversion/EasyAbstractConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.csveed.bean.conversion; | ||
|
||
import org.junit.Test; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
|
||
public class EasyAbstractConverterTest { | ||
|
||
@Test | ||
public void testEasyAbstractConverter() throws Exception { | ||
Converter<Coordinate> converter = new EasyAbstractConverter<Coordinate>(Coordinate.class) { | ||
@Override | ||
public Coordinate fromString(String text) throws Exception { | ||
return Coordinate.fromString(text); | ||
} | ||
}; | ||
Coordinate coords = converter.fromString("11/38"); | ||
assertEquals((Integer)11, coords.getX()); | ||
assertEquals((Integer)38, coords.getY()); | ||
} | ||
} |