View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   */
43  
44  package org.exolab.jms.selector;
45  
46  
47  /***
48   * This class is an adapter for the Long type.
49   *
50   * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:50:44 $
51   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
52   * @see         SNumber
53   */
54  final class SLong extends SNumber {
55  
56      /***
57       * The wrapped value
58       */
59      private long _value;
60  
61      /***
62       * Construct a new <code>SLong</code>
63       *
64       * @param value the underlying value
65       */
66      public SLong(final long value) {
67          _value = value;
68      }
69  
70      /***
71       * Returns the addition of a number to this
72       *
73       * @param number the number to add
74       * @return the value of <code>this + number<code>
75       */
76      public SNumber add(final SNumber number) {
77          SNumber result;
78          if (number instanceof SDouble) {
79              result = promote().add(number);
80          } else {
81              result = new SLong(_value + number.getLong());
82          }
83          return result;
84      }
85  
86      /***
87       * Returns the value of the substraction of a number from this
88       *
89       * @param number the number to subtract
90       * @return the value of <code>this - number<code>
91       */
92      public SNumber subtract(final SNumber number) {
93          SNumber result;
94          if (number instanceof SDouble) {
95              result = promote().subtract(number);
96          } else {
97              result = new SLong(_value - number.getLong());
98          }
99          return result;
100     }
101 
102     /***
103      * Returns the multiplication of a number to this
104      *
105      * @param number the number to multiply
106      * @return the value of <code>this * number<code>
107      */
108     public SNumber multiply(final SNumber number) {
109         SNumber result;
110         if (number instanceof SDouble) {
111             result = promote().multiply(number);
112         } else {
113             result = new SLong(_value * number.getLong());
114         }
115         return result;
116     }
117 
118     /***
119      * Returns the division of a number from this
120      *
121      * @param number the number to divide
122      * @return the value of <code>this / number<code>
123      */
124     public SNumber divide(final SNumber number) {
125         SNumber result = null;
126         try {
127             if (number instanceof SDouble) {
128                 result = promote().divide(number);
129             } else {
130                 result = new SLong(_value / number.getLong());
131             }
132         } catch (ArithmeticException ignore) {
133         }
134         return result;
135     }
136 
137     /***
138      * Returns the value of this as a <code>long</code>
139      *
140      * @return the value of this as a <code>long</code>
141      */
142     public long getLong() {
143         return _value;
144     }
145 
146     /***
147      * Returns the value of this as a <code>double</code>
148      *
149      * @return the value of this as a <code>double</code>
150      */
151     public double getDouble() {
152         return _value;
153     }
154 
155     /***
156      * Returns the value of this, wrapped in a <code>Long</code>
157      *
158      * @return the value of this, wrapped in a <code>Long</code>
159      */
160     public Object getObject() {
161         return new Long(_value);
162     }
163 
164     /***
165      * Determines if this is equal to another object.
166      *
167      * @param obj the object to compare. An instance of <code>SNumber</code>
168      * @return <code>SBool.TRUE</code> if <code>this = obj</code>, otherwise
169      * <code>SBool.FALSE</code>
170      */
171     public SBool equal(final SObject obj) {
172         SBool result = null;
173         if (obj instanceof SLong) {
174             long rhs = ((SNumber) obj).getLong();
175             if (_value == rhs) {
176                 result = SBool.TRUE;
177             } else {
178                 result = SBool.FALSE;
179             }
180         } else if (obj instanceof SDouble) {
181             result = promote().equal(obj);
182         }
183         return result;
184     }
185 
186     /***
187      * Determines if this is less than another object.
188      *
189      * @param obj the object to compare. An instance of <code>SNumber</code>
190      * @return <code>SBool.TRUE</code> if <code>this &lt; obj</code>, otherwise
191      * <code>SBool.FALSE</code>
192      */
193     public SBool less(final SObject obj) {
194         SBool result = null;
195         if (obj instanceof SLong) {
196             long rhs = ((SNumber) obj).getLong();
197             if (_value < rhs) {
198                 result = SBool.TRUE;
199             } else {
200                 result = SBool.FALSE;
201             }
202         } else if (obj instanceof SDouble) {
203             result = promote().less(obj);
204         }
205         return result;
206     }
207 
208     /***
209      * Determines if this is greater than another object.
210      *
211      * @param obj the object to compare. An instance of <code>SNumber</code>
212      * @return <code>SBool.TRUE</code> if <code>this &gt; obj</code>, otherwise
213      * <code>SBool.FALSE</code>
214      */
215     public SBool greater(final SObject obj) {
216         SBool result = null;
217         if (obj instanceof SLong) {
218             long rhs = ((SNumber) obj).getLong();
219             if (_value > rhs) {
220                 result = SBool.TRUE;
221             } else {
222                 result = SBool.FALSE;
223             }
224         } else if (obj instanceof SDouble) {
225             result = promote().greater(obj);
226         }
227         return result;
228     }
229 
230     /***
231      * Promotes this to a double
232      *
233      * @return the value of this as a double
234      */
235     private SDouble promote() {
236         return new SDouble(_value);
237     }
238 
239 } //-- SLong