Example usage for java.util List replaceAll

List of usage examples for java.util List replaceAll

Introduction

In this page you can find the example usage for java.util List replaceAll.

Prototype

default void replaceAll(UnaryOperator<E> operator) 

Source Link

Document

Replaces each element of this list with the result of applying the operator to that element.

Usage

From source file:org.apache.nifi.csv.TestCSVRecordReader.java

@Test
public void testSimpleParse() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(
            new File("src/test/resources/csv/single-bank-account.csv"));
            final CSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] record = reader.nextRecord().getValues();
        final Object[] expectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street", "My City",
                "MS", "11111", "USA" };
        Assert.assertArrayEquals(expectedValues, record);

        assertNull(reader.nextRecord());
    }//w  w w.  j  a  v  a  2  s.  c o  m
}

From source file:org.apache.nifi.csv.TestCSVRecordReader.java

@Test
public void testMultipleRecords() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(new File("src/test/resources/csv/multi-bank-account.csv"));
            final CSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] firstRecord = reader.nextRecord().getValues();
        final Object[] firstExpectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street",
                "My City", "MS", "11111", "USA" };
        Assert.assertArrayEquals(firstExpectedValues, firstRecord);

        final Object[] secondRecord = reader.nextRecord().getValues();
        final Object[] secondExpectedValues = new Object[] { "2", "Jane Doe", 4820.09D, "321 Your Street",
                "Your City", "NY", "33333", "USA" };
        Assert.assertArrayEquals(secondExpectedValues, secondRecord);

        assertNull(reader.nextRecord());
    }//from ww w .ja  v a 2 s  .  c  o m
}

From source file:org.apache.nifi.csv.TestCSVRecordReader.java

@Test
public void testExtraWhiteSpace() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(new File("src/test/resources/csv/extra-white-space.csv"));
            final CSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] firstRecord = reader.nextRecord().getValues();
        final Object[] firstExpectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street",
                "My City", "MS", "11111", "USA" };
        Assert.assertArrayEquals(firstExpectedValues, firstRecord);

        final Object[] secondRecord = reader.nextRecord().getValues();
        final Object[] secondExpectedValues = new Object[] { "2", "Jane Doe", 4820.09D, "321 Your Street",
                "Your City", "NY", "33333", "USA" };
        Assert.assertArrayEquals(secondExpectedValues, secondRecord);

        assertNull(reader.nextRecord());
    }//  w  w w. j ava 2 s  .c  om
}

From source file:org.apache.nifi.csv.TestCSVRecordReader.java

@Test
public void testMissingField() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    final String headerLine = "id, name, balance, address, city, state, zipCode, country";
    final String inputRecord = "1, John, 40.80, 123 My Street, My City, MS, 11111";
    final String csvData = headerLine + "\n" + inputRecord;
    final byte[] inputData = csvData.getBytes();

    try (final InputStream bais = new ByteArrayInputStream(inputData);
            final CSVRecordReader reader = createReader(bais, schema, format)) {

        final Record record = reader.nextRecord();
        assertNotNull(record);//from w w w . j  a v a 2s. c o  m

        assertEquals("1", record.getValue("id"));
        assertEquals("John", record.getValue("name"));
        assertEquals(40.8D, record.getValue("balance"));
        assertEquals("123 My Street", record.getValue("address"));
        assertEquals("My City", record.getValue("city"));
        assertEquals("MS", record.getValue("state"));
        assertEquals("11111", record.getValue("zipCode"));
        assertNull(record.getValue("country"));

        assertNull(reader.nextRecord());
    }
}

From source file:org.apache.nifi.csv.TestCSVRecordReader.java

@Test
public void testMultipleRecordsEscapedWithSpecialChar() throws IOException, MalformedRecordException {

    char delimiter = StringEscapeUtils.unescapeJava("\u0001").charAt(0);

    final CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withTrim().withQuote('"')
            .withDelimiter(delimiter);/*from  w  ww .j ava 2s .  c  o m*/
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(
            new File("src/test/resources/csv/multi-bank-account_escapedchar.csv"));
            final CSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] firstRecord = reader.nextRecord().getValues();
        final Object[] firstExpectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street",
                "My City", "MS", "11111", "USA" };
        Assert.assertArrayEquals(firstExpectedValues, firstRecord);

        final Object[] secondRecord = reader.nextRecord().getValues();
        final Object[] secondExpectedValues = new Object[] { "2", "Jane Doe", 4820.09D, "321 Your Street",
                "Your City", "NY", "33333", "USA" };
        Assert.assertArrayEquals(secondExpectedValues, secondRecord);

        assertNull(reader.nextRecord());
    }
}

