/** * Try to generate a move/swap fromRegion between fromServer and toServer such that locality is improved. * Returns empty optional if no move can be found */ private Optional<Action> tryMoveOrSwap(Cluster cluster, int fromServer, int fromRegion, int toServer) { // Try move first. We know apriori fromRegion has the highest locality on toServer if (cluster.serverHasTooFewRegions(toServer)) { return Optional.of(getAction(fromServer, fromRegion, toServer, -1)); } // Compare locality gain/loss from swapping fromRegion with regions on toServer double fromRegionLocalityDelta = getWeightedLocality(cluster, fromRegion, toServer) - getWeightedLocality(cluster, fromRegion, fromServer); for (int toRegionIndex : getRandomIterationOrder(cluster.regionsPerServer[toServer].length)) { int toRegion = cluster.regionsPerServer[toServer][toRegionIndex]; double toRegionLocalityDelta = getWeightedLocality(cluster, toRegion, fromServer) - getWeightedLocality(cluster, toRegion, toServer); // If locality would remain neutral or improve, attempt the swap if (fromRegionLocalityDelta + toRegionLocalityDelta >= 0) { return Optional.of(getAction(fromServer, fromRegion, toServer, toRegion)); } } return Optional.absent(); }
/** Constructor for use by subclasses. */ protected FluentIterable() { this.iterableDelegate = Optional.absent(); }
/** * If {@code nullableReference} is non-null, returns an {@code Optional} instance containing that * reference; otherwise returns {@link Optional#absent}. * * <p><b>Comparison to {@code java.util.Optional}:</b> this method is equivalent to Java 8's * {@code Optional.ofNullable}. */ public static <T> Optional<T> fromNullable(@Nullable T nullableReference) { return (nullableReference == null) ? Optional.<T>absent() : new Present<T>(nullableReference); }
@Override public Optional<Long> sizeIfKnown() { if (file.isFile()) { return Optional.of(file.length()); } else { return Optional.absent(); } }
@Override public <V> Optional<V> transform(Function<? super T, V> function) { checkNotNull(function); return Optional.absent(); }
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) { WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value); return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get())); }
/** * Returns an {@link Optional} containing the first element in this fluent iterable. If the * iterable is empty, {@code Optional.absent()} is returned. * * <p><b>{@code Stream} equivalent:</b> if the goal is to obtain any element, {@link * Stream#findAny}; if it must specifically be the <i>first</i> element, {@code Stream#findFirst}. * * @throws NullPointerException if the first element is null; if this is a possibility, use {@code * iterator().next()} or {@link Iterables#getFirst} instead. */ public final Optional<E> first() { Iterator<E> iterator = getDelegate().iterator(); return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.<E>absent(); }
/** * Returns the size of this source in chars, if the size can be easily determined without actually * opening the data stream. * * <p>The default implementation returns {@link Optional#absent}. Some sources, such as a * {@code CharSequence}, may return a non-absent value. Note that in such cases, it is * <i>possible</i> that this method will return a different number of chars than would be returned * by reading all of the chars. * * <p>Additionally, for mutable sources such as {@code StringBuilder}s, a subsequent read may * return a different number of chars if the contents are changed. * * @since 19.0 */ @Beta public Optional<Long> lengthIfKnown() { return Optional.absent(); }
/** * Returns the size of this source in bytes, if the size can be easily determined without actually * opening the data stream. * * <p>The default implementation returns {@link Optional#absent}. Some sources, such as a file, * may return a non-absent value. Note that in such cases, it is <i>possible</i> that this method * will return a different number of bytes than would be returned by reading all of the bytes (for * example, some special files may return a size of 0 despite actually having content when read). * * <p>Additionally, for mutable sources such as files, a subsequent read may return a different * number of bytes if the contents are changed. * * @since 19.0 */ @Beta public Optional<Long> sizeIfKnown() { return Optional.absent(); }
/** * Creates a new media type with the given type and subtype. * * @throws IllegalArgumentException if type or subtype is invalid or if a wildcard is used for the * type, but not the subtype. */ public static MediaType create(String type, String subtype) { MediaType mediaType = create(type, subtype, ImmutableListMultimap.<String, String>of()); mediaType.parsedCharset = Optional.absent(); return mediaType; }
/** * Returns an optional charset for the value of the charset parameter if it is specified. * * @throws IllegalStateException if multiple charset values have been set for this media type * @throws IllegalCharsetNameException if a charset value is present, but illegal * @throws UnsupportedCharsetException if a charset value is present, but no support is available * in this instance of the Java virtual machine */ public Optional<Charset> charset() { // racy single-check idiom, this is safe because Optional is immutable. Optional<Charset> local = parsedCharset; if (local == null) { String value = null; local = Optional.absent(); for (String currentValue : parameters.get(CHARSET_ATTRIBUTE)) { if (value == null) { value = currentValue; local = Optional.of(Charset.forName(value)); } else if (!value.equals(currentValue)) { throw new IllegalStateException( "Multiple charset values defined: " + value + ", " + currentValue); } } parsedCharset = local; } return local; }
@Override public Optional<Long> sizeIfKnown() { Optional<Long> optionalUnslicedSize = ByteSource.this.sizeIfKnown(); if (optionalUnslicedSize.isPresent()) { long unslicedSize = optionalUnslicedSize.get(); long off = Math.min(offset, unslicedSize); return Optional.of(Math.min(length, unslicedSize - off)); } return Optional.absent(); }
private static MediaType createConstant(String type, String subtype) { MediaType mediaType = addKnownType(new MediaType(type, subtype, ImmutableListMultimap.<String, String>of())); mediaType.parsedCharset = Optional.absent(); return mediaType; }
@Override public Optional<Long> sizeIfKnown() { BasicFileAttributes attrs; try { attrs = readAttributes(); } catch (IOException e) { // Failed to get attributes; we don't know the size. return Optional.absent(); } // Don't return a size for directories or symbolic links; their sizes are implementation // specific and they can't be read as bytes using the read methods anyway. if (attrs.isDirectory() || attrs.isSymbolicLink()) { return Optional.absent(); } return Optional.of(attrs.size()); }
@Override public Optional<Long> sizeIfKnown() { long result = 0L; for (ByteSource source : sources) { Optional<Long> sizeIfKnown = source.sizeIfKnown(); if (!sizeIfKnown.isPresent()) { return Optional.absent(); } result += sizeIfKnown.get(); } return Optional.of(result); }
@Override public Optional<Long> lengthIfKnown() { long result = 0L; for (CharSource source : sources) { Optional<Long> lengthIfKnown = source.lengthIfKnown(); if (!lengthIfKnown.isPresent()) { return Optional.absent(); } result += lengthIfKnown.get(); } return Optional.of(result); }
/** * Returns an {@link Optional} containing the first element in {@code * iterator} that satisfies the given predicate, if such an element exists. If * no such element is found, an empty {@link Optional} will be returned from * this method and the iterator will be left exhausted: its {@code * hasNext()} method will return {@code false}. * * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code * null}. If {@code null} is matched in {@code iterator}, a * NullPointerException will be thrown. * * @since 11.0 */ public static <T> Optional<T> tryFind(Iterator<T> iterator, Predicate<? super T> predicate) { UnmodifiableIterator<T> filteredIterator = filter(iterator, predicate); return filteredIterator.hasNext() ? Optional.of(filteredIterator.next()) : Optional.<T>absent(); }