⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultcomparetohandler.java

📁 jGAp 遗传算法 提不错的一款软件 最新的更新
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licensing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.impl;

import org.jgap.*;
import org.jgap.util.*;
import java.io.*;

/**
 * Default implementation for comparing Comparables. Boolean values are also
 * covered with this handler as the Boolean class has no compareTo method.
 *
 * @author Klaus Meffert
 * @since 2.6
 */
public class DefaultCompareToHandler
    implements ICompareToHandler, ICloneable, Serializable, Comparable {
  /** String containing the CVS revision. Read out via reflection!*/
  private static final String CVS_REVISION = "$Revision: 1.7 $";

  public boolean isHandlerFor(final Object a_obj, final Class a_clazz) {
    Class clazz;
    if (a_clazz == null) {
      if (a_obj == null) {
        return false;
      }
      clazz = a_obj.getClass();
    }
    else {
      clazz = a_clazz;
    }
    if (Comparable.class.isAssignableFrom(clazz)) {
      return true;
    }
    else {
      if (clazz != null && Boolean.class == clazz) {
        return true;
      }
      else {
        return false;
      }
    }
  }

  public Object perform(final Object a_obj, final Class a_class,
                        final Object a_params)
      throws Exception {
    int i;
    if (a_obj == null) {
      if (a_params != null) {
        i = -1;
      }
      else {
        i = 0;
      }
    }
    else if (a_params == null) {
      i = 1;
    }
    else {
      if (a_obj.getClass() == Boolean.class) {
        boolean b1 = ( (Boolean) a_obj).booleanValue();
        boolean b2 = ( (Boolean) a_params).booleanValue();
        if (b1 == b2) {
          i = 0;
        }
        else if (b1) {
          i = 1;
        }
        else
          i = -1;
      }
      else {
        i = ( (Comparable) a_obj).compareTo(a_params);
      }
    }
    return new Integer(i);
  }

  /**
   * @return deep clone of this instance
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Object clone() {
    return new DefaultCompareToHandler();
  }

  /**
   * @param a_other sic
   * @return as always
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public int compareTo(Object a_other) {
    if (a_other.getClass().equals(getClass())) {
      return 0;
    }
    else {
      return getClass().getName().compareTo(a_other.getClass().getName());
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -