@Override public void allocateFilterResources(GL2 gl) { OptionalInt shader = ShaderUtil.createFragmentShader(gl, createSource()); if (shader.isPresent()) { linkedShader = shader.getAsInt(); OptionalInt program = ShaderUtil.createSingleShaderProgram(gl, linkedShader); if (program.isPresent()) { linkedProgram = program.getAsInt(); } else { ShaderUtil.deleteShaderProgram(gl, linkedProgram); linkedProgram = 0; } } }
public ListenableFuture<?> partitionPage(Page page) { requireNonNull(page, "page is null"); Page partitionFunctionArgs = getPartitionFunctionArguments(page); for (int position = 0; position < page.getPositionCount(); position++) { if (nullChannel.isPresent() && page.getBlock(nullChannel.getAsInt()).isNull(position)) { for (PageBuilder pageBuilder : pageBuilders) { pageBuilder.declarePosition(); for (int channel = 0; channel < sourceTypes.size(); channel++) { Type type = sourceTypes.get(channel); type.appendTo(page.getBlock(channel), position, pageBuilder.getBlockBuilder(channel)); } } } else { int partition = partitionFunction.getPartition(partitionFunctionArgs, position); PageBuilder pageBuilder = pageBuilders.get(partition); pageBuilder.declarePosition(); for (int channel = 0; channel < sourceTypes.size(); channel++) { Type type = sourceTypes.get(channel); type.appendTo(page.getBlock(channel), position, pageBuilder.getBlockBuilder(channel)); } } } return flush(false); }
@Override protected DataBuffer encodeBespoke() { int size = 3 * Short.BYTES; for (Model model : Arrays.asList(primary, secondary)) { size += model.getId().isPresent() ? Short.BYTES : Byte.BYTES; size += model.getAnimation().isPresent() ? Short.BYTES : Byte.BYTES; } DataBuffer buffer = DataBuffer.allocate(size); for (OptionalInt optional : Arrays.asList( primary.getId(), secondary.getId(), primary.getAnimation(), secondary.getAnimation())) { if (optional.isPresent()) { int id = optional.getAsInt(); buffer.putByte(id >> 8 + 1); buffer.putByte(id); } else { buffer.putBoolean(false); } } buffer.putShort(scale); buffer.putShort(pitch); buffer.putShort(roll); return buffer.flip().asReadOnlyBuffer(); }
public int partOne(Sample gift) { for (Map.Entry<Integer, Sample> entry : samples.entrySet()) { int sue = entry.getKey(); Sample sample = entry.getValue(); boolean found = true; for (String compound : gift.knownCompounds()) { OptionalInt optional = sample.amount(compound); if (!optional.isPresent()) { continue; } int knownAmount = optional.getAsInt(); int actualAmount = gift.amount(compound).getAsInt(); if (knownAmount != actualAmount) { found = false; break; } } if (found) { return sue; } } throw new IllegalArgumentException("Failed to find gift: " + gift + "."); }
public static void main(String... args) { List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2); Arrays.stream(numbers.toArray()).forEach(System.out::println); int calories = Dish.menu.stream().mapToInt(Dish::getCalories).sum(); System.out.println("Number of calories:" + calories); // max and OptionalInt OptionalInt maxCalories = Dish.menu.stream().mapToInt(Dish::getCalories).max(); int max; if (maxCalories.isPresent()) { max = maxCalories.getAsInt(); } else { // we can choose a default value max = 1; } System.out.println(max); // numeric ranges IntStream evenNumbers = IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0); System.out.println(evenNumbers.count()); Stream<int[]> pythagoreanTriples = IntStream.rangeClosed(1, 100) .boxed() .flatMap( a -> IntStream.rangeClosed(a, 100) .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0) .boxed() .map(b -> new int[] {a, b, (int) Math.sqrt(a * a + b * b)})); pythagoreanTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2])); }
public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("Ahem, I need at least 2 arguments please."); System.exit(1); return; } Pattern scalePat = Pattern.compile("-(?:-scale|s)(?:-(w|h))?=(\\d+)"); @SuppressWarnings("unchecked") Predicate<String> isVerbose = (Predicate<String>) (Object) Predicate.isEqual("-v").or(Predicate.isEqual("--verbose")); boolean verbose = Stream.of(args).anyMatch(isVerbose); OptionalInt[] packed = Stream.of(args) .map(scalePat::matcher) .filter(Matcher::matches) .map( matcher -> { OptionalInt[] ints = new OptionalInt[] {OptionalInt.empty(), OptionalInt.empty()}; OptionalInt scale = OptionalInt.of(Integer.parseInt(matcher.group(2))); if (matcher.group(1) == null) { ints[0] = ints[1] = scale; } else { if (matcher.group(1).equals("w")) { ints[0] = scale; } else { ints[1] = scale; } } return ints; }) .reduce( new OptionalInt[] {OptionalInt.empty(), OptionalInt.empty()}, (id, next) -> { OptionalInt[] ints = new OptionalInt[2]; OptionalInt aID = id[0]; OptionalInt bID = id[1]; OptionalInt aNx = next[0]; OptionalInt bNx = next[1]; ints[0] = aNx.isPresent() ? aNx : aID; ints[1] = bNx.isPresent() ? bNx : bID; return ints; }); int scaleWidth = packed[0].orElse(1); int scaleHeight = packed[1].orElse(1); Pattern lightPat = Pattern.compile("-(?:-light|l)=([dDbB]+)"); List<Function<Color, Color>> lightChanges = Stream.of(args) .map(lightPat::matcher) .filter(Matcher::matches) .flatMap( m -> m.group(1) .chars() .mapToObj( c -> c == 'd' || c == 'D' ? (Function<Color, Color>) Color::darker : (Function<Color, Color>) Color::brighter)) .collect(Collectors.toList()); args = Stream.of(args) .filter(isVerbose.or(scalePat.asPredicate()).or(lightPat.asPredicate()).negate()) .toArray(String[]::new); Random r = new Random(); int width = Integer.parseInt(args[0]); int height = Integer.parseInt(args[1]); String file = args.length > 2 ? args[2] : ":-"; if (verbose) { System.err.println("Generating an image..."); } byte[] pixels = new byte[width * height * 4]; r.nextBytes(pixels); BufferedImage created = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int i = 0, index = 0; i < pixels.length; i += 4, index++) { int x = index % width; int y = index / width; Color color = new Color( ((/* 0x0F */ 0xFF) << 24) | ((pixels[i + 1] & 0xFF) << 16) | ((pixels[i + 2] & 0xFF) << 8) | ((pixels[i + 3] & 0xFF))); for (Function<Color, Color> change : lightChanges) { color = change.apply(color); } created.setRGB(x, y, color.getRGB()); } int scaledWidth = width * scaleWidth; int scaledHeight = height * scaleHeight; BufferedImage tmp = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = tmp.createGraphics(); AffineTransform at = AffineTransform.getScaleInstance(scaleWidth, scaleHeight); g.drawRenderedImage(created, at); created = tmp; if (verbose) { System.err.println("Writing to file..."); } ImageIO.write( created, "PNG", file.equals(":-") ? System.out : Files.newOutputStream(Paths.get(file))); if (verbose) { System.err.println("Complete..."); } }
@Override public String toJson(OptionalInt object) { return object.isPresent() ? String.valueOf(object.getAsInt()) : NULL; }