View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package net.sf.zel.math;
7   
8   import java.io.Serializable;
9   import java.math.MathContext;
10  import javax.annotation.CheckReturnValue;
11  import javax.annotation.Nonnull;
12  import javax.annotation.concurrent.Immutable;
13  import net.sf.zel.nr.OperableBigDecimal;
14  import net.sf.zel.nr.OperableDouble;
15  import net.sf.zel.nr.OperableGNUBigFloat;
16  import net.sf.zel.nr.OperableNumber;
17  
18  /**
19   *
20   * @author zoly
21   */
22  @Immutable
23  public final class ExtendedMathContext
24          implements Serializable
25  {
26  
27      @Nonnull
28      private final MathContext context;
29  
30      @Nonnull
31      private final OperableNumber floatingPointRepresentation;
32  
33      private static final long serialVersionUID = 5579720004736356247L;
34  
35  
36      public ExtendedMathContext(final MathContext mc,final OperableNumber pFloatingPointRepresentation)
37      {
38          context = mc;
39          this.floatingPointRepresentation = pFloatingPointRepresentation;
40      }
41  
42      @CheckReturnValue
43      public MathContext getMathContext()
44      {
45          return context;
46      }
47  
48      @CheckReturnValue
49      public OperableNumber createFloatOperableNumber(final String str)
50      {
51          return floatingPointRepresentation.create(str);
52      }
53  
54      public static final ExtendedMathContext DECIMAL64_MATHCONTEXT =
55              new ExtendedMathContext(
56              MathContext.DECIMAL64, OperableBigDecimal.ZERO);
57  
58      public static final ExtendedMathContext DECIMAL128_MATHCONTEXT =
59              new ExtendedMathContext(
60              MathContext.DECIMAL128, OperableBigDecimal.ZERO);
61  
62      public static final ExtendedMathContext DOUBLE64_MATHCONTEXT =
63              new ExtendedMathContext(
64              new MathContext(64), OperableDouble.ZERO);
65  
66      /**
67       * If the following float representations are used,
68       * please make sure you have the native libraries in your classpath
69       */
70      public static final class NATIVE
71      {
72          private NATIVE() {}
73          public static final ExtendedMathContext GNUFLOAT128_MATHCONTEXT =
74                  new ExtendedMathContext(new MathContext(128), OperableGNUBigFloat.ZERO);
75  
76          public static final ExtendedMathContext GNUFLOAT256_MATHCONTEXT =
77                  new ExtendedMathContext(new MathContext(128), OperableGNUBigFloat.ZERO);
78  
79          public static final ExtendedMathContext GNUFLOAT512_MATHCONTEXT =
80                  new ExtendedMathContext(new MathContext(128), OperableGNUBigFloat.ZERO);
81      }
82  
83      public static final ThreadLocal<ExtendedMathContext> CURRENT_EXTENDED_MATH_CONTEXT =
84              new ThreadLocal<ExtendedMathContext>() {
85          @Override
86           protected synchronized ExtendedMathContext initialValue() {
87               return DOUBLE64_MATHCONTEXT;
88           }
89       };
90  
91  }