/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.slots.block;

/***
 * @author youji.zj
 * @author Eric Zhao
 */
public abstract class AbstractRule implements Rule {

    /**
     * Resource name.
     */
    private String resource;

    /**
     * <p>
     * Application name that will be limited by origin.
     * Multiple application name can be separated with comma (',').
     * </p>
     * <p>The default limitApp is `default`, which means allowing all origin apps.</p>
     * <p>For example: limitApp = `appA,appB` will limit requests from appA and appB.</p>
     */
    private String limitApp;

    public String getResource() {
        return resource;
    }

    public AbstractRule setResource(String resource) {
        this.resource = resource;
        return this;
    }

    public String getLimitApp() {
        return limitApp;
    }

    public AbstractRule setLimitApp(String limitApp) {
        this.limitApp = limitApp;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof AbstractRule)) {
            return false;
        }

        AbstractRule that = (AbstractRule)o;

        if (resource != null ? !resource.equals(that.resource) : that.resource != null) {
            return false;
        }
        // if (limitApp != null ? !limitApp.equals(that.limitApp) :
        // that.limitApp != null) { return false; }
        if (!limitAppEquals(limitApp, that.limitApp)) {
            return false;
        }
        return true;
    }

    private boolean limitAppEquals(String str1, String str2) {
        if ("".equals(str1)) {
            return "default".equals(str2);
        } else if ("default".equals(str1)) {
            return "".equals(str2) || str2 == null || str1.equals(str2);
        }
        if (str1 == null) {
            return str2 == null || "default".equals(str2);
        }
        return str1.equals(str2);
    }

    public <T extends AbstractRule> T as(Class<T> clazz) {
        return (T)this;
    }

    @Override
    public int hashCode() {
        int result = resource != null ? resource.hashCode() : 0;
        // result = 31 * result + (limitApp != null ? limitApp.hashCode() : 0);
        if (!("".equals(limitApp) || "default".equals(limitApp) || limitApp == null)) {
            result = 31 * result + limitApp.hashCode();
        }
        return result;
    }
}

@@ -659,16 +659,43 @@
 ck;%0A%0A/**
+%0A * Abstract rule entity.%0A 
 *%0A * @au
@@ -941,91 +941,8 @@
   * 
-Multiple application name can be separated with comma (',').%0A     * %3C/p%3E%0A     * %3Cp%3E
 The 
@@ -965,17 +965,23 @@
  is 
-%60
+%7B@code 
 default
-%60
+%7D
 , wh
@@ -1015,16 +1015,24 @@
 in apps.
+%0A     * 
 %3C/p%3E%0A   
@@ -1042,83 +1042,100 @@
  %3Cp%3E
-For example: limitApp = %60appA,appB%60 will limit requests from appA and appB.
+%0A     * For authority rules, multiple origin name can be separated with comma (',').%0A     * 
 %3C/p%3E
@@ -1908,128 +1908,8 @@
   %7D%0A
-        // if (limitApp != null ? !limitApp.equals(that.limitApp) :%0A        // that.limitApp != null) %7B return false; %7D%0A
     
@@ -2137,25 +2137,46 @@
  return 
-%22default%22
+RuleConstant.LIMIT_APP_DEFAULT
 .equals(
@@ -2201,25 +2201,46 @@
 lse if (
-%22default%22
+RuleConstant.LIMIT_APP_DEFAULT
 .equals(
@@ -2390,33 +2390,54 @@
  == null %7C%7C 
-%22default%22
+RuleConstant.LIMIT_APP_DEFAULT
 .equals(str2
@@ -2699,126 +2699,67 @@
     
-// result = 31 * result + (limitApp != null ? limitApp.hashCode() : 0);%0A        if (!(%22%22.equals(limitApp) %7C%7C %22default%22
+if (!(%22%22.equals(limitApp) %7C%7C RuleConstant.LIMIT_APP_DEFAULT
 .equ

Update javadoc and code refinement for AbstractRule