use of java.util.function.Consumer in project MantaroBot by Mantaro.
the class MusicOptions method onRegistry.
@Subscribe
public void onRegistry(OptionRegistryEvent e) {
registerOption("fairqueue:max", "Fair queue maximum", "Sets the maximum fairqueue value (max amount of the same song any user can add).\n" + "Example: `~>opts fairqueue max 5`", "Sets the maximum fairqueue value.", (event, args) -> {
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
if (args.length == 0) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify a positive integer.").queue();
return;
}
String much = args[0];
final int fq;
try {
fq = Integer.parseInt(much);
} catch (Exception ex) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid number").queue();
return;
}
guildData.setMaxFairQueue(fq);
dbGuild.save();
event.getChannel().sendMessage(EmoteReference.CORRECT + "Set max fair queue size to " + fq).queue();
});
registerOption("musicannounce:toggle", "Music announce toggle", "Toggles whether the bot will announce the new song playing or no.", event -> {
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
boolean t1 = guildData.isMusicAnnounce();
guildData.setMusicAnnounce(!t1);
event.getChannel().sendMessage(EmoteReference.CORRECT + "Set music announce to " + "**" + !t1 + "**").queue();
dbGuild.save();
});
registerOption("music:channel", "Music VC lock", "Locks the bot to a VC. You need the VC name.\n" + "Example: `~>opts music channel Music`", "Locks the music feature to the specified VC.", (event, args) -> {
if (args.length == 0) {
OptsCmd.onHelp(event);
return;
}
String channelName = String.join(" ", args);
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
Consumer<VoiceChannel> consumer = voiceChannel -> {
guildData.setMusicChannel(voiceChannel.getId());
dbGuild.save();
event.getChannel().sendMessage(EmoteReference.OK + "Music Channel set to: " + voiceChannel.getName()).queue();
};
VoiceChannel channel = Utils.findVoiceChannelSelect(event, channelName, consumer);
if (channel != null) {
consumer.accept(channel);
}
});
registerOption("music:queuelimit", "Music queue limit", "Sets a custom queue limit.\n" + "Example: `~>opts music queuelimit 90`", "Sets a custom queue limit.", (event, args) -> {
if (args.length == 0) {
OptsCmd.onHelp(event);
return;
}
boolean isNumber = args[0].matches("^[0-9]*$");
if (!isNumber) {
event.getChannel().sendMessage(EmoteReference.ERROR + "That's not a valid number!").queue();
return;
}
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
try {
int finalSize = Integer.parseInt(args[0]);
int applySize = finalSize >= 300 ? 300 : finalSize;
guildData.setMusicQueueSizeLimit((long) applySize);
dbGuild.save();
event.getChannel().sendMessage(String.format(EmoteReference.MEGA + "The queue limit on this server is now " + "**%d** songs.", applySize)).queue();
} catch (NumberFormatException ex) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You're trying to set too high of a number (which won't" + " be applied anyway), silly").queue();
}
});
registerOption("music:clearchannel", "Music channel clear", "Clears the specific music channel.", (event) -> {
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
guildData.setMusicChannel(null);
dbGuild.save();
event.getChannel().sendMessage(EmoteReference.CORRECT + "I can play music on all channels now").queue();
});
}
use of java.util.function.Consumer in project java-design-patterns by iluwatar.
the class DbCustomerDao method getAll.
/**
* @return a lazily populated stream of customers. Note the stream returned must be closed to
* free all the acquired resources. The stream keeps an open connection to the database till
* it is complete or is closed manually.
*/
@Override
public Stream<Customer> getAll() throws Exception {
Connection connection;
try {
connection = getConnection();
// NOSONAR
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS");
// NOSONAR
ResultSet resultSet = statement.executeQuery();
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE, Spliterator.ORDERED) {
@Override
public boolean tryAdvance(Consumer<? super Customer> action) {
try {
if (!resultSet.next()) {
return false;
}
action.accept(createCustomer(resultSet));
return true;
} catch (SQLException e) {
// NOSONAR
throw new RuntimeException(e);
}
}
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
} catch (SQLException e) {
throw new CustomException(e.getMessage(), e);
}
}
use of java.util.function.Consumer in project AgriCraft by AgriCraft.
the class TileEntitySeedStorage method addServerDebugInfo.
// Debug method
@Override
public void addServerDebugInfo(Consumer<String> consumer) {
final String info = this.getLockedSeed().map(s -> s.getPlant().getPlantName()).orElse("null");
final int mapSize = this.slots == null ? 0 : this.slots.size();
final int listSize = this.slotsList == null ? 0 : this.slotsList.size();
consumer.accept("Locked Seed: " + info);
consumer.accept("Nr of map entries: " + mapSize);
consumer.accept("Nr of list entries: " + listSize);
}
use of java.util.function.Consumer in project xtext-xtend by eclipse.
the class DeclarationsTest method testRemove.
@Test
public void testRemove() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class C {");
_builder.newLine();
_builder.append("\t");
_builder.append("def void m() {}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
final ClassDeclaration c = ((ClassDeclaration) _head);
final MutableClassDeclaration mutable = it.getTypeLookup().findClass(c.getQualifiedName());
final Consumer<MutableMemberDeclaration> _function_1 = (MutableMemberDeclaration it_1) -> {
it_1.remove();
};
mutable.getDeclaredMembers().forEach(_function_1);
Assert.assertTrue(IterableExtensions.isEmpty(mutable.getDeclaredMembers()));
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of java.util.function.Consumer in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_1.
@Test
public void testAnnotationValueSetting_1() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import java.util.List");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.*");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.*");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(ConfigurableAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation ConfigurableAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.append("BlackOrWhite color");
_builder.newLine();
_builder.append("\t");
_builder.append("BlackOrWhite[] colors");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?> type");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?>[] types");
_builder.newLine();
_builder.append("\t");
_builder.append("SomeAnnotation annotation");
_builder.newLine();
_builder.append("\t");
_builder.append("SomeAnnotation[] annotations");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("annotation SomeAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.append("boolean value");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("enum BlackOrWhite {");
_builder.newLine();
_builder.append("\t");
_builder.append("BLACK, WHITE");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class ConfigurableAnnotationProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val anno = annotatedClass.annotations.head");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val someAnnotationType = findTypeGlobally(SomeAnnotation)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val enumType = findTypeGlobally(\'myannotation.BlackOrWhite\') as EnumerationTypeDeclaration");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val white = enumType.findDeclaredValue(\'WHITE\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val black = enumType.findDeclaredValue(\'BLACK\')");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingValue = anno.getValue(\'color\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingValue != white)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"color\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithColor = annotatedClass.addAnnotation(newAnnotationReference(anno) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setEnumValue(\'color\', black)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(anno)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingColorsValue = annoWithColor.getValue(\'colors\') as Object[]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingColorsValue.get(0) != white && existingColorsValue.get(1) != black && existingColorsValue.length != 2)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"colors\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithColors = annotatedClass.addAnnotation(newAnnotationReference(annoWithColor) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setEnumValue(\'colors\', black, white)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithColor)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingType = annoWithColors.getValue(\'type\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingType != string)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"type\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithType = annotatedClass.addAnnotation(newAnnotationReference(annoWithColors) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setClassValue(\'type\', annotatedClass.newTypeReference)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithColors)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingTypes = annoWithType.getValue(\'types\') as Object[]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingTypes.get(0) != primitiveInt && existingTypes.get(1) != annotatedClass.newTypeReference && existingTypes.length != 2)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"types\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithTypes = annotatedClass.addAnnotation(newAnnotationReference(annoWithType) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setClassValue(\'types\', primitiveBoolean)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithType)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annotationReference = annoWithTypes.getAnnotationValue(\'annotation\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (someAnnotationType != annotationReference.annotationTypeDeclaration)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"someAnnotationType != annotationReference.annotationTypeDeclaration\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithAnnotation = annotatedClass.addAnnotation(newAnnotationReference(annoWithTypes) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setAnnotationValue(\'annotation\', ");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("newAnnotationReference(someAnnotationType) [");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("setBooleanValue(\'value\', false)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append(")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithTypes)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annotationReferences = annoWithAnnotation.getAnnotationArrayValue(\'annotations\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (annotationReferences.size != 2)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"annotationReferences.size != 2\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotationReferences.forEach [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("if (someAnnotationType != annotationTypeDeclaration)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("throw new AssertionError(\"someAnnotationType != annotationTypeDeclaration\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.addAnnotation(newAnnotationReference(annoWithAnnotation) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setAnnotationValue(\'annotations\', ");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("newAnnotationReference(someAnnotationType) [");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("setBooleanValue(\'value\', false)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("],");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("newAnnotationReference(someAnnotationType) [");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("setBooleanValue(\'value\', false)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append(")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithAnnotation)");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ConfigurableAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.*");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@ConfigurableAnnotation(");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("color=BlackOrWhite.WHITE, ");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("colors=#[BlackOrWhite.WHITE, BlackOrWhite.BLACK], ");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("type = String, ");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("types=#[Integer, MyClass],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("annotation=@SomeAnnotation(true),");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("annotations=#[@SomeAnnotation(true), @SomeAnnotation(true)]");
_builder_1.newLine();
_builder_1.append(")");
_builder_1.newLine();
_builder_1.append("class MyClass {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
Type _findTypeGlobally = it.getTypeLookup().findTypeGlobally("myannotation.BlackOrWhite");
final EnumerationTypeDeclaration colorEnum = ((EnumerationTypeDeclaration) _findTypeGlobally);
final Type annotationType = it.getTypeLookup().findTypeGlobally("myannotation.ConfigurableAnnotation");
final AnnotationReference annotation = clazz.findAnnotation(annotationType);
Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), annotation.getValue("color"));
Object _value = annotation.getValue("colors");
final Object[] colors = ((Object[]) _value);
Assert.assertEquals(2, colors.length);
Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), colors[0]);
Assert.assertEquals(colorEnum.findDeclaredValue("WHITE"), colors[1]);
Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(clazz), annotation.getValue("type"));
final TypeReference[] types = annotation.getClassArrayValue("types");
Assert.assertEquals(1, types.length);
Assert.assertEquals(it.getTypeReferenceProvider().getPrimitiveBoolean(), types[0]);
final Type someAnnotationType = it.getTypeLookup().findTypeGlobally("myannotation.SomeAnnotation");
final AnnotationReference annotationValue = annotation.getAnnotationValue("annotation");
Assert.assertNotNull(annotationValue);
Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
Assert.assertFalse(annotationValue.getBooleanValue("value"));
final AnnotationReference[] annotationsValue = annotation.getAnnotationArrayValue("annotations");
Assert.assertNotNull(annotationsValue);
Assert.assertEquals(2, ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).size());
final Consumer<AnnotationReference> _function_1 = (AnnotationReference it_1) -> {
Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
Assert.assertFalse(annotationValue.getBooleanValue("value"));
};
((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).forEach(_function_1);
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations