1/*
2 * Copyright (c) 2004, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.tools.jstat;
27
28import java.util.*;
29
30
31/**
32 * A typesafe enumeration for describing mathematical operators.
33 *
34 * @author Brian Doherty
35 * @since 1.5
36 */
37public abstract class Operator {
38
39    private static int nextOrdinal = 0;
40    private static HashMap<String, Operator> map = new HashMap<String, Operator>();
41
42    private final String name;
43    private final int ordinal = nextOrdinal++;
44
45    private Operator(String name) {
46        this.name = name;
47        map.put(name, this);
48    }
49
50    protected abstract double eval(double x, double y);
51
52    /* Operator '+' */
53    public static final Operator PLUS = new Operator("+") {
54        protected double eval(double x, double y) {
55            return x + y;
56        }
57    };
58
59    /* Operator '-' */
60    public static final Operator MINUS = new Operator("-") {
61        protected double eval(double x, double y) {
62            return x - y;
63        }
64    };
65
66    /* Operator '/' */
67    public static final Operator DIVIDE = new Operator("/") {
68        protected double eval(double x, double y) {
69            if (y == 0) {
70                return Double.NaN;
71            }
72            return x / y;
73        }
74    };
75
76    /* Operator '*' */
77    public static final Operator MULTIPLY = new Operator("*") {
78        protected double eval(double x, double y) {
79            return x * y;
80        }
81    };
82
83    /**
84     * Returns the string representation of this Operator object.
85     *
86     * @return  the string representation of this Operator object
87     */
88    public String toString() {
89        return name;
90    }
91
92    /**
93     * Maps a string to its corresponding Operator object.
94     *
95     * @param   s  an string to match against Operator objects.
96     * @return     The Operator object matching the given string.
97     */
98    public static Operator toOperator(String s) {
99        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}
112