/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed 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.glowroot.api;

import java.util.concurrent.ConcurrentMap;

import javax.annotation.Nullable;

import com.google.common.base.Objects;

/**
 * This class is modeled after Guava's Optional class. It can be useful for plugins passing a
 * {@link ConcurrentMap} to {@link Message#withDetail(String, java.util.Map)}. {@link ConcurrentMap}
 * does not accept {@code null} values, but if values are wrapped in {@code Optional}, then Glowroot
 * will unwrap the values and treat {@link Optional#absent()} the same as {@code null}.
 */
public abstract class Optional</*@NonNull*/T> {

    /**
     * Returns the contained instance, which must be present. If the instance might be absent, use
     * {@link #or(Object)} instead.
     * 
     * @throws IllegalStateException
     *             if the instance is absent ({@link #isPresent} returns {@code false})
     */
    public abstract T get();

    /**
     * Returns {@code true} if this holder contains a (non-{@code null}) instance.
     */
    public abstract boolean isPresent();

    /**
     * Returns the contained instance if it is present; {@code defaultValue} otherwise. If no
     * default value should be required because the instance is known to be present, use
     * {@link #get()} instead.
     */
    public abstract @Nullable T or(@Nullable T defaultValue);

    /**
     * Returns the contained instance if it is present; {@code null} otherwise. If the instance is
     * known to be present, use {@link #get()} instead.
     */
    public abstract @Nullable T orNull();

    /**
     * Returns an {@code Optional} instance with no contained reference.
     */
    @SuppressWarnings("unchecked")
    public static </*@NonNull*/T> Optional<T> absent() {
        return (Optional<T>) Absent.INSTANCE;
    }

    /**
     * Returns an {@code Optional} instance with no contained reference.
     * 
     * This version can be helpful to avoid unchecked casting.
     */
    @SuppressWarnings({"unchecked"})
    public static </*@NonNull*/T> Optional<T> absent(@SuppressWarnings("unused") Class<T> clazz) {
        return (Optional<T>) Absent.INSTANCE;
    }

    /**
     * Returns an {@code Optional} instance containing the given non-{@code null} reference.
     */
    public static </*@NonNull*/T> Optional<T> of(T reference) {
        return new Present<T>(reference);
    }

    /**
     * If {@code nullableReference} is non-{@code null}, returns an {@code Optional} instance
     * containing that reference; otherwise returns {@link Optional#absent}.
     */
    public static <T> Optional</*@NonNull*/T> fromNullable(T nullableReference) {
        if (nullableReference == null) {
            return absent();
        } else {
            return new Present</*@NonNull*/T>(nullableReference);
        }
    }

    private static class Absent extends Optional<Object> {
        private static final Absent INSTANCE = new Absent();
        @Override
        public Object get() {
            throw new IllegalStateException("Cannot retrieve value from Absent,"
                    + " check isPresent() first");
        }
        @Override
        public boolean isPresent() {
            return false;
        }
        @Override
        public @Nullable Object or(@Nullable Object defaultValue) {
            return defaultValue;
        }
        @Override
        public @Nullable Object orNull() {
            return null;
        }
    }

    private static class Present</*@NonNull*/T> extends Optional<T> {
        private final T reference;
        public Present(T reference) {
            this.reference = reference;
        }
        @Override
        public T get() {
            return reference;
        }
        @Override
        public boolean isPresent() {
            return true;
        }
        @Override
        public boolean equals(@Nullable Object o) {
            if (o instanceof Present) {
                Present<?> that = (Present<?>) o;
                return Objects.equal(reference, that.reference);
            }
            return false;
        }
        @Override
        public int hashCode() {
            return Objects.hashCode(reference);
        }
        @Override
        public T or(@Nullable T defaultValue) {
            return reference;
        }
        @Override
        public @Nullable T orNull() {
            return reference;
        }
    }
}

@@ -3172,20 +3172,8 @@
 tic 
-%3CT%3E Optional
 %3C/*@
@@ -3183,16 +3183,28 @@
 Null*/T%3E
+ Optional%3CT%3E
  fromNul
@@ -3209,16 +3209,26 @@
 ullable(
+@Nullable 
 T nullab

Fix checker build error