package org.batfish.datamodel.routing_policy.expr;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.batfish.datamodel.AsPath;
import org.batfish.datamodel.AsPathAccessList;
import org.batfish.datamodel.BgpRoute;
import org.batfish.datamodel.routing_policy.Environment;

public class NamedAsPathSet extends AsPathSetExpr {

  /** */
  private static final long serialVersionUID = 1L;

  private String _name;

  @JsonCreator
  private NamedAsPathSet() {}

  public NamedAsPathSet(String name) {
    _name = name;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    NamedAsPathSet other = (NamedAsPathSet) obj;
    if (_name == null) {
      if (other._name != null) {
        return false;
      }
    } else if (!_name.equals(other._name)) {
      return false;
    }
    return true;
  }

  public String getName() {
    return _name;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((_name == null) ? 0 : _name.hashCode());
    return result;
  }

  @Override
  public boolean matches(Environment environment) {
    AsPathAccessList list = environment.getConfiguration().getAsPathAccessLists().get(_name);
    if (list != null) {
      boolean match = false;
      AsPath inputAsPath = null;
      if (environment.getUseOutputAttributes()
          && environment.getOutputRoute() instanceof BgpRoute.Builder) {
        BgpRoute.Builder bgpRouteBuilder = (BgpRoute.Builder) environment.getOutputRoute();
        inputAsPath = new AsPath(bgpRouteBuilder.getAsPath());
      } else if (environment.getReadFromIntermediateBgpAttributes()) {
        inputAsPath = new AsPath(environment.getIntermediateBgpAttributes().getAsPath());
      } else if (environment.getOriginalRoute() instanceof BgpRoute) {
        BgpRoute bgpRoute = (BgpRoute) environment.getOriginalRoute();
        inputAsPath = bgpRoute.getAsPath();
      }
      if (inputAsPath != null) {
        match = list.permits(inputAsPath);
      }
      return match;
    } else {
      environment.setError(true);
      return false;
    }
  }

  public void setName(String name) {
    _name = name;
  }
}

@@ -98,16 +98,85 @@
 reator;%0A
+import com.google.common.base.MoreObjects;%0Aimport java.util.Objects;%0A
 import o
@@ -704,85 +704,40 @@
  if 
+(!
 (obj 
-== null) %7B%0A      return false;%0A    %7D%0A    if (getClass() != obj.getClass(
+instanceof NamedAsPathSet
 )) %7B
@@ -820,121 +820,30 @@
     
-if (_name == null) %7B%0A      if (other._name != null) %7B%0A        return false;%0A      %7D%0A    %7D else if (!_name
+return Objects
 .equals(
 othe
@@ -838,16 +838,23 @@
 .equals(
+_name, 
 other._n
@@ -861,53 +861,8 @@
 ame)
-) %7B%0A      return false;%0A    %7D%0A    return true
 ;%0A  
@@ -2209,11 +2209,192 @@
 ame;%0A  %7D
+%0A%0A  @Override%0A  public String toString() %7B%0A    return MoreObjects.toStringHelper(NamedAsPathSet.class)%0A        .omitNullValues()%0A        .add(%22name%22, _name)%0A        .toString();%0A  %7D
 %0A%7D%0A

add toString