/** * this method will call initialize for each message, since we are caching the entity indexes, we * don't worry about aggregating by app id * * @param indexOperationMessage */ private void initializeEntityIndexes(final IndexOperationMessage indexOperationMessage) { // create a set so we can have a unique list of appIds for which we call createEntityIndex Set<UUID> appIds = new HashSet<>(); // loop through all indexRequests and add the appIds to the set indexOperationMessage .getIndexRequests() .forEach( req -> { UUID appId = IndexingUtils.getApplicationIdFromIndexDocId(req.documentId); appIds.add(appId); }); // loop through all deindexRequests and add the appIds to the set indexOperationMessage .getDeIndexRequests() .forEach( req -> { UUID appId = IndexingUtils.getApplicationIdFromIndexDocId(req.documentId); appIds.add(appId); }); // for each of the appIds in the unique set, call create entity index to ensure the aliases are // created appIds.forEach( appId -> { ApplicationScope appScope = CpNamingUtils.getApplicationScope(appId); entityIndexFactory.createEntityIndex( indexLocationStrategyFactory.getIndexLocationStrategy(appScope)); }); }
@OnMessage public void handleMessage(String message, Session session) { logger.info("Received message " + message); peers.forEach( peer -> { if (session != peer) { peer.getAsyncRemote().sendObject(message); } }); }
@Test public void testInference() { QueryTree queryTree = new QueryTree(); JoinCondition jc1 = new JoinCondition( SAMPLED_TABLE_NAME, FOREIGN_KEY_COLUMN, FOREIGN_TABLE_NAME, FOREIGN_TABLE_KEY_COLUMN); queryTree.getJoinConditions().add(jc1); TableInfo sampledTableInfo = new TableInfo(); sampledTableInfo.setName(SAMPLED_TABLE_NAME); sampledTableInfo.setColumns(columnSet); TableInfo foreignTableInfo = new TableInfo(); foreignTableInfo.setName(FOREIGN_TABLE_NAME); foreignTableInfo.setColumns(foreignColumnSet); foreignTableInfo.setPrimaryKeys(new HashSet<>(Arrays.asList(FOREIGN_TABLE_KEY_COLUMN))); Set<TableInfo> tableInfoSet = new HashSet<TableInfo>(); tableInfoSet.add(sampledTableInfo); tableInfoSet.add(foreignTableInfo); FuncDependencies funcDep = new FuncDependencies(queryTree, tableInfoSet); Set<String> det = new HashSet<String>( Arrays.asList( Utility.formatColumnName(SAMPLED_TABLE_NAME, KEY1), Utility.formatColumnName( SAMPLED_TABLE_NAME, FuncDependencies.ANNOTATION_COLUMN_NAME))); Set<String> dep = new HashSet<>(); columnSet.forEach( (columnName) -> dep.add(Utility.formatColumnName(SAMPLED_TABLE_NAME, columnName))); foreignColumnSet.forEach( (columnName) -> dep.add(Utility.formatColumnName(FOREIGN_TABLE_NAME, columnName))); /*Infer that P.KEY1, P.pi ----> HEAD(P) and HEAD(Q)*/ Assert.assertTrue("Inferrence failed", funcDep.infer(det, dep)); }
public static String fromJExpr2KExprString(Expression jexpr, Set<SimpleName> names) { HashMap<String, String> typeEnv = new HashMap<>(); HashMap<String, String> fromJVarName2KVarName = extractJVar2KVarMapping(names.stream()); names.forEach( name -> { typeEnv.put( fromJVarName2KVarName.get(name.getIdentifier()), name.resolveTypeBinding().getName()); }); return fromJExpr2KExprString(jexpr, fromJVarName2KVarName, typeEnv); }
@Override public List<String> runTests() { final List<String> errors = new ArrayList<>(); try { final T instance = (T) clazz.newInstance(); methodsUnderTest.forEach( field -> testSetter(instance, field).ifPresent(value -> errors.add(value))); } catch (IllegalAccessException | InstantiationException e) { throw new TestAidException("Setter test failed to run", e); } return errors; }
// batch job (possibly real-time using streaming APIs) public static void createProductAssociativityGraphPerCategory() { Set<String> customers = purchasesCache.keySet(); customers.forEach( customer -> { List<String> customerPurchases = Lists.newArrayList(purchasesCache.get(customer)); for (int i = 0; i < customerPurchases.size(); i++) { for (int j = i + 1; j < customerPurchases.size(); j++) { Product product1 = productCache.get(customerPurchases.get(i)); Product product2 = productCache.get(customerPurchases.get(j)); if (product1.category.equals(product2.category)) { ProductAssociativityGraph graph = productAssociativityGraphMap.getOrDefault( product1.category, ProductAssociativityGraph.create()); graph.addAssociation(Vertex.create(product1.id), Vertex.create(product2.id), 1); productAssociativityGraphMap.putIfAbsent(product1.category, graph); } } } }); }
/** * Puts all the given staff into the given lessonPlans, avoiding all given lessonPlans. Returns * true if it successfully put the staff into the lessonPlans. * * @param lessonPlans The lessonPlans to have staff put in. * @param staffList The staff to put into the lessonPlans. * @param overloadedPeriods The lessonPlans which overlap. * @return True if successfully put the staff into the lessonPlans. */ private boolean putStaffIntoLessonPlan( List<LessonPlan> lessonPlans, List<Staff> staffList, List<Set<LessonPlan>> overloadedPeriods) { boolean done = false; while (!done) { Set<SubjectSet> subjectSets = new HashSet<>(); lessonPlans.forEach(lessonPlan -> subjectSets.add(lessonPlan.subjectSet)); for (SubjectSet subjectSet : subjectSets) { List<LessonPlan> lessonsForSubjectSet = lessonPlans .stream() // Turn list into an iterating stream .filter( lessonPlan -> lessonPlan.subjectSet.id == subjectSet .id) // Only add lessonsPlans who's subjectSet is the subjectSet // currently being processed. .collect( Collectors.toCollection( ArrayList::new)); // Collect all which meet the filter into an ArrayList and // return it /*List<LessonPlan> lessonsForSubjectSet = new ArrayList<>(); //This is the equivalent of the above for (LessonPlan lessonPlan : lessonPlans) { if (lessonPlan.subjectSet.id == subjectSet.id) { lessonsForSubjectSet.add(lessonPlan); } }*/ if (lessonsForSubjectSet.get(0).staff.id == -1) { // Only check first as all will be set if the first is set Set<SubjectSet> possibleConflicts = new HashSet<>(); // A set containing every subjectSet which happens at the same time // as the current subjectSet for (Set<LessonPlan> overloadedPeriod : overloadedPeriods) { if (overloadedPeriod .stream() .anyMatch(lessonPlan -> lessonPlan.subjectSet.id == subjectSet.id)) { // Only a set rather than List(Set) as do not need to know when the conflict is, overloadedPeriod.forEach( lessonPlan -> possibleConflicts.add( lessonPlan.subjectSet)); // only there is a conflict between the current // subjectSet and the subjectSets // in the set } } for (Staff staff : staffList) { if (staff.currentHoursPerWeek >= staff.hoursPerWeek) { break; } Set<SubjectSet> subjectSetsForStaff = new HashSet<>(); // A set containing the subjectSets taught by the current staff lessonsForSubjectSet.forEach( lessonPlan -> { if (lessonPlan.staff.id != -1 && lessonPlan.id != staff.id) { subjectSetsForStaff.add(lessonPlan.subjectSet); } }); if (Collections.disjoint( possibleConflicts, subjectSetsForStaff)) { // disjoint returns true if the collections has no elements // in common staff.currentHoursPerWeek += lessonsForSubjectSet.size(); for (LessonPlan lessonPlan : lessonPlans) { if (lessonPlan.subjectSet.id == subjectSet.id) { LessonPlan lesson = new LessonPlan( lessonPlan.id, staff, lessonPlan.classroom, lessonPlan.period, lessonPlan.subjectSet); lessonPlans.set(lessonPlans.indexOf(lessonPlan), lesson); } } } } } } done = lessonPlans .stream() .allMatch( lessonPlan -> lessonPlan.staff != null); // Checks if every lessonPlan has a member of staff teaching it } return true; }
private void updateConfig() { JSONArray chats = new JSONArray(); availableChats.forEach(chats::put); config.put("chats", chats); }
/** * Provides the Observer with a new item to observe. * * <p>The {@link com.caricah.iotracah.core.modules.Server} may call this method 0 or more times. * * <p>The {@code Observable} will not call this method again after it calls either {@link * #onCompleted} or {@link #onError}. * * @param iotMessage the item emitted by the Observable */ @Override public void onNext(IOTMessage iotMessage) { getExecutorService() .submit( () -> { log.info(" onNext : received {}", iotMessage); try { IOTMessage response = null; switch (iotMessage.getMessageType()) { case ConnectMessage.MESSAGE_TYPE: ConnectMessage connectMessage = (ConnectMessage) iotMessage; response = ConnectAcknowledgeMessage.from( connectMessage.isDup(), connectMessage.getQos(), connectMessage.isRetain(), connectMessage.getKeepAliveTime(), MqttConnectReturnCode.CONNECTION_ACCEPTED); break; case SubscribeMessage.MESSAGE_TYPE: SubscribeMessage subscribeMessage = (SubscribeMessage) iotMessage; List<Integer> grantedQos = new ArrayList<>(); subscribeMessage .getTopicFilterList() .forEach( topic -> { String topicKey = quickCheckIdKey( "", Arrays.asList(topic.getKey().split(Constant.PATH_SEPARATOR))); Set<String> channelIds = subscriptions.get(topicKey); if (Objects.isNull(channelIds)) { channelIds = new HashSet<>(); } channelIds.add(subscribeMessage.getConnectionId()); subscriptions.put(topicKey, channelIds); grantedQos.add(topic.getValue()); }); response = SubscribeAcknowledgeMessage.from( subscribeMessage.getMessageId(), grantedQos); break; case UnSubscribeMessage.MESSAGE_TYPE: UnSubscribeMessage unSubscribeMessage = (UnSubscribeMessage) iotMessage; response = UnSubscribeAcknowledgeMessage.from(unSubscribeMessage.getMessageId()); break; case Ping.MESSAGE_TYPE: response = iotMessage; break; case PublishMessage.MESSAGE_TYPE: PublishMessage publishMessage = (PublishMessage) iotMessage; Set<String> matchingTopics = getMatchingSubscriptions("", publishMessage.getTopic()); for (String match : matchingTopics) { Set<String> channelIds = subscriptions.get(match); if (Objects.nonNull(channelIds)) { channelIds.forEach( id -> { PublishMessage clonePublishMessage = publishMessage.cloneMessage(); clonePublishMessage.copyTransmissionData(iotMessage); clonePublishMessage.setConnectionId(id); pushToServer(clonePublishMessage); }); } } if (MqttQoS.AT_MOST_ONCE.value() == publishMessage.getQos()) { break; } else if (MqttQoS.AT_LEAST_ONCE.value() == publishMessage.getQos()) { response = AcknowledgeMessage.from(publishMessage.getMessageId()); break; } case PublishReceivedMessage.MESSAGE_TYPE: case ReleaseMessage.MESSAGE_TYPE: case CompleteMessage.MESSAGE_TYPE: case DisconnectMessage.MESSAGE_TYPE: case AcknowledgeMessage.MESSAGE_TYPE: default: DisconnectMessage disconnectMessage = DisconnectMessage.from(true); disconnectMessage.copyTransmissionData(iotMessage); throw new ShutdownException(disconnectMessage); } if (Objects.nonNull(response)) { response.copyTransmissionData(iotMessage); pushToServer(response); } } catch (ShutdownException e) { IOTMessage response = e.getResponse(); if (Objects.nonNull(response)) { pushToServer(response); } } catch (Exception e) { log.error(" onNext : Serious error that requires attention ", e); } }); }
protected final void fireChangeEvent() { listeners.forEach(MarkerModelListener::markerChanged); }
@Override public void metadataUpdated() { listeners.forEach(MetadataListener::metadataUpdated); }
@Override public void gameOver(Set<String> usernames) { usernames.forEach(roomContainer::remove); }