Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.brooklyn.util.guava;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.brooklyn.util.javalang.JavaClassNames;
import org.apache.brooklyn.util.javalang.MemoryUsageTracker.SoftUsageTracker;
import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableSet;
/** Like Guava Optional but permitting null and permitting errors to be thrown. */
public abstract class Maybe implements Serializable, Supplier {
private static final long serialVersionUID = -6372099069863179019L;
/** Returns an absent indicator. No message is available and access does not include any reference to this creation.
* Therefore it is fast and simple, but hard to work with if someone might {@link #get()} it and want a useful exception.
* See also {@link #absentNoTrace(String)} to include a message with very low overhead,
* or {@link #absentWithTrace(String)} or {@link #absent(Throwable)} for more control over the exception thrown.
*/
public static Maybe absent() {
return new Maybe.Absent();
}
/** Convenience for {@link #absentWithTrace(String)}. */
public static Maybe absent(final String message) {
return absent(new IllegalStateException(message));
}
/** Creates an absent whose {@link #get()} throws an {@link IllegalStateException} with the indicated message.
* Both stack traces (the cause and the callers) are provided, which can be quite handy,
* but comparatively expensive as the cause stack trace has to generated when this method is invoked,
* even if it is never accessed. See also {@link #absentNoTrace(String)} and {@link #absent(Throwable)}. */
public static Maybe absentWithTrace(final String message) {
return absent(new IllegalStateException(message));
}
/** Creates an absent whose get throws an {@link IllegalStateException} with the indicated message,
* but not a stack trace for the calling location. As stack traces can be comparatively expensive
* this is useful for efficiency, but it can make debugging harder as the origin of the absence is not kept,
* in contrast to {@link #absentWithTrace(String)} and {@link #absent(Throwable)}. */
public static Maybe absentNoTrace(final String message) {
return absent(new IllegalStateExceptionSupplier(message));
}
/** As {@link #absentWithTrace(String)} but using the provided exception instead of this location
* as the cause, and a string based on this cause as the message on the {@link IllegalStateException}
* thrown if a user does a {@link #get()}.
* Useful if an {@link Exception} has already been generated (and no overhead)
* or if you want to supply a specific cause as in absent(new MyException(...))
* (but there is the Exception creation overhead there). */
public static Maybe absent(final Throwable cause) {
return absent(new IllegalStateExceptionSupplier(cause));
}
/** As {@link #absent(Throwable)} but using the given message as the message on the {@link IllegalStateException}
* thrown if a user does a {@link #get()}. */
public static Maybe absent(final String message, final Throwable cause) {
return absent(new IllegalStateExceptionSupplier(message, cause));
}
/** Creates an absent whose {@link #get()} throws a {@link RuntimeException}
* generated on demand from the given supplier */
public static Maybe absent(final Supplier exceptionSupplier) {
return new Absent(Preconditions.checkNotNull(exceptionSupplier));
}
/** as {@link #absentNull(String)} but with a generic message */
public static Maybe absentNull() {
return absentNull("disallowed null value");
}
/** like {@link #absent(String)} but {@link #isNull()} will return true on the result. */
public static Maybe absentNull(String message) {
return new AbsentNull(message);
}
/** Creates a new Maybe object which is present.
* The argument may be null and the object still present,
* which may be confusing in some contexts
* (traditional {@link Optional} usages) but
* may be natural in others (where null is a valid value, distinguished from no value set).
* See also {@link #ofDisallowingNull(Object)}. */
public static Maybe ofAllowingNull(@Nullable T value) {
return new Present(value);
}
/** Creates a new Maybe object which is present if and only if the argument is not null.
* If the argument is null, then an {@link #absentNull()} is returned,
* on which {@link #isNull()} will be true. */
public static Maybe ofDisallowingNull(@Nullable T value) {
if (value==null) return absentNull();
return new Present(value);
}
/** Creates a new Maybe object.
* Currently this uses {@link #ofAllowingNull(Object)} semantics,
* but it is recommended to use that method for clarity
* if the argument might be null. */
// note: Optional throws if null is supplied; we might want to do the same here
public static Maybe of(@Nullable T value) {
return ofAllowingNull(value);
}
/**
* Casts the given value to the desired type. This is valid because {@link Maybe} is immutable,
* so things like {@code Maybe