Skip to content

Commit

Permalink
42BV#47 added EasyAbstractConverter class with pre-implemented toStri…
Browse files Browse the repository at this point in the history
…ng() method
  • Loading branch information
robert-bor committed Aug 26, 2014
1 parent ea825d9 commit fc1cc16
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
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;
}

}
41 changes: 41 additions & 0 deletions src/test/java/org/csveed/bean/conversion/Coordinate.java
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;
}

}
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());
}
}

0 comments on commit fc1cc16

Please sign in to comment.