Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/Operator.java
41159 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstat;2627import java.util.*;282930/**31* A typesafe enumeration for describing mathematical operators.32*33* @author Brian Doherty34* @since 1.535*/36public abstract class Operator {3738private static int nextOrdinal = 0;39private static HashMap<String, Operator> map = new HashMap<String, Operator>();4041private final String name;42private final int ordinal = nextOrdinal++;4344private Operator(String name) {45this.name = name;46map.put(name, this);47}4849protected abstract double eval(double x, double y);5051/* Operator '+' */52public static final Operator PLUS = new Operator("+") {53protected double eval(double x, double y) {54return x + y;55}56};5758/* Operator '-' */59public static final Operator MINUS = new Operator("-") {60protected double eval(double x, double y) {61return x - y;62}63};6465/* Operator '/' */66public static final Operator DIVIDE = new Operator("/") {67protected double eval(double x, double y) {68if (y == 0) {69return Double.NaN;70}71return x / y;72}73};7475/* Operator '*' */76public static final Operator MULTIPLY = new Operator("*") {77protected double eval(double x, double y) {78return x * y;79}80};8182/**83* Returns the string representation of this Operator object.84*85* @return the string representation of this Operator object86*/87public String toString() {88return name;89}9091/**92* Maps a string to its corresponding Operator object.93*94* @param s an string to match against Operator objects.95* @return The Operator object matching the given string.96*/97public static Operator toOperator(String s) {98return map.get(s);99}100101/**102* Returns an enumeration of the keys for this enumerated type103*104* @param s an string to match against Operator objects.105* @return The Operator object matching the given string.106*/107protected static Set<?> keySet() {108return map.keySet();109}110}111112113