001/* Compare two Double objects with a fuzzy threshold. 002 003 Copyright (c) 2006-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 */ 027package ptolemy.actor.util; 028 029import java.util.Comparator; 030 031/////////////////////////////////////////////////////////////////// 032//// FuzzyDoubleComparator 033 034/** 035 Compare two Double objects with respect to a fuzzy threshold. 036 The threshold is set by setThreshold(). If the difference of the 037 two double number is less than the threshold, then they are considered 038 equal. The default value of the fuzzy threshold is 1e-10. 039 <p> 040 After Ptolemy II 4.1, this class is not used any more. Use 041 {@link GeneralComparator} instead. 042 043 @author Jie Liu 044 @version $Id$ 045 @since Ptolemy II 5.2 046 @deprecated As Ptolemy II 4.1, use GeneralComparator instead. 047 048 @Pt.ProposedRating Yellow (liuj) 049 @Pt.AcceptedRating Yellow (hyzheng) 050 */ 051@Deprecated 052public class FuzzyDoubleComparator implements Comparator { 053 /** Construct a FuzzyDoubleComparator. The compare threshold is 054 * 1e-10 055 */ 056 public FuzzyDoubleComparator() { 057 _threshold = 1.0e-10; 058 } 059 060 /** Construct a FuzzyDoubleComparator with the given threshold. 061 * @param threshold The threshold 062 */ 063 public FuzzyDoubleComparator(double threshold) { 064 _threshold = threshold; 065 } 066 067 /////////////////////////////////////////////////////////////////// 068 //// public variables //// 069 070 /** Compare two objects according to a threshold. 071 * Return -1 if first < second - threshold/2; 072 * return 1 if first > second + threshold/2; 073 * return 0 otherwise. 074 * 075 * <p>If any of the argument is not a Double object, a ClassCastException 076 * will be thrown. 077 * @param first The first Double object. 078 * @param second The second Double object. 079 * @return The comparison result, -1, 0, or 1. 080 */ 081 @Override 082 public int compare(Object first, Object second) { 083 double firstValue = ((Double) first).doubleValue(); 084 double secondValue = ((Double) second).doubleValue(); 085 086 if (firstValue < secondValue - _threshold / 2.0) { 087 return -1; 088 } else if (firstValue > secondValue + _threshold / 2.0) { 089 return 1; 090 } else { 091 return 0; 092 } 093 } 094 095 /** Return the fuzziness threshold. 096 * @return The fuzziness threshold. 097 * @see #setThreshold(double) 098 */ 099 public double getThreshold() { 100 return _threshold; 101 } 102 103 /** Set the fuzziness threshold. The threshold is always positive. 104 * If the argument is negative, then its absolute value is taken. 105 * @param threshold The threshold. 106 * @see #getThreshold() 107 */ 108 public void setThreshold(double threshold) { 109 _threshold = Math.abs(threshold); 110 } 111 112 /////////////////////////////////////////////////////////////////// 113 //// private variables //// 114 115 /** The threshold that controls the fuzziness. Default value 1e-10. */ 116 private double _threshold; 117}