View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.layout;
21  
22  /**
23   * @deprecated since Tobago 3.0.1
24   */
25  @Deprecated
26  public final class MathUtils {
27  
28    /**
29     * Values smaller than this EPSILON should be treated as zero.
30     */
31    public static final double EPSILON = 0.0000001;
32  
33    private MathUtils() {
34    }
35  
36    /**
37     * Adjusts the list of double values to rounded values with the same sum.
38     * E. g. 2.3, 2.4, 2.5, 2.8 -> 2.0, 2.0, 3.0, 3.0
39     */
40    public static void adjustRemainders(final double[] list, final double initialBias) {
41      double bias = initialBias;
42      for (final double ignored : list) {
43        final double lastBias;
44        if (bias < 0.0) {
45          lastBias = findAndAdjustMaxRemainder(list);
46        } else {
47          lastBias = findAndAdjustMinRemainder(list);
48        }
49        if (isZero(lastBias)) {
50          break;
51        }
52        bias += lastBias;
53      }
54  
55      assert isZero(bias);
56    }
57  
58    public static double findAndAdjustMaxRemainder(final double[] list) {
59      double max = 0.0;
60      Integer indexOfMax = null;
61      for (int i = 0; i < list.length; i++) {
62        final double remainder = remainder(list[i]);
63        if (remainder > max + EPSILON) {
64          max = remainder;
65          indexOfMax = i;
66        }
67      }
68      if (indexOfMax != null) {
69        list[indexOfMax] += 1.0 - max;
70        return 1.0 - max;
71      }
72      return 0.0;
73    }
74  
75    public static double findAndAdjustMinRemainder(final double[] list) {
76      double min = 1.0;
77      Integer indexOfMin = null;
78      for (int i = 0; i < list.length; i++) {
79        final double remainder = remainder(list[i]);
80        if (Double.compare(remainder, 0) == 0) { // is zero
81          continue;
82        }
83        if (remainder < min - EPSILON) {
84          min = remainder;
85          indexOfMin = i;
86        }
87      }
88      if (indexOfMin != null) {
89        list[indexOfMin] -= min;
90        return -min;
91      }
92      return 0.0;
93    }
94  
95    public static double remainder(final double v) {
96      return v - Math.floor(v);
97    }
98  
99    public static boolean isZero(final double factor) {
100     return Math.abs(factor) < EPSILON;
101   }
102 
103   public static boolean isNotZero(final double factor) {
104     return Math.abs(factor) >= EPSILON;
105   }
106 
107   public static boolean isInteger(final double value) {
108     return isZero(value - Math.round(value));
109   }
110 
111   public static boolean isNotInteger(final double value) {
112     return isNotZero(value - Math.round(value));
113   }
114 }