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 package optionsvalidation; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 28 public class DoubleJVMOption extends JVMOption { 29 30 private final double MAX_DOUBLE = 18446744073709551616.000; 31 32 /** 33 * Mininum option value 34 */ 35 private double min; 36 /** 37 * Maximum option value 38 */ 39 private double max; 40 41 /** 42 * Default constructor. 43 */ 44 private DoubleJVMOption() { 45 min = Double.MIN_VALUE; 46 max = MAX_DOUBLE; 47 } 48 49 /** 50 * Initialize double option with passed name 51 * 52 * @param name Name of the option 53 */ 54 DoubleJVMOption(String name) { 55 this(); 56 this.name = name; 57 } 58 59 /** 60 * Initialize double option with passed name, min and max values 61 * 62 * @param name Name of the option 63 * @param min Minimum value of the option 64 * @param max Maximum value of the option 65 */ 66 public DoubleJVMOption(String name, double min, double max) { 67 this(name); 68 this.min = min; 69 this.max = max; 70 } 71 72 /** 73 * Construct option string with passed value 74 * 75 * @param value Parameter value 76 * @return String containing option with passed value 77 */ 78 private String constructOption(String value) { 79 return "-XX:" + name + "=" + value; 80 } 81 82 /** 83 * Set new minimum option value 84 * 85 * @param min New minimum value 86 */ 87 @Override 88 void setMin(String min) { 89 this.min = new Double(min); 90 } 91 92 /** 93 * Get string with minimum value of the option 94 * 95 * @return String with minimum value of the option 96 */ 97 @Override 98 String getMin() { 99 return String.format("%f", min); 100 } 101 102 /** 103 * Set new maximum option value 104 * 105 * @param max New maximum value 106 */ 107 @Override 108 void setMax(String max) { 109 this.max = new Double(max); 110 } 111 112 /** 113 * Get string with maximum value of the option 114 * 115 * @return String with maximum value of the option 116 */ 117 @Override 118 String getMax() { 119 return String.format("%f", max); 120 } 121 122 /** 123 * Return list of strings with valid option values which used for testing 124 * using jcmd, attach and etc. 125 * 126 * @return List of strings which contain valid values for option 127 */ 128 @Override 129 protected List<String> getValidValues() { 130 List<String> validValues = new ArrayList<>(); 131 132 validValues.add(String.format("%f", min)); 133 validValues.add(String.format("%f", max)); 134 135 if (min == Double.MIN_VALUE && max == MAX_DOUBLE) { 136 validValues.add(String.format("%f", -1.5)); 137 validValues.add(String.format("%f", 0.0)); 138 validValues.add(String.format("%f", 0.85)); 139 } 140 141 return validValues; 142 } 143 144 /** 145 * Return list of strings with invalid option values which used for testing 146 * using jcmd, attach and etc. 147 * 148 * @return List of strings which contain invalid values for option 149 */ 150 @Override 151 protected List<String> getInvalidValues() { 152 List<String> invalidValues = new ArrayList<>(); 153 154 if (min != Double.MIN_VALUE) { 155 invalidValues.add(String.format("%f", min - 0.01)); 156 } 157 158 if (max != MAX_DOUBLE) { 159 invalidValues.add(String.format("%f", max + 0.01)); 160 } 161 162 return invalidValues; 163 } 164 165 /** 166 * Return list of strings with parameter with valid values. Valid values 167 * are: min, max Following values are added to valid if min and max not 168 * defined: -1, 0, 1 Following value is added if min is equal to 0 and max 169 * is greater than 0: 1 170 * 171 * @return List of String with parameters with valid values. 172 */ 173 @Override 174 protected List<String> getValidCommandLineOptions() { 175 List<String> validParameters = new ArrayList<>(); 176 177 for (String value : getValidValues()) { 178 validParameters.add(constructOption(value)); 179 } 180 181 return validParameters; 182 } 183 184 /** 185 * Return list of strings with parameter with invalid values. Invalid values 186 * are(if min, max are defined): min - 1, max + 1 187 * 188 * @return List of String with parameters with invalid values. 189 */ 190 @Override 191 protected List<String> getInvalidCommandLineOptions() { 192 List<String> invalidParameters = new ArrayList<>(); 193 194 for (String value : getInvalidValues()) { 195 invalidParameters.add(constructOption(value)); 196 } 197 198 return invalidParameters; 199 } 200 201 /** 202 * Return expected error message for option with value "value" when it used 203 * on command line with passed value 204 * 205 * @param value Option value 206 * @return Expected error message 207 */ 208 @Override 209 protected String getErrorMessageCommandLine(String value) { 210 String errorMsg; 211 212 if (withRange) { 213 /* Option have defined range in VM */ 214 errorMsg = "is outside the allowed range"; 215 } else { 216 errorMsg = ""; 217 } 218 219 return errorMsg; 220 } 221 }