001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2014-2015 ForgeRock AS
025 */
026package org.forgerock.opendj.ldap;
027
028/**
029 * Public API for defining new options.
030 *
031 * @param <T> The option type.
032 */
033public final class Option<T> {
034    private final Class<T> type;
035    private final T defaultValue;
036
037    /**
038     * Defines a new {@link Boolean} option with the provided default value.
039     *
040     * @param defaultValue
041     *            The {@link Boolean} default value of this option.
042     * @return A new {@link Boolean} option with the provided default value.
043     */
044    public static Option<Boolean> withDefault(boolean defaultValue) {
045        return of(Boolean.class, defaultValue);
046    }
047
048    /**
049     * Defines a new option of the provided type with the provided default
050     * value.
051     *
052     * @param <T>
053     *            The type of this option.
054     * @param type
055     *            The type of the option.
056     * @param defaultValue
057     *            The new option default value.
058     * @return A new option of the provided type with the provided default
059     *         value.
060     */
061    public static <T> Option<T> of(Class<T> type, T defaultValue) {
062        return new Option<>(type, defaultValue);
063    }
064
065    private Option(Class<T> type, T defaultValue) {
066        this.type = type;
067        this.defaultValue = defaultValue;
068    }
069
070    /**
071     * Returns the type of this option.
072     *
073     * @return the type of this option.
074     */
075    public Class<T> getType() {
076        return type;
077    }
078
079    /**
080     * Returns the provided value if not {@code null}, otherwise returns the
081     * default one.
082     *
083     * @param value
084     *            The option which overrides the default one if not null.
085     * @return The provided value if not {@code null}, otherwise return the
086     *         default one.
087     */
088    public T getValue(Object value) {
089        return value != null ? type.cast(value) : defaultValue;
090    }
091}