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