Search in sources :

Example 46 with List

use of java.util.List in project camel by apache.

the class BindyCsvFactory method generateCsvPositionMap.

/**
     * Generate a table containing the data formatted and sorted with their position/offset
     * If the model is Ordered than a key is created combining the annotation @Section and Position of the field
     * If a relation @OneToMany is defined, than we iterate recursively through this function
     * The result is placed in the Map<Integer, List> results
     */
private void generateCsvPositionMap(Class<?> clazz, Object obj, Map<Integer, List<String>> results) throws Exception {
    String result = "";
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        DataField datafield = field.getAnnotation(DataField.class);
        if (datafield != null) {
            if (obj != null) {
                // Retrieve the format, pattern and precision associated to the type
                Class<?> type = field.getType();
                // Create format
                FormattingOptions formattingOptions = ConverterUtils.convert(datafield, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
                Format<?> format = formatFactory.getFormat(formattingOptions);
                // Get field value
                Object value = field.get(obj);
                // If the field value is empty, populate it with the default value
                if (ObjectHelper.isNotEmpty(datafield.defaultValue()) && ObjectHelper.isEmpty(value)) {
                    value = datafield.defaultValue();
                }
                result = formatString(format, value);
                if (datafield.trim()) {
                    result = result.trim();
                }
                if (datafield.clip() && result.length() > datafield.length()) {
                    result = result.substring(0, datafield.length());
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Value to be formatted: {}, position: {}, and its formatted value: {}", new Object[] { value, datafield.pos(), result });
                }
            } else {
                result = "";
            }
            Integer key;
            if (isMessageOrdered() && obj != null) {
                // Generate a key using the number of the section
                // and the position of the field
                Integer key1 = sections.get(obj.getClass().getName());
                Integer key2 = datafield.position();
                Integer keyGenerated = generateKey(key1, key2);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Key generated: {}, for section: {}", String.valueOf(keyGenerated), key1);
                }
                key = keyGenerated;
            } else {
                key = datafield.pos();
            }
            if (!results.containsKey(key)) {
                List<String> list = new LinkedList<String>();
                list.add(result);
                results.put(key, list);
            } else {
                List<String> list = results.get(key);
                list.add(result);
            }
        }
        OneToMany oneToMany = field.getAnnotation(OneToMany.class);
        if (oneToMany != null) {
            // Set global variable
            // Will be used during generation of CSV
            isOneToMany = true;
            List<?> list = (List<?>) field.get(obj);
            if (list != null) {
                Iterator<?> it = list.iterator();
                while (it.hasNext()) {
                    Object target = it.next();
                    generateCsvPositionMap(target.getClass(), target, results);
                }
            } else {
                // Call this function to add empty value
                // in the table
                generateCsvPositionMap(field.getClass(), null, results);
            }
        }
    }
}
Also used : BindyConverter(org.apache.camel.dataformat.bindy.annotation.BindyConverter) OneToMany(org.apache.camel.dataformat.bindy.annotation.OneToMany) LinkedList(java.util.LinkedList) Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 47 with List

use of java.util.List in project camel by apache.

the class ArgumentSubstitutionParser method processResults.

