首页

关于drools-core源码包中Operator操作符基于多例模式的设计实现源码分享

标签:多例模式,操作符设计,drools     发布时间:2018-04-12   

一、前言

关于drools-core源码包中的org.drools.base.evaluators.Operator数据操作符设计类,分别给予多例设计模式将大于、小于、不等于、大于等于、小于等于等分别单独对象注入类加载静态永久缓存区中,外部需要同静态分别引用多例对象操作变量定义使用。

二、源码说明

package org.drools.base.evaluators;@b@@b@import java.io.Externalizable;@b@import java.io.IOException;@b@import java.io.ObjectInput;@b@import java.io.ObjectOutput;@b@import java.io.ObjectStreamException;@b@import java.util.Collections;@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@public class Operator@b@  implements Externalizable, org.drools.runtime.rule.Operator@b@{@b@  private static final long serialVersionUID = 510L;@b@  private static final Map<String, Operator> CACHE = Collections.synchronizedMap(new HashMap());@b@  public static final Operator EQUAL = addOperatorToRegistry("==", false);@b@  public static final Operator NOT_EQUAL = addOperatorToRegistry("!=", false);@b@  public static final Operator LESS = addOperatorToRegistry("<", false);@b@  public static final Operator LESS_OR_EQUAL = addOperatorToRegistry("<=", false);@b@  public static final Operator GREATER = addOperatorToRegistry(">", false);@b@  public static final Operator GREATER_OR_EQUAL = addOperatorToRegistry(">=", false);@b@  private String operator;@b@  private boolean isNegated;@b@@b@  public static Operator addOperatorToRegistry(String operatorId, boolean isNegated)@b@  {@b@    Operator op = new Operator(operatorId, isNegated);@b@@b@    CACHE.put(getKey(operatorId, isNegated), op);@b@@b@    return op;@b@  }@b@@b@  public static Operator determineOperator(String operatorId, boolean isNegated)@b@  {@b@    Operator op = (Operator)CACHE.get(getKey(operatorId, isNegated));@b@@b@    return op;@b@  }@b@@b@  private static String getKey(String string, boolean isNegated)@b@  {@b@    return isNegated + ":" + string;@b@  }@b@@b@  public void readExternal(ObjectInput in)@b@    throws IOException, ClassNotFoundException@b@  {@b@    this.operator = ((String)in.readObject());@b@    this.isNegated = in.readBoolean();@b@  }@b@@b@  public void writeExternal(ObjectOutput out) throws IOException {@b@    out.writeObject(this.operator);@b@    out.writeBoolean(this.isNegated);@b@  }@b@@b@  public Operator()@b@  {@b@  }@b@@b@  private Operator(String operator, boolean isNegated)@b@  {@b@    this.operator = operator;@b@    this.isNegated = isNegated;@b@  }@b@@b@  private Object readResolve() throws ObjectStreamException {@b@    return determineOperator(this.operator, this.isNegated);@b@  }@b@@b@  public String toString()@b@  {@b@    return "Operator = '" + this.operator + "'";@b@  }@b@@b@  public String getOperatorString()@b@  {@b@    return this.operator;@b@  }@b@@b@  public boolean isNegated()@b@  {@b@    return this.isNegated;@b@  }@b@@b@  public int hashCode()@b@  {@b@    int PRIME = 31;@b@    int result = super.hashCode();@b@    result = 31 * result + ((this.isNegated) ? 1231 : 1237);@b@    result = 31 * result + ((this.operator == null) ? 0 : this.operator.hashCode());@b@    return result;@b@  }@b@@b@  public boolean equals(Object obj)@b@  {@b@    if (this == obj) return true;@b@    if (obj == null) return false;@b@    if (super.getClass() != obj.getClass()) return false;@b@    Operator other = (Operator)obj;@b@    if (this.isNegated != other.isNegated) return false;@b@    if (this.operator == null) {@b@      if (other.operator == null) break label76; return false; }@b@    label76: return (this.operator.equals(other.operator));@b@  }@b@}