From source file:org.apache.nifi.csv.TestJacksonCSVRecordReader.java

@Test
public void testSimpleParse() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(
            new File("src/test/resources/csv/single-bank-account.csv"));
            final JacksonCSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] record = reader.nextRecord().getValues();
        final Object[] expectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street", "My City",
                "MS", "11111", "USA" };
        Assert.assertArrayEquals(expectedValues, record);

        assertNull(reader.nextRecord());
    }//  w ww  .  j  a v a 2  s. c  o  m
}

From source file:org.apache.nifi.csv.TestJacksonCSVRecordReader.java

@Test
public void testMultipleRecords() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(new File("src/test/resources/csv/multi-bank-account.csv"));
            final JacksonCSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] firstRecord = reader.nextRecord().getValues();
        final Object[] firstExpectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street",
                "My City", "MS", "11111", "USA" };
        Assert.assertArrayEquals(firstExpectedValues, firstRecord);

        final Object[] secondRecord = reader.nextRecord().getValues();
        final Object[] secondExpectedValues = new Object[] { "2", "Jane Doe", 4820.09D, "321 Your Street",
                "Your City", "NY", "33333", "USA" };
        Assert.assertArrayEquals(secondExpectedValues, secondRecord);

        assertNull(reader.nextRecord());
    }//from  w  w  w.  j av  a2 s  .co m
}

From source file:org.apache.nifi.csv.TestJacksonCSVRecordReader.java

@Test
public void testExtraWhiteSpace() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(new File("src/test/resources/csv/extra-white-space.csv"));
            final JacksonCSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] firstRecord = reader.nextRecord().getValues();
        final Object[] firstExpectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street",
                "My City", "MS", "11111", "USA" };
        Assert.assertArrayEquals(firstExpectedValues, firstRecord);

        final Object[] secondRecord = reader.nextRecord().getValues();
        final Object[] secondExpectedValues = new Object[] { "2", "Jane Doe", 4820.09D, "321 Your Street",
                "Your City", "NY", "33333", "USA" };
        Assert.assertArrayEquals(secondExpectedValues, secondRecord);

        assertNull(reader.nextRecord());
    }//from   w  ww .jav  a  2 s.  c o m
}

From source file:org.apache.nifi.csv.TestJacksonCSVRecordReader.java

@Test
public void testMissingField() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    final String headerLine = "id, name, balance, address, city, state, zipCode, country";
    final String inputRecord = "1, John, 40.80, 123 My Street, My City, MS, 11111";
    final String csvData = headerLine + "\n" + inputRecord;
    final byte[] inputData = csvData.getBytes();

    try (final InputStream bais = new ByteArrayInputStream(inputData);
            final JacksonCSVRecordReader reader = createReader(bais, schema, format)) {

        final Record record = reader.nextRecord();
        assertNotNull(record);/*from ww  w  .j  a  v  a2s  .co  m*/

        assertEquals("1", record.getValue("id"));
        assertEquals("John", record.getValue("name"));
        assertEquals(40.8D, record.getValue("balance"));
        assertEquals("123 My Street", record.getValue("address"));
        assertEquals("My City", record.getValue("city"));
        assertEquals("MS", record.getValue("state"));
        assertEquals("11111", record.getValue("zipCode"));
        assertNull(record.getValue("country"));

        assertNull(reader.nextRecord());
    }
}

From source file:org.apache.nifi.csv.TestJacksonCSVRecordReader.java

@Test
public void testMultipleRecordsEscapedWithSpecialChar() throws IOException, MalformedRecordException {

    char delimiter = StringEscapeUtils.unescapeJava("\u0001").charAt(0);

    final CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withTrim().withQuote('"')
            .withDelimiter(delimiter);//  w  w  w . j a v  a 2 s  .co m
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);

    final RecordSchema schema = new SimpleRecordSchema(fields);

    try (final InputStream fis = new FileInputStream(
            new File("src/test/resources/csv/multi-bank-account_escapedchar.csv"));
            final JacksonCSVRecordReader reader = createReader(fis, schema, format)) {

        final Object[] firstRecord = reader.nextRecord().getValues();
        final Object[] firstExpectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street",
                "My City", "MS", "11111", "USA" };
        Assert.assertArrayEquals(firstExpectedValues, firstRecord);

        final Object[] secondRecord = reader.nextRecord().getValues();
        final Object[] secondExpectedValues = new Object[] { "2", "Jane Doe", 4820.09D, "321 Your Street",
                "Your City", "NY", "33333", "USA" };
        Assert.assertArrayEquals(secondExpectedValues, secondRecord);

        assertNull(reader.nextRecord());
    }
}