001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: import java.util.*;
029:
030: /**
031: * A typesafe enumeration for describing mathematical operators.
032: *
033: * @author Brian Doherty
034: * @version 1.9, 05/09/07
035: * @since 1.5
036: */
037: public abstract class Operator {
038:
039: private static int nextOrdinal = 0;
040: private static HashMap<String, Operator> map = new HashMap<String, Operator>();
041:
042: private final String name;
043: private final int ordinal = nextOrdinal++;
044:
045: private Operator(String name) {
046: this .name = name;
047: map.put(name, this );
048: }
049:
050: protected abstract double eval(double x, double y);
051:
052: /* Operator '+' */
053: public static final Operator PLUS = new Operator("+") {
054: protected double eval(double x, double y) {
055: return x + y;
056: }
057: };
058:
059: /* Operator '-' */
060: public static final Operator MINUS = new Operator("-") {
061: protected double eval(double x, double y) {
062: return x - y;
063: }
064: };
065:
066: /* Operator '/' */
067: public static final Operator DIVIDE = new Operator("/") {
068: protected double eval(double x, double y) {
069: if (y == 0) {
070: return Double.NaN;
071: }
072: return x / y;
073: }
074: };
075:
076: /* Operator '*' */
077: public static final Operator MULTIPLY = new Operator("*") {
078: protected double eval(double x, double y) {
079: return x * y;
080: }
081: };
082:
083: /**
084: * Returns the string representation of this Operator object.
085: *
086: * @return the string representation of this Operator object
087: */
088: public String toString() {
089: return name;
090: }
091:
092: /**
093: * Maps a string to its corresponding Operator object.
094: *
095: * @param s an string to match against Operator objects.
096: * @return The Operator object matching the given string.
097: */
098: public static Operator toOperator(String s) {
099: return map.get(s);
100: }
101:
102: /**
103: * Returns an enumeration of the keys for this enumerated type
104: *
105: * @param s an string to match against Operator objects.
106: * @return The Operator object matching the given string.
107: */
108: protected static Set keySet() {
109: return map.keySet();
110: }
111: }
|