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. The default value and the
  57      * type must not be <code>null</code>.
  58      *
  59      * @since 1.0
  60      */
  61     public OptionKey(T defaultValue, OptionType<T> type) {
  62         Objects.requireNonNull(defaultValue);
  63         Objects.requireNonNull(type);
  64         this.defaultValue = defaultValue;
  65         this.type = type;
  66     }
  67 
  68     /**
  69      * Returns the option type of this key.
  70      *
  71      * @since 1.0
  72      */
  73     public OptionType<T> getType() {
  74         return type;
  75     }
  76 
  77     /**
  78      * Returns the default value for this option.
  79      *
  80      * @since 1.0
  81      */
  82     public T getDefaultValue() {
  83         return defaultValue;
  84     }
  85 
  86     /**
  87      * Returns the value of this key given the {@link OptionValues values}.
  88      *
  89      * @since 1.0
  90      */
  91     public T getValue(OptionValues values) {
  92         return values.get(this);
  93     }
  94 
  95     /**
  96      * Returns <code>true</code> if a value for this key has been set for the given option values or
  97      * <code>false</code> if no value has been set.
  98      *
  99      * @since 1.0
 100      */
 101     public boolean hasBeenSet(OptionValues values) {
 102         return values.hasBeenSet(this);
 103     }
 104 
 105 }