.recordFailure(throwable -> API.Match(throwable).of( Case($(instanceOf(WebServiceException.class)), true), Case($(), false)))
@Override public final StackManipulation stackManipulation() { return Match(type).<StackManipulation>of(API.Case( $(t -> t.represents(boolean.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(BOOLEAN_VALUEOF))
Session initializeSession(final String keyspaceName) { final Session s = this.cluster.connect(); // if the keyspace already exists, just return the session if (this.cluster.getMetadata().getKeyspace(keyspaceName) != null) { return s; } final Configuration configuration = getStorageConfig(); // Setting replication strategy based on value reading from the configuration: either "SimpleStrategy" or "NetworkTopologyStrategy" final Map<String, Object> replication = Match(configuration.get(REPLICATION_STRATEGY)).of( Case($("SimpleStrategy"), strategy -> HashMap.<String, Object> of("class", strategy, "replication_factor", configuration.get(REPLICATION_FACTOR))), Case($("NetworkTopologyStrategy"), strategy -> HashMap.<String, Object> of("class", strategy) .merge(Array.of(configuration.get(REPLICATION_OPTIONS)) .grouped(2) .toMap(array -> Tuple.of(array.get(0), Integer.parseInt(array.get(1))))))) .toJavaMap(); s.execute(createKeyspace(keyspaceName) .ifNotExists() .with() .replication(replication)); return s; }
/** * Makes the character counter colorful depending on the current amount typed relative to the maximum Twitter * allows. */ private void enableTweetLengthCheck() { tweetTextArea.textProperty().addListener((observable, oldValue, newValue) -> { final Color color = Match(newValue.length()).of( Case($(tweetLen -> tweetLen < 250), GREEN), Case($(tweetLen -> tweetLen >= 250 && tweetLen < 280), ORANGE), Case($(tweetLen -> tweetLen > 280), RED), Case($(tweetLen -> tweetLen == 280), BLUE) ); Platform.runLater(() -> { charactersLeft.setText(Integer.toString(newValue.length())); charactersLeft.setTextFill(color); }); }); }
/** * Maps the cause to a new exception if this is a {@code Failure} or returns this instance if this is a {@code Success}. * <p> * If none of the given cases matches the cause, the same {@code Failure} is returned. * * @param cases A not necessarily exhaustive sequence of cases that will be matched against a cause. * @return A new {@code Try} if this is a {@code Failure}, otherwise this. */ @GwtIncompatible @SuppressWarnings({ "unchecked", "varargs" }) default Try<T> mapFailure(Match.Case<? extends Throwable, ? extends Throwable>... cases) { if (isSuccess()) { return this; } else { final Option<Throwable> x = Match(getCause()).option(cases); return x.isEmpty() ? this : failure(x.get()); } }
/** * Builds an instance of a DataView. * DataRows are validated before creation, returning a Failure on error. * * @param table The underlying table. * @param rows The Data Rows. * @return Returns a DataView wrapped in a Try. */ public static Try<DataView> build(DataTable table, Iterable<DataRow> rows) { return Match(DataRowCollection.build(table, rows)).of( Case($Success($()), dataRows -> Try.success(new DataView(table, dataRows))), Case($Failure($()), Try::failure) ); } }
/** * Attempts to replace an existing item with a new item in the column. * A type check is performed before replacement. * * @param index The index the item is to be replaced at. * @param value The new item. * @return Returns a Success with the new modified DataColumn, or a Failure. */ @Override public Try<IDataColumn> replace(Integer index, Object value) { return Match(GenericExtensions.tryCast(this.type, value)).of( Case($Success($()), typedVal -> Try.of(() -> createColumn(this.data.update(index, typedVal)))), Case($Failure($()), DataTableException.tryError("tryReplace failed. Item of invalid type passed.")) ); }
/** * Performs a quick sort on a DataTable given the specified parameters. * * @param table The underlying table containing the data. * @param rows The collection of rows to be sorted. * @param sortItems The sort details. * @return Returns a sorted DataView. */ public static Try<DataView> quickSort(DataTable table, Seq<DataRow> rows, Seq<SortItem> sortItems) { return Match(validateSortColumns(table, sortItems)).of( Case($Success($()), cols -> Try.success(performQuickSort(table, rows, sortItems))), Case($Failure($()), Try::failure) ); }
/** * Attempts to insert a new item into the column. * A type check is performed before insertion. * * @param index The index the item is to be inserted at. * @param value The item required to be inserted. * @return Returns a Success with the new modified DataColumn, or a Failure. */ @Override public Try<IDataColumn> insert(Integer index, Object value) { return Match(GenericExtensions.tryCast(this.type, value)).of( Case($Success($()), typedVal -> Try.of(() -> createColumn(this.data.insert(index, typedVal)))), Case($Failure($()), DataTableException.tryError("tryInsert failed. Item of invalid type passed.")) ); }
private static CompactionOptions<?> compactionOptions(final Configuration configuration) { if (!configuration.has(COMPACTION_STRATEGY)) { return null; } final CompactionOptions<?> compactionOptions = Match(configuration.get(COMPACTION_STRATEGY)) .of( Case($("SizeTieredCompactionStrategy"), sizedTieredStategy()), Case($("DateTieredCompactionStrategy"), dateTieredStrategy()), Case($("LeveledCompactionStrategy"), leveledStrategy())); Array.of(configuration.get(COMPACTION_OPTIONS)) .grouped(2) .forEach(keyValue -> compactionOptions.freeformOption(keyValue.get(0), keyValue.get(1))); return compactionOptions; }
/** * Attempts to add / append a new item to the end of the column. * A type check is performed before addition. * * @param value The item required to be added. * @return Returns a Success with the new modified DataColumn, or a Failure. */ @Override public Try<IDataColumn> add(Object value) { return Match(GenericExtensions.tryCast(this.type, value)).of( Case($Success($()), typedVal -> Try.of(() -> createColumn(this.data.append(typedVal)))), Case($Failure($()), DataTableException.tryError("tryAdd failed. Item of invalid type passed.")) ); }
/** * Builds an instance of a DataTable. * Columns are validated before creation, returning a Failure on error. * * @param tableName The name of the table. * @param columns The column collection. * @return Returns a DataTable wrapped in a Try. */ public static Try<DataTable> build(String tableName, Stream<IDataColumn> columns) { return Match(validateColumns(columns)).of( Case($Success($()), cols -> Try.success(new DataTable(tableName, cols))), Case($Failure($()), Try::failure) ); }
/** * Returns a new DataTable with the data replaced at the specified index. * * @param idx The row index. * @param rowValues The new values to replaced the old ones. * @return Returns a new DataTable with the row inserted. */ public Try<DataTable> replace(int idx, Object[] rowValues) { return Match(mapValuesToColumns(Stream.of(rowValues))).of( Case($Success($()), values -> replaceRow(idx, values)), Case($Failure($()), Try::failure) ); }
/** * Returns a new DataTable with the additional row inserted at the specified index. * * @param idx The row index. * @param rowValues The values to insert into the row. * @return Returns a new DataTable with the row inserted. */ public Try<DataTable> insert(int idx, Object[] rowValues) { return Match(mapValuesToColumns(Stream.of(rowValues))).of( Case($Success($()), values -> insertRow(idx, values)), Case($Failure($()), Try::failure) ); }
/** * Dynamically either sends a normal tweet or a reply depending on whether the controller was used to prepare a * "normal" new tweet or a reply (i.e. if there was a value set for {@link #inReplyStatus}. */ private void send() { Match(inReplyStatus.getValue()).of( Case($(Objects::nonNull), this::sendReply), Case($(Objects::isNull), this::sendTweet) ).thenRunAsync( () -> Stream.of(timeline, mentions).forEach(RateLimited::refresh), CompletableFuture.delayedExecutor(2, TimeUnit.SECONDS) ); }
/** * Returns a new DataTable with the additional row appended. * * @param rowValues The values to append to the row. * @return Returns a new DataTable with the row appended. */ public Try<DataTable> add(Object[] rowValues) { return Match(mapValuesToColumns(Stream.of(rowValues))).of( Case($Success($()), this::addRow), Case($Failure($()), Try::failure) ); }
@Override public BinaryString decode(EncodedString input) { EncodedString corrected = Match(indexesOfInvalidParityBits(input).isEmpty()).of( Case($(true), () -> input), Case($(false), () -> withBitFlippedAt(input, indexesOfInvalidParityBits(input).reduce((a, b) -> a + b) - 1)) ); return extractor.stripHammingMetadata(corrected); }
private Try<DataTable> buildTable(Try<Seq<IDataColumn>> columns) { return Match(columns).of( Case($Success($()), cols -> DataTable.build(table.name(), cols)), Case($Failure($()), Try::failure) ); }
private static CompressionOptions compressionOptions(final Configuration configuration) { if (!configuration.get(CF_COMPRESSION)) { // No compression return noCompression(); } return Match(configuration.get(CF_COMPRESSION_TYPE)).of( Case($("LZ4Compressor"), lz4()), Case($("SnappyCompressor"), snappy()), Case($("DeflateCompressor"), deflate())) .withChunkLengthInKb(configuration.get(CF_COMPRESSION_BLOCK_SIZE)); }
private String toHammingCodeValue(int it, BinaryString input) { return Match(it + 1).of( Case($(HammingHelper::isPowerOfTwo), () -> helper.getParityBit(it, input)), Case($(), () -> helper.getDataBit(it, input)) ); } }