package net.ranides.demo.debugger.impl;

import com.sun.jdi.Locatable;
import com.sun.jdi.Location;
import com.sun.jdi.request.BreakpointRequest;
import net.ranides.demo.debugger.DiLocation;

import net.ranides.assira.generic.HashUtils;

public class CDiLocation implements DiLocation {

    private final Location location;

    public CDiLocation(Locatable element) {
        this(element.location());
    }

    public CDiLocation(Location location) {
        this.location = location;
    }

    @Override
    public String name() {
        return location.declaringType().name();
    }

    @Override
    public int line() {
        return location.lineNumber();
    }

    @Override
    public BreakpointRequest breakpoint() {
        return location.virtualMachine().eventRequestManager().createBreakpointRequest(location);
    }

    @Override
    public int hashCode() {
        return HashUtils.hashPair(name(), line());
    }

    @Override
    public boolean equals(Object other) {
        return (other == this) || (other instanceof DiLocation) && corresponds((DiLocation) other);
    }

    @Override
    public boolean corresponds(Locatable other) {
        return corresponds(other.location());
    }

    @Override
    public boolean corresponds(Location other) {
        return name().equals(other.declaringType().name()) && line() == other.lineNumber();
    }

    @Override
    public boolean corresponds(DiLocation other) {
        return name().equals(other.name()) && line() == other.line();
    }
}
