1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.graalvm.options;
  26 
  27 import java.util.Objects;
  28 
  29 /**
  30  * Represents the option key for an option specification.
  31  *
  32  * @since 1.0
  33  */
  34 public final class OptionKey<T> {
  35 
  36     private final OptionType<T> type;
  37     private final T defaultValue;
  38 
  39     /**
  40      * Constructs a new option key given a default value. Throws {@link IllegalArgumentException} if
  41      * no default {@link OptionType} could be {@link OptionType#defaultType(Object) resolved} for
  42      * the given type. The default value must not be <code>null</code>.
  43      *
  44      * @since 1.0
  45      */
  46     public OptionKey(T defaultValue) {
  47         Objects.requireNonNull(defaultValue);
  48         this.defaultValue = defaultValue;
  49         this.type = OptionType.defaultType(defaultValue);
  50         if (type == null) {
  51             throw new IllegalArgumentException("No default type specified for type " + defaultValue.getClass().getName() + ". Specify the option type explicitly to resolve this.");
  52         }
  53     }
  54 
  55     /**
  56      * Constructs a new option key given a default value and option key.
  57      *
  58      * @since 1.0
  59      */
  60     public OptionKey(T defaultValue, OptionType<T> type) {
  61         Objects.requireNonNull(type);
  62         this.defaultValue = defaultValue;
  63         this.type = type;
  64     }
  65 
  66     /**
  67      * Returns the option type of this key.
  68      *
  69      * @since 1.0
  70      */
  71     public OptionType<T> getType() {
  72         return type;
  73     }
  74 
  75     /**
  76      * Returns the default value for this option.
  77      *
  78      * @since 1.0
  79      */
  80     public T getDefaultValue() {
  81         return defaultValue;
  82     }
  83 
  84     /**
  85      * Returns the value of this key given the {@link OptionValues values}.
  86      *
  87      * @since 1.0
  88      */
  89     public T getValue(OptionValues values) {
  90         return values.get(this);
  91     }
  92 
  93     /**
  94      * Returns <code>true</code> if a value for this key has been set for the given option values or
  95      * <code>false</code> if no value has been set.
  96      *
  97      * @since 1.0
  98      */
  99     public boolean hasBeenSet(OptionValues values) {
 100         return values.hasBeenSet(this);
 101     }
 102 
 103 }