package jetbrains.mps.internal.make.runtime.util;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.make.IHName;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

@GeneratedClass(node = "r:f8580193-afc4-4673-a635-d4757ca591cf(jetbrains.mps.internal.make.runtime.util)/922321631991212011", model = "r:f8580193-afc4-4673-a635-d4757ca591cf(jetbrains.mps.internal.make.runtime.util)")
public abstract class DefaultHName<T> implements IHName<T> {
  private static Pattern FQNAME = Pattern.compile("(.+)\\.([^\\.]+)");
  private T parentName;
  private String name;
  protected DefaultHName(T parentName, String name) {
    this.parentName = parentName;
    this.name = name;
  }
  protected DefaultHName(String fqName) {
    Matcher m = FQNAME.matcher(fqName);
    if (m.matches()) {
      this.parentName = createParentName(m.group(1));
      this.name = m.group(2);
    } else {
      this.name = fqName;
    }
  }
  @Override
  public final T parentName() {
    return parentName;
  }
  @Override
  public final String fqName() {
    return prefix() + name;
  }
  @Override
  public String name() {
    return name;
  }
  @Override
  public boolean equals(Object that) {
    if (this == that) {
      return true;
    }
    if (!(that instanceof IHName)) {
      return false;
    }
    return fqName().equals(((IHName) that).fqName());
  }
  @Override
  public int hashCode() {
    return fqName().hashCode() * 43;
  }
  @Override
  public String toString() {
    return fqName();
  }
  /**
   * Override and make final. Called from the constructor when object is not fully constructed.
   * 
   * @param parentFqName  
   * @return  
   */
  protected abstract T createParentName(String parentFqName);
  private String prefix() {
    if (parentName == null) {
      return "";
    }
    return (((parentName instanceof IHName) ? ((IHName) parentName).fqName() : parentName.toString())) + ".";
  }
}
