use of java.util.stream.Stream in project L42 by ElvisResearchGroup.
the class Desugar method visit.
public Expression visit(ClassB s) {
Position pos = s.getP();
assert !(s.getH() instanceof ConcreteHeader);
if (!s.getFields().isEmpty()) {
List<Member> ms = s.getFields().stream().flatMap(f -> Desugar.field(pos, f)).collect(Collectors.toList());
ms.addAll(s.getMs());
s = s.withMs(ms).withH(new Ast.TraitHeader());
}
Set<String> oldUsedVars = this.usedVars;
HashMap<String, Type> oldVarEnv = this.varEnv;
try {
s = (ClassB) super.visit(s);
s = FlatFirstLevelLocalNestedClasses.of(s, this);
s = DesugarCatchDefault.of(s);
return s;
} finally {
this.usedVars = oldUsedVars;
this.varEnv = oldVarEnv;
}
}
use of java.util.stream.Stream in project jdk8u_jdk by JetBrains.
the class StreamZipEntriesTest method testClosedZipFile.
@Test
public void testClosedZipFile() throws IOException {
ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), "input.zip"));
zf.close();
try {
Stream s = zf.stream();
fail("Should have thrown IllegalStateException");
} catch (IllegalStateException e) {
// expected;
}
}
use of java.util.stream.Stream in project jdk8u_jdk by JetBrains.
the class ConcurrentAssociateTest method testOnce.
private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
CountDownLatch s = new CountDownLatch(1);
Supplier<Runnable> sr = () -> () -> {
try {
s.await();
} catch (InterruptedException e) {
}
for (int i = 0; i < N; i++) {
Object o = new X();
associator.accept(m, o);
if (!m.containsKey(o)) {
throw new AssociationFailure(desc + " failed: entry does not exist");
}
}
};
int ps = Runtime.getRuntime().availableProcessors();
Stream<CompletableFuture> runners = IntStream.range(0, ps).mapToObj(i -> sr.get()).map(CompletableFuture::runAsync);
CompletableFuture all = CompletableFuture.allOf(runners.toArray(CompletableFuture[]::new));
// Trigger the runners to start associating
s.countDown();
try {
all.join();
} catch (CompletionException e) {
Throwable t = e.getCause();
if (t instanceof AssociationFailure) {
throw (AssociationFailure) t;
} else {
throw e;
}
}
}
use of java.util.stream.Stream in project jdk8u_jdk by JetBrains.
the class CountTest method testOps.
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
AtomicLong expectedCount = new AtomicLong();
data.stream().forEach(e -> expectedCount.incrementAndGet());
withData(data).terminal(Stream::count).expectedResult(expectedCount.get()).exercise();
}
use of java.util.stream.Stream in project MantaroBot by Mantaro.
the class PollLobby method startPoll.
public void startPoll() {
try {
if (!isCompilant) {
getChannel().sendMessage(EmoteReference.WARNING + "This poll cannot build. " + "**Remember that the maximum amount of options are 9, the minimum is 2 and that the maximum timeout is 10m and the minimum timeout is 30s.**\n" + "Options are separated with a comma, for example `1,2,3`. For spaced stuff use commas at the start and end of the sentence.").queue();
getRunningPolls().remove(getChannel());
return;
}
if (isPollAlreadyRunning(getChannel())) {
getChannel().sendMessage(EmoteReference.WARNING + "There seems to be another poll running here...").queue();
return;
}
if (!event.getGuild().getSelfMember().hasPermission(getChannel(), Permission.MESSAGE_ADD_REACTION)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Seems like I cannot add reactions here...").queue();
getRunningPolls().remove(getChannel());
return;
}
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData data = dbGuild.getData();
AtomicInteger at = new AtomicInteger();
data.setRanPolls(data.getRanPolls() + 1L);
dbGuild.save();
String toShow = Stream.of(options).map(opt -> String.format("#%01d.- %s", at.incrementAndGet(), opt)).collect(Collectors.joining("\n"));
EmbedBuilder builder = new EmbedBuilder().setAuthor(String.format("Poll #%1d created by %s", data.getRanPolls(), event.getAuthor().getName()), null, event.getAuthor().getAvatarUrl()).setDescription("**Poll started. React to the number to vote.**\n*" + name + "*").addField("Options", "```md\n" + toShow + "```", false).setColor(event.getMember().getColor()).setThumbnail("https://cdn.pixabay.com/photo/2012/04/14/16/26/question-34499_960_720.png").setFooter("You have " + Utils.getDurationMinutes(timeout) + " minutes to vote.", event.getAuthor().getAvatarUrl());
getChannel().sendMessage(builder.build()).queue(message -> ReactionOperations.create(message, TimeUnit.MILLISECONDS.toSeconds(timeout), new ReactionOperation() {
@Override
public boolean run(MessageReactionAddEvent e) {
int i = e.getReactionEmote().getName().charAt(0) - '0';
if (i < 1 || i > options.length)
return false;
return false;
}
@Override
public void onExpire() {
EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Poll results").setDescription("**Showing results for the poll started by " + event.getAuthor().getName() + "** with name: *" + name + "*").setFooter("Thanks for your vote", null);
AtomicInteger react = new AtomicInteger(0);
AtomicInteger counter = new AtomicInteger(0);
String votes = new ArrayList<>(getChannel().getMessageById(message.getIdLong()).complete().getReactions()).stream().filter(r -> react.getAndIncrement() <= options.length).map(r -> "+Registered " + (r.getCount() - 1) + " votes for option " + options[counter.getAndIncrement()]).collect(Collectors.joining("\n"));
embedBuilder.addField("Results", "```diff\n" + votes + "```", false);
event.getChannel().sendMessage(embedBuilder.build()).queue();
}
}, reactions(options.length)));
} catch (Exception e) {
getChannel().sendMessage(EmoteReference.ERROR + "An unknown error has occurred while setting up a poll. Maybe try again?").queue();
}
}
Aggregations