/** * 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(); }
@Override public Optional<Long> sizeIfKnown() { return Optional.of((long) length); }
@Override public Optional<Long> lengthIfKnown() { return Optional.of((long) seq.length()); }
/** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public GraphBuilder<N> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
@Override public Optional<Long> sizeIfKnown() { if (file.isFile()) { return Optional.of(file.length()); } else { return Optional.absent(); } }
/** * Specifies the expected number of nodes in the network. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public NetworkBuilder<N, E> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
/** * Specifies the expected number of edges in the network. * * @throws IllegalArgumentException if {@code expectedEdgeCount} is negative */ public NetworkBuilder<N, E> expectedEdgeCount(int expectedEdgeCount) { this.expectedEdgeCount = Optional.of(checkNonNegative(expectedEdgeCount)); return this; }
/** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public ValueGraphBuilder<N, V> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
/** * 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(); }
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 a new instance with the same type and subtype as this instance, with the * {@code charset} parameter set to the {@link Charset#name name} of the given charset. Only one * {@code charset} parameter will be present on the new instance regardless of the number set on * this one. * * <p>If a charset must be specified that is not supported on this JVM (and thus is not * representable as a {@link Charset} instance, use {@link #withParameter}. */ public MediaType withCharset(Charset charset) { checkNotNull(charset); MediaType withCharset = withParameter(CHARSET_ATTRIBUTE, charset.name()); // precache the charset so we don't need to parse it withCharset.parsedCharset = Optional.of(charset); return withCharset; }
private static MediaType createConstantUtf8(String type, String subtype) { MediaType mediaType = addKnownType(new MediaType(type, subtype, UTF_8_CONSTANT_PARAMETERS)); mediaType.parsedCharset = Optional.of(UTF_8); 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(); }
@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(); }
@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()); }