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 /** 32 * Additional double values to test 33 */ 34 private static final double ADDITIONAL_TEST_DOUBLE_NEGATIVE = -1.5; 35 private static final double ADDITIONAL_TEST_DOUBLE_ZERO = 0.0; 36 private static final double ADDITIONAL_TEST_DOUBLE_POSITIVE = 1.75; 37 38 /** 39 * Mininum option value 40 */ 41 private double min; 42 /** 43 * Maximum option value 44 */ 45 private double max; 46 47 /** 48 * Initialize double option with passed name 49 * 50 * @param name name of the option 51 */ 52 DoubleJVMOption(String name) { 53 this.name = name; 54 min = Double.MIN_VALUE; 55 max = Double.MAX_VALUE; 56 } 57 58 /** 59 * Initialize double option with passed name, min and max values 60 * 61 * @param name name of the option 62 * @param min minimum value of the option 63 * @param max maximum value of the option 64 */ 65 public DoubleJVMOption(String name, double min, double max) { 66 this(name); 67 this.min = min; 68 this.max = max; 69 } 70 71 /** 72 * Set new minimum option value 73 * 74 * @param min new minimum value 75 */ 76 @Override 77 void setMin(String min) { 78 this.min = new Double(min); 79 } 80 81 /** 82 * Get string with minimum value of the option 83 * 84 * @return string with minimum value of the option 85 */ 86 @Override 87 String getMin() { 88 return formatValue(min); 89 } 90 91 /** 92 * Set new maximum option value 93 * 94 * @param max new maximum value 95 */ 96 @Override 97 void setMax(String max) { 98 this.max = new Double(max); 99 } 100 101 /** 102 * Get string with maximum value of the option 103 * 104 * @return string with maximum value of the option 105 */ 106 @Override 107 String getMax() { 108 return formatValue(max); 109 } 110 111 private String formatValue(double value) { 112 return String.format("%f", value); 113 } 114 115 /** 116 * Return list of strings with valid option values which used for testing 117 * using jcmd, attach and etc. 118 * 119 * @return list of strings which contain valid values for option 120 */ 121 @Override 122 protected List<String> getValidValues() { 123 List<String> validValues = new ArrayList<>(); 124 125 validValues.add(formatValue(min)); 126 validValues.add(formatValue(max)); 127 128 if ((Double.compare(min, ADDITIONAL_TEST_DOUBLE_NEGATIVE) < 0) 129 && (Double.compare(max, ADDITIONAL_TEST_DOUBLE_NEGATIVE) > 0)) { 130 validValues.add(formatValue(ADDITIONAL_TEST_DOUBLE_NEGATIVE)); 131 } 132 133 if ((Double.compare(min, ADDITIONAL_TEST_DOUBLE_ZERO) < 0) 134 && (Double.compare(max, ADDITIONAL_TEST_DOUBLE_ZERO) > 0)) { 135 validValues.add(formatValue(ADDITIONAL_TEST_DOUBLE_ZERO)); 136 } 137 138 if ((Double.compare(min, ADDITIONAL_TEST_DOUBLE_POSITIVE) < 0) 139 && (Double.compare(max, ADDITIONAL_TEST_DOUBLE_POSITIVE) > 0)) { 140 validValues.add(formatValue(ADDITIONAL_TEST_DOUBLE_POSITIVE)); 141 } 142 143 return validValues; 144 } 145 146 /** 147 * Return list of strings with invalid option values which used for testing 148 * using jcmd, attach and etc. 149 * 150 * @return list of strings which contain invalid values for option 151 */ 152 @Override 153 protected List<String> getInvalidValues() { 154 List<String> invalidValues = new ArrayList<>(); 155 156 if (withRange) { 157 /* Return invalid values only for options which have defined range in VM */ 158 if (Double.compare(min, Double.MIN_VALUE) != 0) { 159 if ((Double.compare(min, 0.0) > 0) 160 && (Double.isNaN(min * 0.999) == false)) { 161 invalidValues.add(formatValue(min * 0.999)); 162 } else if ((Double.compare(min, 0.0) < 0) 163 && (Double.isNaN(min * 1.001) == false)) { 164 invalidValues.add(formatValue(min * 1.001)); 165 } 166 } 167 168 if (Double.compare(max, Double.MAX_VALUE) != 0) { 169 if ((Double.compare(max, 0.0) > 0) 170 && (Double.isNaN(max * 1.001) == false)) { 171 invalidValues.add(formatValue(max * 1.001)); 172 } else if ((Double.compare(max, 0.0) < 0) 173 && (Double.isNaN(max * 0.999) == false)) { 174 invalidValues.add(formatValue(max * 0.999)); 175 } 176 } 177 } 178 179 return invalidValues; 180 } 181 182 /** 183 * Return expected error message for option with value "value" when it used 184 * on command line with passed value 185 * 186 * @param value option value 187 * @return expected error message 188 */ 189 @Override 190 protected String getErrorMessageCommandLine(String value) { 191 String errorMsg; 192 193 if (withRange) { 194 /* Option have defined range in VM */ 195 errorMsg = "is outside the allowed range"; 196 } else { 197 errorMsg = ""; 198 } 199 200 return errorMsg; 201 } 202 }