List of usage examples for java.util.stream Stream collect
<R, A> R collect(Collector<? super T, A, R> collector);
From source file:org.openecomp.sdc.be.model.operations.impl.ComponentOperation.java
public Either<Map<String, List<RequirementDefinition>>, TitanOperationStatus> getRequirements( Component component, NodeTypeEnum nodeTypeEnum, boolean inTransaction) { final HashMap<String, List<RequirementDefinition>> emptyMap = new HashMap<>(); Either<Map<String, List<RequirementDefinition>>, TitanOperationStatus> eitherResult = Either.left(emptyMap); try {//from w w w. j a va 2 s. c o m List<ComponentInstance> componentInstances = component.getComponentInstances(); if (componentInstances != null) { Function<ComponentInstance, Either<List<ImmutablePair<RequirementData, GraphEdge>>, TitanOperationStatus>> dataCollector = e -> componentInstanceOperation .getRequirements(e, nodeTypeEnum); Either<List<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<RequirementData, GraphEdge>>, TitanOperationStatus>>>, TitanOperationStatus> eitherDataCollected = collectDataFromComponentsInstances( componentInstances, dataCollector); if (eitherDataCollected.isRight()) { eitherResult = Either.right(eitherDataCollected.right().value()); } else { // Converts Data to Def stop if encountered conversion error DataDefConvertor<RequirementDefinition, RequirementData> someConvertor = (e1, e2) -> convertReqDataListToReqDefList(e1, e2); Either<List<List<RequirementDefinition>>, TitanOperationStatus> fullDefList = convertDataToDefComponentLevel( eitherDataCollected.left().value(), someConvertor); if (fullDefList.isRight()) { eitherResult = Either.right(fullDefList.right().value()); } else { Stream<RequirementDefinition> defStream = fullDefList.left().value().stream() .flatMap(e -> e.stream()); // Collect to Map and using grouping by Map<String, List<RequirementDefinition>> capTypeCapListMap = defStream .collect(Collectors.groupingBy(e -> e.getCapability())); eitherResult = Either.left(capTypeCapListMap); } } } } finally { if (inTransaction == false) { titanGenericDao.commit(); } } return eitherResult; }
From source file:org.apache.usergrid.corepersistence.asyncevents.AsyncEventServiceImpl.java
/** * calls the event handlers and returns a result with information on whether it needs to be ack'd and whether it needs to be indexed * @param messages/*from w w w. jav a 2 s .co m*/ * @return */ private List<IndexEventResult> callEventHandlers(final List<QueueMessage> messages) { if (logger.isDebugEnabled()) { logger.debug("callEventHandlers with {} message(s)", messages.size()); } Stream<IndexEventResult> indexEventResults = messages.stream().map(message -> { if (logger.isDebugEnabled()) { logger.debug("Queue message with ID {} has been received {} time(s)", message.getMessageId(), message.getReceiveCount()); } AsyncEvent event = null; try { event = (AsyncEvent) message.getBody(); } catch (ClassCastException cce) { logger.error("Failed to deserialize message body", cce); return new IndexEventResult(Optional.absent(), Optional.absent(), System.currentTimeMillis()); } if (event == null) { logger.error("AsyncEvent type or event is null!"); return new IndexEventResult(Optional.absent(), Optional.absent(), System.currentTimeMillis()); } final AsyncEvent thisEvent = event; if (logger.isDebugEnabled()) { logger.debug("Processing event with type {}", event.getClass().getSimpleName()); } try { IndexOperationMessage single = new IndexOperationMessage(); // normal indexing event for an entity if (event instanceof EntityIndexEvent) { single = handleEntityIndexUpdate(message); } // normal indexing event for an edge else if (event instanceof EdgeIndexEvent) { single = handleEdgeIndex(message); } // deletes are 2-part, actual IO to delete data, then queue up a de-index else if (event instanceof EdgeDeleteEvent) { single = handleEdgeDelete(message); } // deletes are 2-part, actual IO to delete data, then queue up a de-index else if (event instanceof EntityDeleteEvent) { single = handleEntityDelete(message); } // initialization has special logic, therefore a special event type and no index operation message else if (event instanceof InitializeApplicationIndexEvent) { handleInitializeApplicationIndex(event, message); } // this is the main event that pulls the index doc from map persistence and hands to the index producer else if (event instanceof ElasticsearchIndexEvent) { handleIndexOperation((ElasticsearchIndexEvent) event); } else if (event instanceof DeIndexOldVersionsEvent) { single = handleDeIndexOldVersionEvent((DeIndexOldVersionsEvent) event); } else { throw new Exception("Unknown EventType for message: " + message.getStringBody().trim()); } if (!(event instanceof ElasticsearchIndexEvent) && !(event instanceof InitializeApplicationIndexEvent) && single.isEmpty()) { logger.warn("No index operation messages came back from event processing for msg: {} ", message.getStringBody().trim()); } // if no exception happens and the QueueMessage is returned in these results, it will get ack'd return new IndexEventResult(Optional.of(single), Optional.of(message), thisEvent.getCreationTime()); } catch (IndexDocNotFoundException e) { // this exception is throw when we wait before trying quorum read on map persistence. // return empty event result so the event's message doesn't get ack'd if (logger.isDebugEnabled()) { logger.debug(e.getMessage()); } return new IndexEventResult(Optional.absent(), Optional.absent(), thisEvent.getCreationTime()); } catch (Exception e) { // NPEs don't have a detail message, so add something for our log statement to identify better final String errorMessage; if (e instanceof NullPointerException) { errorMessage = "NullPointerException"; } else { errorMessage = e.getMessage(); } // if the event fails to process, log and return empty message result so it doesn't get ack'd logger.error("{}. Failed to process message: {}", errorMessage, message.getStringBody().trim()); return new IndexEventResult(Optional.absent(), Optional.absent(), thisEvent.getCreationTime()); } }); return indexEventResults.collect(Collectors.toList()); }
From source file:ca.oson.json.Oson.java
public static <T> Field[] getFields(Class<T> valueType) { if (ignored(valueType)) { return new Field[0]; }//from w ww .ja v a 2 s.c o m if (cachedFields.containsKey(valueType)) { return cachedFields.get(valueType); } Stream<Field> stream = Arrays.stream(valueType.getDeclaredFields()); Class valueTypeAll = valueType.getSuperclass(); while (valueTypeAll != null && valueTypeAll != Object.class) { stream = Stream.concat(stream, Arrays.stream(valueTypeAll.getDeclaredFields())); valueTypeAll = valueTypeAll.getSuperclass(); } //(Field[]) stream.distinct().toArray(size -> new Field[size]) List<Field> uniqueFields = new ArrayList<>(); Set<String> set = new HashSet<>(); for (Field field : stream.collect(Collectors.toList())) { String name = field.getName().toLowerCase(); Class ftype = field.getType(); String fname = ftype.getName(); // && ftype != valueType if (!set.contains(name) && !name.startsWith("this$") && ftype != Class.class && ftype != Field.class && ftype != Field[].class && ftype != sun.reflect.ReflectionFactory.class // && ftype != ClassLoader.class && !fname.startsWith("java.security.") && !fname.startsWith("[Ljava.security.") && !fname.startsWith("java.lang.Class$") && !fname.startsWith("sun.reflect.")) { set.add(name); uniqueFields.add(field); } } cachedFields.put(valueType, uniqueFields.toArray(new Field[uniqueFields.size()])); return cachedFields.get(valueType); }