Search in sources :

Example 1 with Concatenate

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

the class StringComplexFieldActions method concatenate.

@AtlasFieldActionInfo(name = "Concatenate", sourceType = FieldType.ANY, targetType = FieldType.STRING, sourceCollectionType = CollectionType.ALL, targetCollectionType = CollectionType.NONE)
public static String concatenate(Action action, Object input) {
    if (action == null || !(action instanceof Concatenate)) {
        throw new IllegalArgumentException("Action must be a Concatenate action");
    }
    if (input == null) {
        return null;
    }
    Concatenate concat = (Concatenate) action;
    String delim = concat.getDelimiter() == null ? "" : concat.getDelimiter();
    Collection<?> inputs = collection(input);
    StringBuilder builder = new StringBuilder();
    for (Object entry : inputs) {
        if (builder.length() > 0) {
            builder.append(delim);
        }
        if (entry != null) {
            builder.append(entry.toString());
        }
    }
    return builder.toString();
}
Also used : Concatenate(io.atlasmap.v2.Concatenate) SubString(io.atlasmap.v2.SubString) AtlasFieldActionInfo(io.atlasmap.spi.AtlasFieldActionInfo)

Example 2 with Concatenate

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

the class StringComplexFieldActionsTest method testConcatenate.

@Test
public void testConcatenate() {
    Concatenate action = new Concatenate();
    assertEquals(null, StringComplexFieldActions.concatenate(action, null));
    assertEquals("1true2.0", StringComplexFieldActions.concatenate(action, new Object[] { 1, true, 2.0 }));
    assertEquals("1true2.0", StringComplexFieldActions.concatenate(action, Arrays.asList(1, true, 2.0)));
    Map<Object, Object> map = new LinkedHashMap<>();
    map.put(1, 1);
    map.put(true, true);
    map.put(2.0, 2.0);
    assertEquals("1true2.0", StringComplexFieldActions.concatenate(action, map));
    action.setDelimiter("-");
    assertEquals(null, StringComplexFieldActions.concatenate(action, null));
    assertEquals("1-true-2.0", StringComplexFieldActions.concatenate(action, new Object[] { 1, true, 2.0 }));
    assertEquals("1-true-2.0", StringComplexFieldActions.concatenate(action, Arrays.asList(1, true, 2.0)));
    assertEquals("1-true-2.0", StringComplexFieldActions.concatenate(action, map));
}
Also used : Concatenate(io.atlasmap.v2.Concatenate) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Concatenate (io.atlasmap.v2.Concatenate)2 AtlasFieldActionInfo (io.atlasmap.spi.AtlasFieldActionInfo)1 SubString (io.atlasmap.v2.SubString)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1