1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package optionsvalidation;
  25 
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 public class DoubleJVMOption extends JVMOption {
  30 
  31     private final double MAX_DOUBLE = 18446744073709551616.000;
  32 
  33     /**
  34      * Mininum option value
  35      */
  36     private double min;
  37     /**
  38      * Maximum option value
  39      */
  40     private double max;
  41 
  42     /**
  43      * Default constructor.
  44      */
  45     private DoubleJVMOption() {
  46         min = Double.MIN_VALUE;
  47         max = MAX_DOUBLE;
  48     }
  49 
  50     /**
  51      * Initialize double option with passed name
  52      *
  53      * @param name Name of the option
  54      */
  55     DoubleJVMOption(String name) {
  56         this();
  57         this.name = name;
  58     }
  59     
  60     /**
  61      * Initialize double option with passed name, min and max values
  62      *
  63      * @param name Name of the option
  64      * @param min Minimum value of the option
  65      * @param max Maximum value of the option
  66      */
  67     public DoubleJVMOption(String name, double min, double max) {
  68         this(name);
  69         this.min = min;
  70         this.max = max;
  71     }
  72 
  73     /**
  74      * Set new minimum option value
  75      *
  76      * @param min New minimum value
  77      */
  78     @Override
  79     void setMin(String min) {
  80         this.min = new Double(min);
  81     }
  82 
  83     /**
  84      * Get string with minimum value of the option
  85      *
  86      * @return String with minimum value of the option
  87      */
  88     @Override
  89     String getMin() {
  90         return String.format("%f", min);
  91     }
  92 
  93     /**
  94      * Set new maximum option value
  95      *
  96      * @param max New maximum value
  97      */
  98     @Override
  99     void setMax(String max) {
 100         this.max = new Double(max);
 101     }
 102 
 103     /**
 104      * Get string with maximum value of the option
 105      *
 106      * @return String with maximum value of the option
 107      */
 108     @Override
 109     String getMax() {
 110         return String.format("%f", max);
 111     }
 112 
 113     /**
 114      * Return list of strings with valid option values which used for testing
 115      * using jcmd, attach and etc.
 116      *
 117      * @return List of strings which contain valid values for option
 118      */
 119     @Override
 120     protected List<String> getValidValues() {
 121         List<String> validValues = new ArrayList<>();
 122 
 123         validValues.add(String.format("%f", min));
 124         validValues.add(String.format("%f", max));
 125 
 126         if (min == Double.MIN_VALUE && max == MAX_DOUBLE) {
 127             validValues.add(String.format("%f", -1.5));
 128             validValues.add(String.format("%f", 0.0));
 129             validValues.add(String.format("%f", 0.85));
 130         }
 131 
 132         return validValues;
 133     }
 134 
 135     /**
 136      * Return list of strings with invalid option values which used for testing
 137      * using jcmd, attach and etc.
 138      *
 139      * @return List of strings which contain invalid values for option
 140      */
 141     @Override
 142     protected List<String> getInvalidValues() {
 143         List<String> invalidValues = new ArrayList<>();
 144 
 145         if (min != Double.MIN_VALUE) {
 146             invalidValues.add(String.format("%f", min - 0.01));
 147         }
 148 
 149         if (max != MAX_DOUBLE) {
 150             invalidValues.add(String.format("%f", max + 0.01));
 151         }
 152 
 153         return invalidValues;
 154     }
 155 
 156     /**
 157      * Return expected error message for option with value "value" when it used
 158      * on command line with passed value
 159      *
 160      * @param value Option value
 161      * @return Expected error message
 162      */
 163     @Override
 164     protected String getErrorMessageCommandLine(String value) {
 165         String errorMsg;
 166 
 167         if (withRange) {
 168             /* Option have defined range in VM */
 169             errorMsg = "is outside the allowed range";
 170         } else {
 171             errorMsg = "";
 172         }
 173 
 174         return errorMsg;
 175     }
 176 }