@Override
public List<ApiMethodModel> processResults(List<ApiMethodModel> parseResult) {
    final List<ApiMethodModel> result = new ArrayList<ApiMethodModel>();
    for (ApiMethodModel model : parseResult) {
        // look for method name matches
        for (Map.Entry<Pattern, Map<Pattern, List<NameReplacement>>> methodEntry : methodMap.entrySet()) {
            // match the whole method name
            if (methodEntry.getKey().matcher(model.getName()).matches()) {
                // look for arg name matches
                final List<ApiMethodArg> updatedArguments = new ArrayList<ApiMethodArg>();
                final Map<Pattern, List<NameReplacement>> argMap = methodEntry.getValue();
                for (ApiMethodArg argument : model.getArguments()) {
                    final Class<?> argType = argument.getType();
                    final String typeArgs = argument.getTypeArgs();
                    final String argTypeName = argType.getCanonicalName();
                    for (Map.Entry<Pattern, List<NameReplacement>> argEntry : argMap.entrySet()) {
                        final Matcher matcher = argEntry.getKey().matcher(argument.getName());
                        // match argument name substring
                        if (matcher.find()) {
                            final List<NameReplacement> adapters = argEntry.getValue();
                            for (NameReplacement adapter : adapters) {
                                if (adapter.typePattern == null) {
                                    // no type pattern
                                    final String newName = getJavaArgName(matcher.replaceAll(adapter.replacement));
                                    argument = new ApiMethodArg(newName, argType, typeArgs);
                                } else {
                                    final Matcher typeMatcher = adapter.typePattern.matcher(argTypeName);
                                    if (typeMatcher.find()) {
                                        if (!adapter.replaceWithType) {
                                            // replace argument name
                                            final String newName = getJavaArgName(matcher.replaceAll(adapter.replacement));
                                            argument = new ApiMethodArg(newName, argType, typeArgs);
                                        } else {
                                            // replace name with argument type name
                                            final String newName = getJavaArgName(typeMatcher.replaceAll(adapter.replacement));
                                            argument = new ApiMethodArg(newName, argType, typeArgs);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    updatedArguments.add(argument);
                }
                model = new ApiMethodModel(model.getUniqueName(), model.getName(), model.getResultType(), updatedArguments, model.getMethod());
            }
        }
        result.add(model);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 48 with List

use of java.util.List in project camel by apache.

the class URISupport method resolveRawParameterValues.

/**
     * Traverses the given parameters, and resolve any parameter values which uses the RAW token
     * syntax: <tt>key=RAW(value)</tt>. This method will then remove the RAW tokens, and replace
     * the content of the value, with just the value.
     *
     * @param parameters the uri parameters
     * @see #parseQuery(String)
     * @see #RAW_TOKEN_START
     * @see #RAW_TOKEN_END
     */
@SuppressWarnings("unchecked")
public static void resolveRawParameterValues(Map<String, Object> parameters) {
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        if (entry.getValue() != null) {
            // if the value is a list then we need to iterate
            Object value = entry.getValue();
            if (value instanceof List) {
                List list = (List) value;
                for (int i = 0; i < list.size(); i++) {
                    Object obj = list.get(i);
                    if (obj != null) {
                        String str = obj.toString();
                        if (str.startsWith(RAW_TOKEN_START) && str.endsWith(RAW_TOKEN_END)) {
                            str = str.substring(4, str.length() - 1);
                            // update the string in the list
                            list.set(i, str);
                        }
                    }
                }
            } else {
                String str = entry.getValue().toString();
                if (str.startsWith(RAW_TOKEN_START) && str.endsWith(RAW_TOKEN_END)) {
                    str = str.substring(4, str.length() - 1);
                    entry.setValue(str);
                }
            }
        }
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 49 with List

use of java.util.List in project camel by apache.

the class MessageHistoryCopyExchangeTest method testDefensiveCopyOfMessageHistory.

public void testDefensiveCopyOfMessageHistory() throws Exception {
    MockEndpoint a = getMockEndpoint("mock:a");
    a.expectedMessageCount(1);
    MockEndpoint b = getMockEndpoint("mock:b");
    b.expectedMessageCount(1);
    template.sendBody("seda:start", "Hello World");
    assertMockEndpointsSatisfied();
    List listA = (List) a.getReceivedExchanges().get(0).getProperty(Exchange.MESSAGE_HISTORY);
    List listB = (List) b.getReceivedExchanges().get(0).getProperty(Exchange.MESSAGE_HISTORY);
    assertNotSame(listA, listB);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) List(java.util.List)

Example 50 with List

use of java.util.List in project camel by apache.

the class SplitterCollateTest method testSplitterCollate.

public void testSplitterCollate() throws Exception {
    getMockEndpoint("mock:line").expectedMessageCount(2);
    List<Object> data = new ArrayList<Object>();
    data.add("A");
    data.add("B");
    data.add("C");
    data.add("D");
    data.add("E");
    template.sendBody("direct:start", data);
    assertMockEndpointsSatisfied();
    List chunk = getMockEndpoint("mock:line").getReceivedExchanges().get(0).getIn().getBody(List.class);
    List chunk2 = getMockEndpoint("mock:line").getReceivedExchanges().get(1).getIn().getBody(List.class);
    assertEquals(3, chunk.size());
    assertEquals(2, chunk2.size());
    assertEquals("A", chunk.get(0));
    assertEquals("B", chunk.get(1));
    assertEquals("C", chunk.get(2));
    assertEquals("D", chunk2.get(0));
    assertEquals("E", chunk2.get(1));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

List (java.util.List)19204 ArrayList (java.util.ArrayList)12470 Test (org.junit.Test)4025 HashMap (java.util.HashMap)3622 Map (java.util.Map)3242 IOException (java.io.IOException)1670 Iterator (java.util.Iterator)1563 LinkedList (java.util.LinkedList)1336 HashSet (java.util.HashSet)1189 Set (java.util.Set)1151 File (java.io.File)921 ImmutableList (com.google.common.collect.ImmutableList)826 Collectors (java.util.stream.Collectors)784 LinkedHashMap (java.util.LinkedHashMap)540 Test (org.testng.annotations.Test)527 Session (org.hibernate.Session)521 Collection (java.util.Collection)496 Collections (java.util.Collections)474 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)471 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)453