Search in sources :

Example 1 with ReplaceFirst

use of io.atlasmap.v2.ReplaceFirst in project atlasmap by atlasmap.

the class StringComplexFieldActions method replaceFirst.

@AtlasFieldActionInfo(name = "ReplaceFirst", sourceType = FieldType.STRING, targetType = FieldType.STRING, sourceCollectionType = CollectionType.NONE, targetCollectionType = CollectionType.NONE)
public static String replaceFirst(Action action, String input) {
    if (action == null || !(action instanceof ReplaceFirst)) {
        throw new IllegalArgumentException("Action must be a ReplaceFirst action");
    }
    ReplaceFirst replaceFirst = (ReplaceFirst) action;
    String match = replaceFirst.getMatch();
    if (match == null || match.length() == 0) {
        throw new IllegalArgumentException("ReplaceFirst action must be specified with a non-empty old string");
    }
    String newString = replaceFirst.getNewString();
    return input == null ? null : input.replaceFirst(match, newString == null ? "" : newString);
}
Also used : SubString(io.atlasmap.v2.SubString) ReplaceFirst(io.atlasmap.v2.ReplaceFirst) AtlasFieldActionInfo(io.atlasmap.spi.AtlasFieldActionInfo)

Example 2 with ReplaceFirst

use of io.atlasmap.v2.ReplaceFirst in project atlasmap by atlasmap.

the class StringComplexFieldActionsTest method testReplaceFirstEmptyMatch.

@Test(expected = IllegalArgumentException.class)
public void testReplaceFirstEmptyMatch() {
    ReplaceFirst replaceFirst = new ReplaceFirst();
    replaceFirst.setMatch("");
    StringComplexFieldActions.replaceFirst(replaceFirst, " ");
}
Also used : ReplaceFirst(io.atlasmap.v2.ReplaceFirst) Test(org.junit.Test)

Example 3 with ReplaceFirst

use of io.atlasmap.v2.ReplaceFirst in project atlasmap by atlasmap.

the class StringComplexFieldActionsTest method testReplaceFirstNullMatch.

@Test(expected = IllegalArgumentException.class)
public void testReplaceFirstNullMatch() {
    ReplaceFirst replaceFirst = new ReplaceFirst();
    StringComplexFieldActions.replaceFirst(replaceFirst, " ");
}
Also used : ReplaceFirst(io.atlasmap.v2.ReplaceFirst) Test(org.junit.Test)

Example 4 with ReplaceFirst

use of io.atlasmap.v2.ReplaceFirst in project atlasmap by atlasmap.

the class StringComplexFieldActionsTest method testReplaceFirst.

@Test
public void testReplaceFirst() {
    ReplaceFirst replaceFirst = new ReplaceFirst();
    replaceFirst.setMatch(" ");
    assertNull(StringComplexFieldActions.replaceFirst(replaceFirst, null));
    assertEquals("", StringComplexFieldActions.replaceFirst(replaceFirst, ""));
    assertEquals("test", StringComplexFieldActions.replaceFirst(replaceFirst, "test"));
    replaceFirst.setMatch("e");
    assertEquals("tst", StringComplexFieldActions.replaceFirst(replaceFirst, "test"));
    replaceFirst.setMatch("t");
    replaceFirst.setNewString("h");
    assertEquals("hest", StringComplexFieldActions.replaceFirst(replaceFirst, "test"));
    replaceFirst.setMatch("is");
    replaceFirst.setNewString("at");
    assertEquals("That is a test", StringComplexFieldActions.replaceFirst(replaceFirst, "This is a test"));
}
Also used : ReplaceFirst(io.atlasmap.v2.ReplaceFirst) Test(org.junit.Test)

Aggregations

ReplaceFirst (io.atlasmap.v2.ReplaceFirst)4 Test (org.junit.Test)3 AtlasFieldActionInfo (io.atlasmap.spi.AtlasFieldActionInfo)1 SubString (io.atlasmap.v2.SubString)1